Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 141 lines (110 sloc) 3.247 kB
d93e1d9 @ladyada super simple Python interface sketch
ladyada authored
1 # python code for interfacing to VC0706 cameras and grabbing a photo
2 # pretty basic stuff
3 # written by ladyada. MIT license
4
5 import serial
6
7 BAUD = 38400
8 PORT = "COM1" # change this to your com port!
9 TIMEOUT = 0.2
10
11 SERIALNUM = 0 # start with 0
12
13 COMMANDSEND = 0x56
14 COMMANDREPLY = 0x76
15 COMMANDEND = 0x00
16
17 CMD_GETVERSION = 0x11
18 CMD_RESET = 0x26
19 CMD_TAKEPHOTO = 0x36
20 CMD_READBUFF = 0x32
21 CMD_GETBUFFLEN = 0x34
22
23 FBUF_CURRENTFRAME = 0x00
24 FBUF_NEXTFRAME = 0x01
25 FBUF_STOPCURRENTFRAME = 0x00
26
27 getversioncommand = [COMMANDSEND, SERIALNUM, CMD_GETVERSION, COMMANDEND]
28 resetcommand = [COMMANDSEND, SERIALNUM, CMD_RESET, COMMANDEND]
29 takephotocommand = [COMMANDSEND, SERIALNUM, CMD_TAKEPHOTO, 0x01, FBUF_STOPCURRENTFRAME]
30 getbufflencommand = [COMMANDSEND, SERIALNUM, CMD_GETBUFFLEN, 0x01, FBUF_CURRENTFRAME]
31
32
33 def checkreply(r, b):
34 r = map (ord, r)
35 if (r[0] == 0x76 and r[1] == SERIALNUM and r[2] == b and r[3] == 0x00):
36 return True
37 return False
38
39 def reset():
40 cmd = ''.join (map (chr, resetcommand))
41 s.write(cmd)
42 reply = s.read(100)
43 r = list(reply)
44 if checkreply(r, CMD_RESET):
45 return True
46 return False
47
48 def getversion():
49 cmd = ''.join (map (chr, getversioncommand))
50 s.write(cmd)
51 reply = s.read(16)
52 r = list(reply);
53 if checkreply(r, CMD_GETVERSION):
54 print r
55 return True
56 return False
57
58 def takephoto():
59 cmd = ''.join (map (chr, takephotocommand))
60 s.write(cmd)
61 reply = s.read(5)
62 r = list(reply);
63 if (checkreply(r, CMD_TAKEPHOTO) and r[3] == chr(0x0)):
64 return True
65 return False
66
67 def getbufferlength():
68 cmd = ''.join (map (chr, getbufflencommand))
69 s.write(cmd)
70 reply = s.read(9)
71 r = list(reply);
72 if (checkreply(r, CMD_GETBUFFLEN) and r[4] == chr(0x4)):
73 l = ord(r[5])
74 l <<= 8
75 l += ord(r[6])
76 l <<= 8
77 l += ord(r[7])
78 l <<= 8
79 l += ord(r[8])
80 return l
81
82 return 0
83
84 readphotocommand = [COMMANDSEND, SERIALNUM, CMD_READBUFF, 0x0c, FBUF_CURRENTFRAME, 0x0a]
85
86
87 def readbuffer(bytes):
88 addr = 0
89 photo = []
90
91 while (addr < bytes + 32):
92 command = readphotocommand + [(addr >> 24) & 0xFF, (addr >> 16) & 0xFF,
93 (addr >> 8) & 0xFF, addr & 0xFF]
94 command += [0, 0, 0, 32] # 32 bytes at a time
95 command += [1,0] # delay of 10ms
96 #print map(hex, command)
97 cmd = ''.join(map (chr, command))
98 s.write(cmd)
99 reply = s.read(32+5)
100 r = list(reply)
101 if (len(r) != 37):
102 continue
103 #print r
104 if (not checkreply(r, CMD_READBUFF)):
105 print "ERROR READING PHOTO"
106 return
107 photo += r[5:]
108 addr += 32
109 return photo
110
111
112
113
114 ######## main
115
116 s = serial.Serial(PORT, baudrate=BAUD, timeout=TIMEOUT)
117
118 reset()
119
120 if (not getversion()):
121 print "Camera not found"
122 exit
123 print "VC0706 Camera found"
124
125 if takephoto():
126 print "Snap!"
127
128 bytes = getbufferlength()
129
130 print bytes, "bytes to read"
131
132 photo = readbuffer(bytes)
133 #photo = readbuffer(5024)
134
135 f = open("photo.jpg", 'w')
136 #print photo
137 photodata = ''.join(photo)
138 f.write(photodata)
139 f.close()
140 #print length(photo)
Something went wrong with that request. Please try again.