UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
camera.c
Go to the documentation of this file.
1 
11 #include "board.h"
12 #include "../uart.h"
13 #include "../rtc.h"
14 #include "../delay.h"
15 
16 #include "../camera.h"
17 
18 //LK_POWERUP[] = {0x0a, 0x49, 0x6e, 0x69, 0x74, 0x20, 0x65, 0x6e, 0x64, 0x0d, 0x0a};
19 //static char LK_POWERUP[] = {0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x49, 0x6e, 0x69, 0x74, 0x20, 0x65, 0x6e, 0x64, 0x0d, 0x0a}; // Init end
20 //LK_POWERUP[] = {0x0d,0x0a,0,4,0,0x49,0x68,0x69,0x68,0x20,0x65,0xc,0};
21 
22 //static char LK_BAUDRATE_19200[] = {0x56, 0x00, 0x24, 0x03, 0x01, 0x56, 0xe4};
23 //static char LK_BAUDRATE_38400[] = {0x56, 0x00, 0x24, 0x03, 0x01, 0x2a, 0xf2};
24 //static char LK_BAUDRATE_RE[] = {0x76, 0x00, 0x24, 0x00, 0x00};
25 
26 //static char LK_RESOLUTION_VGA[] = {0x56, 0x00, 0x54, 0x01, 0x00};
27 //static char LK_RESOLUTION_800[] = {0x56, 0x00, 0x54, 0x01, 0x1D};
28 //static char LK_RESOLUTION_1280[] = {0x56, 0x00, 0x54, 0x01, 0x1B};
29 static char LK_RESOLUTION_1600[] = {0x56, 0x00, 0x54, 0x01, 0x21};
30 static char LK_RESOLUTION_RE[] = {0x76, 0x00, 0x54, 0x00, 0x00}; // v T
31 
32 static char LK_COMPRESSION[] = {0x56, 0x00, 0x31, 0x05, 0x01, 0x01, 0x12, 0x04, 0x00}; // Value in last byte
33 static char LK_COMPRESSION_RE[] = {0x76, 0x00, 0x31, 0x00, 0x00}; // v 1
34 
35 static char LK_RESET[] = {0x56, 0x00, 0x26, 0x00};
36 // OLD: LK_RESET_RE[] = a2s([0x0d, 0x0a, 0x49, 0x6e, 0x69, 0x74, 0x20, 0x65, 0x6e, 0x64, 0x0d, 0x0a])
37 // LEGIT: LK_RESET_RE[] = a2s([0x76,0x00,0x31,0x00,0x00])
38 static char LK_RESET_RE[] = {0x0d, 0x0a, 0x49, 0x6e, 0x69, 0x74, 0x20, 0x65, 0x6e, 0x64, 0x0d, 0x0a};
39 
40 static char LK_PICTURE[] = {0x56, 0x00, 0x36, 0x01, 0x00};
41 static char LK_PICTURE_RE[] = {0x76, 0x00, 0x36, 0x00, 0x00};
42 static char LK_JPEGSIZE[] = {0x56, 0x00, 0x34, 0x01, 0x00};
43 static char LK_JPEGSIZE_RE[] = {0x76, 0x00, 0x34, 0x00, 0x04}; //, 0x00, 0x00, 0x00, 0x00}; // then XH XL // assumption is last 4 bytes are size
44 //static char LK_STOP[] = {0x56, 0x00, 0x36, 0x01, 0x03};
45 //static char LK_STOP_RE[] = {0x76, 0x00, 0x36, 0x00, 0x00};
46 
47 static char LK_READPICTURE[] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
48 static char LK_PICTURE_TIME_dot1ms[] = {0x00, 0x0A}; // .1 ms
49 static char LK_READPICTURE_RE[] = {0x76, 0x00, 0x32, 0x00, 0x00};
50 //static char JPEG_START[] = {0xFF, 0xD8};
51 static char JPEG_END[] = {0xFF, 0xD9};
52 
53 #define CAMWRITE(a) UART_putb(UART_CAMERA,a,sizeof(a)); // send this message
54 
55 static bool UART_waitmatch(char *string, uint32_t string_length, uint32_t timeout)
56 {
57  char c;
58  uint32_t index;
59  uint64_t start_timestamp;
60  RTC_getTime_ms(&start_timestamp);
61 
62  index = 0;
63  while(!RTC_timerElapsed_ms(start_timestamp, timeout))
64  {
65  if(UART_getc_nonblocking(UART_CAMERA, &c))
66  {
67  if(c == string[index])
68  {
69  index++;
70  if(index == string_length)
71  {
72  return true;
73  }
74  }
75  else
76  {
77  index = 0;
78  }
79  }
80  }
81  return false;
82 }
83 
84 static uint32_t UART_getw4(uint8_t serial)
85 {
86  char c1=UART_getc(serial),c2=UART_getc(serial),c3=UART_getc(serial),c4=UART_getc(serial);
87  return (uint32_t)((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4 << 0));
88 }
89 
90 static bool Camera_command(char *command, uint32_t command_length, char *response, uint32_t response_length)
91 {
92  uint32_t attempts = 1;
93 
94  UART_putb(UART_CAMERA,command,command_length);
95  while(!UART_waitmatch(response, response_length, 3000))
96  {
97  if(--attempts == 0)
98  {
99  return false;
100  }
101  Delay_ms(1000);
102  UART_putb(UART_CAMERA,command,command_length);
103  }
104  return true;
105 }
106 
107 bool Camera_capture(uint32_t page_size, void (*page_store)(uint8_t*,uint32_t))
108 {
109  uint32_t jpegsize, i=0, j=0, endfoundcount = 0;
110  uint8_t page_buffer[page_size];
111 
112  // initialise camera
113  if(!Camera_command(LK_RESET, sizeof(LK_RESET), LK_RESET_RE, sizeof(LK_RESET_RE)))
114  {
115  return false;
116  }
117 
118  Delay_ms(2000); // 2-3 sec gap required by data sheet before camera ready
119 
120  // set resolution
121  if(!Camera_command(LK_RESOLUTION_1600, sizeof(LK_RESOLUTION_1600), LK_RESOLUTION_RE, sizeof(LK_RESOLUTION_RE)))
122  {
123  return false;
124  }
125 
126  // set compression
127  LK_COMPRESSION[8] = 0x10;
128  if(!Camera_command(LK_COMPRESSION, sizeof(LK_COMPRESSION), LK_COMPRESSION_RE, sizeof(LK_COMPRESSION_RE)))
129  {
130  return false;
131  }
132 
133  // take picture
134  if(!Camera_command(LK_PICTURE, sizeof(LK_PICTURE), LK_PICTURE_RE, sizeof(LK_PICTURE_RE)))
135  {
136  return false;
137  }
138 
139  // read size
140  if(!Camera_command(LK_JPEGSIZE, sizeof(LK_JPEGSIZE), LK_JPEGSIZE_RE, sizeof(LK_JPEGSIZE_RE)))
141  {
142  return false;
143  }
144  jpegsize = UART_getw4(UART_CAMERA); // file size (lowest 32 bits)
145 
146  // offset in file start (0 here)
147  LK_READPICTURE[6] = 0x00;
148  LK_READPICTURE[7] = 0x00;
149 
150  LK_READPICTURE[8] = 0x00;
151  LK_READPICTURE[9] = 0x00;
152 
153  // write length to obtain +8 bytes?
154  LK_READPICTURE[10] = (char)((jpegsize >> 24) & 0xFF);
155  LK_READPICTURE[11] = (char)((jpegsize >> 16) & 0xFF);
156  LK_READPICTURE[12] = (char)((jpegsize >> 8) & 0xFF);
157  LK_READPICTURE[13] = (char)(jpegsize & 0xFF);
158 
159  // 0.1ms
160  LK_READPICTURE[14] = LK_PICTURE_TIME_dot1ms[0];
161  LK_READPICTURE[15] = LK_PICTURE_TIME_dot1ms[1];
162 
163  if(!Camera_command(LK_READPICTURE, sizeof(LK_READPICTURE), LK_READPICTURE_RE, sizeof(LK_READPICTURE_RE)))
164  {
165  return false;
166  }
167 
168  for(i=0; i<jpegsize && endfoundcount<2;i++)
169  {
170  page_buffer[j] = UART_getc(UART_CAMERA);
171 
172  if(page_buffer[j] == JPEG_END[endfoundcount])
173  {
174  endfoundcount+=1;
175  }
176  else
177  {
178  endfoundcount=0;
179  }
180 
181  j++;
182  if(j==page_size || endfoundcount==2)
183  {
184  page_store(page_buffer, j);
185  j=0;
186  }
187  }
188 
189  return true;
190 }
void Delay_ms(uint32_t milliseconds)
Definition: delay.c:19
bool Camera_capture(uint32_t page_size, void(*page_store)(uint8_t *, uint32_t))
Definition: camera.c:107
bool UART_getc_nonblocking(uint8_t uart_num, char *c)
Definition: uart.c:145
char UART_getc(uint8_t uart_num)
Definition: uart.c:113
bool RTC_timerElapsed_ms(uint64_t start_time_ms, uint64_t period_ms)
Definition: rtc.c:53
void RTC_getTime_ms(uint64_t *time_ms)
Definition: rtc.c:33
void UART_putb(uint8_t uart_num, char *str, uint32_t len)
Definition: uart.c:137