-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
executable file
·127 lines (93 loc) · 3.78 KB
/
serial.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "cambadge.h"
#include "globals.h"
// serial control - mostly a remnant from the old serial camera board, some parts may not entirely work.
// useful for messing with camera & OLED commands etc. as well as getting live image data
// for image data you will probably want to crank up the baudrate to 2Mbaud or higher
#if serialcontrol==1
unsigned int camoptions;
void polluart(void) { // check for serial commands. Slightly crude but simple
unsigned int i, pchange, x, y, outptr;
unsigned char c;
if (tick) {
if (rxptr) if (++rxtimer > 2) rxptr = 0; // serial receive timeout
if (U2STAbits.OERR) {
U2STAbits.OERR = 0;
rxptr = 0;
} // UART overrun
if (U2STAbits.FERR) {
U2STAbits.FERR = 0;
rxptr = 0;
} // Framing error
}
if (rxptr == 0) return;
switch (rxbuf[0]) {
case 0x10:// 0x10 <adr> <value> set camera register. returns 0x55
if (rxptr != 3) break;
rxptr = 0;
cam_setreg(rxbuf[1], rxbuf[2]);
u2txbyte(0x55);
break;
case 0x11: // 0x11 <rr> read camera register. returns 1 byte register value
if (rxptr != 2) break;
rxptr = 0;
u2txbyte(readcamreg(rxbuf[1]));
break;
case 0x13: // 0x12 <en> <graben> camera enable/disable
if (rxptr != 3) break;
rxptr=0;
cam_enable(rxbuf[1]);
cam_grabenable(rxbuf[2],0,0);
u2txbyte(0x55);
break;
case 0x14: // 14 xstart/2 ystart/2 xdiv ydiv refclkdiv set capture parameters 0 = unchanged
if (rxptr != 6) break;
rxptr = 0;
if (rxbuf[1]) xstart = (unsigned int) rxbuf[1]*2;
if (rxbuf[2])ystart = (unsigned int) rxbuf[2]*2;
if (rxbuf[3])xdiv = (unsigned int) rxbuf[3];
if (rxbuf[4])ydiv = (unsigned int) rxbuf[4];
if (rxbuf[5]) REFOCON = 0b1001001000000000 | ((unsigned int) rxbuf[5]) << 16; // XCLK clock to cam
u2txbyte(0x55);
break;
case 0x15 : // 15 poll camera state
if(rxptr!=1) break;
rxptr=0;
u2txbyte(cam_newframe?1:0 | cam_busy?2:0 | cam_wrap?4:0 | cam_started?8:0);
u2txword(linecnt);
u2txword(camaddr);
break;
case 0x16 : // 16 xx return image. xx=1 to wait for new frame
// could be optimised by starting to send while capture in progress
if(rxptr!=2) break;
rxptr=0;
if(rxbuf[1]) while(!cam_newframe);
for(x=1,i=0;i!=xpixels*ypixels*(camflags & camopt_mono)?1:2;i++) {
kickwatchdog;
u2txbyte(cambuffer[x++]);
}
cam_newframe=0;
break;
case 0x20: // return info
if(rxptr!=1) break;
rxptr=0;
u2txbyte(versionbyte);
u2txbyte(clockfreq / 1000000);
u2txbyte(cambufsize >> 16);
u2txbyte(cambufsize >> 8);
u2txbyte(cambufsize & 0xff);
rxptr = 0;
break;
case 0x30: // 30 return accel data, bat level, bat state
if(rxptr!=1) break;
rxptr=0;
u2txword(accx);
u2txword(accy);
u2txword(accz);
u2txbyte(butstate);
u2txbyte(butpress);
u2txword(battlevel);
break;
}//switch
if (rxptr == 0) powerdowntimer = 0; // reset timer on good command
}
#endif