home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / wincam.zip / winc_src.zip / find.c < prev    next >
Text File  |  1997-03-01  |  7KB  |  256 lines

  1. /*
  2.  *
  3.  * probe for and discover information about the camera
  4.  *
  5.  * Copyright (C) 1996, Paul G. Fox
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify it
  8.  * under the terms of the GNU General Public License as published by the
  9.  * Free Software Foundation; either version 2 of the License, or (at your
  10.  * option) any later version.
  11.  * 
  12.  * This program is distributed in the hope that it will be useful, but
  13.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU General Public License along
  18.  * with this program; if not, write to the Free Software Foundation, Inc.,
  19.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  *
  21.  * This software was created with the help of proprietary information
  22.  * belonging to StarDot Technologies.
  23.  *
  24.  * $Header: E:/winc/RCS/find.c 1.1 1997/03/01 03:44:14 Derek Exp Derek $
  25.  */
  26. #define NDEBUG 1
  27.  
  28.  
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <assert.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include "private.h"
  37. #include "trace.h"
  38.  
  39. /*
  40.  * winc_find() looks for a camera, currently on the configured device; the
  41.  * default is "/dev/ttyCamera".  (i set that up as a symlink to the real
  42.  * special device.) the "cam_t" handle which is returned will be used in
  43.  * all subsequent communications with the camera.
  44.  */
  45. /*
  46.  * if we were to support multiple cameras, this
  47.  * routine would find camera N.  i think.  in any
  48.  * case, it would find a camera, and fill in a camera_s
  49.  * struct, with where it lives, what id it has, etc.
  50.  * i'm ignoring the multiple camera per tty facility for now,
  51.  * since the one camera per tty case seems a lot more likely in
  52.  * the near future.  ttys are cheaper than cameras.
  53.  */
  54. cam_t
  55. winc_find( int which )
  56. {
  57.     cam_t cam;
  58.     int baud;
  59.     int reset = 0;
  60.     int maybe_modem = 0;
  61.     int is_modem = 0;
  62.     char *chatargs;
  63.  
  64.     TRACE("va", "%s %s\n%s\n", VERSION, DATE, COPYRIGHT);
  65.  
  66.     cam = (cam_t)malloc(sizeof(struct camera_s));
  67.     if (!cam) {
  68.     errormsg(__FILE__ ": couldn't malloc camera_s struct\n");
  69.     return 0;
  70.     }
  71.  
  72.     memset(cam, 0, sizeof(*cam));
  73.  
  74.     winc_locks_init(cam);
  75.  
  76.     cam->ttyname = winc_configvar("WinCamDevice", "COM2");
  77.  
  78.     if (comms_initialize_port(cam) != OK) {
  79.     free(cam);
  80.     return 0;
  81.     }
  82.  
  83.     /* see if it might be modem-connected */
  84.     chatargs = winc_configvar("WinCamModemChat", "");
  85.     if (chatargs && *chatargs)
  86.         maybe_modem++;
  87.  
  88.  
  89.     /* try talking to the camera -- if we can't, reset it.  then
  90.      * try talking again, and try successively lower baud rates.
  91.      */
  92.  
  93.     /* start out fast */
  94.     for (baud = comms_first_baud();
  95.             !is_modem && baud >= 0;
  96.         baud = comms_next_baud(baud)) {
  97.     TRACE("A","Trying baud rate %d on tty %s\n",
  98.             baud, cam->ttyname);
  99.     retry:
  100.  
  101.     if (comms_set_baud(cam, baud) != OK)
  102.         continue;
  103.  
  104.     if (maybe_modem) {
  105.  
  106.         if (winc_check_for_modem( cam ) == OK) {
  107.             TRACE("va", "Found modem: tty %s, baud %d, running \n  %s\n",
  108.             cam->ttyname, cam->baud_index, chatargs);
  109.  
  110.         is_modem = 1;
  111.  
  112.         /* spawn chat program */
  113.  
  114.                 system(chatargs);
  115.  
  116.         TRACE("a", __FILE__ ": chat program completed ok\n");
  117.         }
  118.  
  119.     }
  120.  
  121.     if (winc_getstatus( cam ) == OK) {
  122.  
  123.         TRACE("va", "Found camera: tty %s, baud %d, "
  124.             "version %d.%d%c, id %d\n",
  125.         cam->ttyname, baud,
  126.         cam->version.major, cam->version.minor, cam->version.chiptype,
  127.         (cam->id.id[3] << 24) +
  128.         (cam->id.id[2] << 16) +
  129.         (cam->id.id[1] <<  8) +
  130.         (cam->id.id[0] <<  0)  );
  131.  
  132.         TRACE("va", "  (manufactured %02d/%02d/%02d %02d:%02d:%02d)\n",
  133.         (cam->id.id[3] & 0x0f),         /* month */
  134.         (cam->id.id[2] & 0xf8) >> 3,        /* day */
  135.         ((cam->id.id[3] & 0xf0) >> 4) + 1996,    /* year */
  136.  
  137.         (cam->id.id[2] & 0x07) +
  138.         ((cam->id.id[1] & 0xc0) >> 6),        /* hour */
  139.         (cam->id.id[1] & 0x3f),         /* minute */
  140.         (cam->id.id[0] & 0xfc) >> 2        /* second */
  141.         );
  142.  
  143.         /* set default useful values for basic camera parameters */
  144.         if (winc_extrastop(cam, 
  145.             cfg_number(winc_configvar("WinCamStopBits", "0"))) != OK)
  146.         return NotOK;
  147.         if (winc_linesync(cam, 
  148.             cfg_boolean(winc_configvar("WinCamLineSync", "on"))) != OK)
  149.         return NotOK;
  150.         if (winc_sensitivity(cam, 
  151.             cfg_number(winc_configvar("WinCamSensitivity", "15"))) != OK)
  152.         return NotOK;
  153.  
  154.         return cam;
  155.     }
  156.     if (!reset) { /* try a reset after first failure */
  157.         if (winc_reset(cam) != OK) {
  158.         goto fail;
  159.         }
  160.         reset++;
  161.         goto retry; /* retry first failed baud rate */
  162.     }
  163.     }
  164.  
  165. fail:
  166.     comms_close_port(cam);
  167.     if (cam)
  168.     free(cam);
  169.     return 0;
  170. }
  171.  
  172. void
  173. winc_close(cam_t cam)
  174. {
  175.     TRACE("a", __FILE__ ": closing camera\n");
  176.     winc_flush(cam);
  177.     comms_close_port(cam);
  178.     if (cam->starfield)
  179.         free(cam->starfield);
  180.     free(cam);
  181. }
  182.  
  183. result
  184. winc_getstatus( cam_t cam )
  185. {
  186.     byte retbytes[8];
  187.  
  188.     assert(cam);
  189.  
  190.     TRACE("a", __FILE__ ": getting camera status\n");
  191.     if (winc_send_cmd(cam, Cmd_send_version, -1) != OK)
  192.     return NotOK;
  193.  
  194.     if (winc_get_resp(cam, 3, retbytes, 0) != OK)
  195.     return NotOK;
  196.  
  197.     cam->version.minor = retbytes[0];
  198.     cam->version.major = retbytes[1];
  199.     cam->version.chiptype = retbytes[2];
  200.  
  201.     if (winc_send_cmd(cam, Cmd_send_id, -1) != OK)
  202.     return NotOK;
  203.  
  204.     if (winc_get_resp(cam, 4, (byte *)&(cam->id), 0) != OK)
  205.     return NotOK;
  206.  
  207.     return OK;
  208. }
  209.  
  210. result
  211. winc_extrastop( cam_t cam,
  212.             int extra_stop)    /* add stop bits */
  213. {
  214.  
  215.     assert(cam);
  216.  
  217.     TRACE("A", __FILE__ ": setting %d extra stop bits\n", extra_stop);
  218.     if (winc_send_cmd(cam, Cmd_extra_stop, extra_stop, -1) != OK)
  219.     return NotOK;
  220.  
  221.     return OK;
  222. }
  223.  
  224. result
  225. winc_linesync( cam_t cam,
  226.             int sync_60hz)  /* for 60hz flicker */
  227. {
  228.  
  229.     assert(cam);
  230.  
  231.     TRACE("A", __FILE__ ": turning flouresnent sync mode %s\n",
  232.                 sync_60hz ? "on" : "off");
  233.     if (winc_send_cmd(cam, Cmd_sync_mode, (sync_60hz != 0), -1) != OK)
  234.     return NotOK;
  235.  
  236.     return OK;
  237. }
  238.  
  239. result
  240. winc_sensitivity( cam_t cam,
  241.             int sensitivity )        /* light sensitivity */
  242. {
  243.  
  244.     assert(cam);
  245.  
  246.     if (sensitivity < 0) sensitivity = 0;
  247.     if (sensitivity > 15) sensitivity = 15;
  248.     TRACE("A", __FILE__ ": setting light sensitivity level to %d\n",
  249.                 sensitivity);
  250.     if (winc_send_cmd(cam, Cmd_set_light, sensitivity, -1) != OK)
  251.     return NotOK;
  252.  
  253.     return OK;
  254. }
  255.  
  256.