-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.ino
184 lines (155 loc) · 3.12 KB
/
camera.ino
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
int processCMDTAmsg(void)
{
int i, j, k, IntegerPart;
char tmp[3];
int32_t pic=0;
// $TMHKR,C,,,,*47
// COM
// 1
IntegerPart = 1;
for (i=0, j=0, k=0; (i<MSGindex) && (j<10); i++) // We start at 7 so we ignore the '$GPGGA,'
{
if (bus_msg[i] == ',')
{
j++; // Segment index
k=0; // Index into target variable
IntegerPart = 1;
}
else
{
if (j == 1)
{
tmp[k] = bus_msg[i];
k++;
}
}
}
if( tmp[0] == 'S')
{
//SICL.println("save");
pic = savePISD_CSd();
DEBUG.println(F(""));
//TODO change to switch case
if(pic == 0)
{
DEBUG.println(F("Success"));
return 0;
}
else if(pic == -1)
{
DEBUG.println(F("Data overflow"));
}
else if(pic == -2)
{
DEBUG.println(F("CAM timeout"));
}
else if(pic == -3)
{
DEBUG.println(F("SD Error"));
}
return 0;
}
if( tmp[0] == 'E')
{
SICL.println(F("$TMCAM,,END*47"));
return 0;
}
}
int32_t savePISD_CSd(void)
{
char str[10];
byte buf[256];
static int i = 0;
static int k = 0;
uint8_t temp = 0,temp_last=0;
bool is_data=false;
volatile int cam_wtchdg=0;
int nowtime=0;
if( !sdcard_is_card_present())
{
return -3; //TODO error codes
}
//itoa(picnum, str, 10);
str[0]='\0';
strcat(str, GPS_time[GPS_valid]);
strcat(str, ".jpg");
//Open the new dataFile
//dataFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
if(!dataFile.open(str, O_RDWR | O_CREAT ))
{
DEBUG.println(F("open dataFile faild"));
dataFile.close();
return -3;
}
//picnum++;
i = 0;
cam_wtchdg = millis();
while(!SICL.available())
{
nowtime=millis();
if(nowtime - cam_wtchdg >6000)
{
dataFile.close();
return -2;
}
}
/* while (SICL.available() > 0)
{
temp = SICL.read();
}*/
temp=0;
//Read JPEG data from FIFO
cam_wtchdg = millis();
while ( (temp !=0xD9) | (temp_last !=0xFF))
{
nowtime=millis();
if(nowtime - cam_wtchdg >3000)
{
dataFile.close();
return -2;
}
if (SICL.available() > 0)
{
cam_wtchdg = millis();
temp_last = temp;
temp = SICL.read(); //Write image data to buffer if not full
if( i < 256)
buf[i++] = temp;
else{
//Write 256 bytes image data to dataFile
dataFile.write(buf ,256);
i = 0;
//timeout in case of corrupt dadaflow; picture data larger than 80kB -> must be tested and calibrated
if(k>320)
{
dataFile.close(); //close dataFile
return -1;
}
buf[i++] = temp;
}
SICL.print(F("o"));
delay(0);
}
}
//Write the remain bytes in the buffer
if(i > 0){
dataFile.write(buf,i);
}
//Close the dataFile
dataFile.close();
return 0;
}
void getPICuart()
{
int timer=0;
int nowtime=0;
int getmsg=10;
//SICL.listen();
//SICL.println("d");
setBusBusy();
delay(500);
SICL.println(F("$TMCAM,1,CAP*47"));
sw_timer_enable_channel(BUS_TIMER);
bus_sm(BUS_PROCESS);
// SICL.print(".");
}