File: lowlevel.c

package info (click to toggle)
gphoto 0.3.5-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,636 kB
  • ctags: 1,973
  • sloc: ansic: 23,035; sh: 4,851; makefile: 175
file content (70 lines) | stat: -rw-r--r-- 1,290 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

#include "lowlevel.h"
#include "error.h"

char qm100_readByte(int serialdev)
{
  char byte;
  if ((read(serialdev, &byte, 1)) < -1) qm100_error(serialdev, "Cannot read from device");

#ifdef _CLI_
  if (qm100_showReadBytes) qm100_iostat("recv :", &byte, 1);
#endif

  return byte;
}

char qm100_readTimedByte(int serialdev)
{
  fd_set rfds;
  struct timeval tv;

  FD_ZERO(&rfds);
  FD_SET(serialdev, &rfds);
  tv.tv_sec=0;
  tv.tv_usec=10000;
  return (select(1+serialdev, &rfds, NULL, NULL, &tv));
}

char qm100_readCodedByte(int serialdev)
{
  char byte;
  byte=qm100_readByte(serialdev);
  if (byte==0x1b)
    {
      byte=qm100_readByte(serialdev);
      byte=(~byte & 0xff);
    }
  return byte;
}

void qm100_writeByte(int serialdev, char data)
{
  usleep(10);
  if ((write(serialdev, &data, 1)) < -1) qm100_error(serialdev, "Cannot write to device");

#ifdef _CLI_
  if (qm100_showWriteBytes) qm100_iostat("sent :", &data, 1);
#endif

}

void qm100_iostat(unsigned char *str, unsigned char *buf, int len)
{
  printf("%s [ ", str);

  if(len>0)
    {
      int p = 1;

      printf("0x%x", buf[0]);

      while(p<len)
          printf(", 0x%x", (unsigned char)buf[p++]);
    }
  printf(" ]\n");
}