SmartWheels self-driving race car. Designed for NXP Cup. Uses FRDM-KL25Z, area-scan camera, and simple image processing to detect and navigate any NXP spec track.

Dependencies:   TSI USBDevice mbed-dev

Fork of SmartWheels by haofan Zheng

Committer:
Bobymicjohn
Date:
Thu Apr 06 20:36:51 2017 +0000
Revision:
54:f1f5648dfacf
Parent:
50:c387c88141fb
Child:
56:7d3395ae022d
Adjusted the angle. Commented out all USB Server code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hazheng 28:271fc8445e89 1 #include "ArduCAM.h"
hazheng 28:271fc8445e89 2
hazheng 33:e3fcc4d6bb9b 3
hazheng 29:f87d8790f57d 4 #include "GlobalVariable.h"
hazheng 29:f87d8790f57d 5 #include "SWUSBServer.h"
hazheng 29:f87d8790f57d 6
hazheng 32:5badeff825dc 7 #include "CamRegBuf.h"
hazheng 32:5badeff825dc 8
hazheng 41:7b21c5e3599e 9 #include "ArduUTFT.h"
hazheng 41:7b21c5e3599e 10
hazheng 32:5badeff825dc 11 #include <string>
hazheng 32:5badeff825dc 12
hazheng 50:c387c88141fb 13 #define CAM_BLK_CAL_LEFT ((RESOLUTION_WIDTH / 2) - 2)
hazheng 50:c387c88141fb 14 #define CAM_BLK_CAL_RIGHT ((RESOLUTION_WIDTH / 2) + 2)
hazheng 44:15de535c4005 15
hazheng 46:a5eb9bd3bb55 16 //#define CAM_DISP_DEBUG
hazheng 48:f76b5e252444 17 //#define CAM_DISP_DEBUG_CENTER
hazheng 50:c387c88141fb 18 //#define CAM_DISP_IMG
hazheng 46:a5eb9bd3bb55 19
hazheng 46:a5eb9bd3bb55 20 #ifdef __cplusplus
hazheng 46:a5eb9bd3bb55 21 extern "C" {
hazheng 46:a5eb9bd3bb55 22 #endif
hazheng 44:15de535c4005 23
hazheng 46:a5eb9bd3bb55 24 static DigitalOut cam_cs(PIN_ACC_CS, 1);
hazheng 29:f87d8790f57d 25
hazheng 46:a5eb9bd3bb55 26 static uint8_t temp_mid_pos = RESOLUTION_WIDTH / 2;
hazheng 46:a5eb9bd3bb55 27 static uint8_t black_calibrate = 70;
hazheng 46:a5eb9bd3bb55 28 static volatile uint8_t centerLine[CAM_ROI_UPPER_LIMIT];
hazheng 46:a5eb9bd3bb55 29 static Thread * m_imgProcessThread = NULL;
hazheng 44:15de535c4005 30
hazheng 44:15de535c4005 31 void image_processing();
hazheng 44:15de535c4005 32 void cal_black_calibrate();
hazheng 34:f79db3bc2f86 33
hazheng 34:f79db3bc2f86 34 inline void ardu_cam_spi_write_8(int address, int value)
hazheng 29:f87d8790f57d 35 {
hazheng 34:f79db3bc2f86 36 // take the SS pin low to select the chip:
hazheng 34:f79db3bc2f86 37 cam_cs = 0;
hazheng 34:f79db3bc2f86 38 // send in the address and value via SPI:
hazheng 34:f79db3bc2f86 39 g_spi_port.write(address | 0x80);
hazheng 34:f79db3bc2f86 40 g_spi_port.write(value);
hazheng 34:f79db3bc2f86 41 // take the SS pin high to de-select the chip:
hazheng 34:f79db3bc2f86 42 cam_cs = 1;
hazheng 34:f79db3bc2f86 43 }
hazheng 34:f79db3bc2f86 44
hazheng 34:f79db3bc2f86 45 inline uint8_t ardu_cam_spi_read_8(int address)
hazheng 34:f79db3bc2f86 46 {
hazheng 34:f79db3bc2f86 47 // take the SS pin low to select the chip:
hazheng 29:f87d8790f57d 48 cam_cs = 0;
hazheng 34:f79db3bc2f86 49 // send in the address and value via SPI:
hazheng 34:f79db3bc2f86 50 g_spi_port.write(address & 0x7F);
hazheng 34:f79db3bc2f86 51 uint8_t value = static_cast<uint8_t>(g_spi_port.write(0x00));
hazheng 34:f79db3bc2f86 52 // take the SS pin high to de-select the chip:
hazheng 29:f87d8790f57d 53 cam_cs = 1;
hazheng 34:f79db3bc2f86 54 return value;
hazheng 34:f79db3bc2f86 55 }
hazheng 34:f79db3bc2f86 56 /*
hazheng 34:f79db3bc2f86 57 inline void ardu_cam_spi_set_burst()
hazheng 34:f79db3bc2f86 58 {
hazheng 34:f79db3bc2f86 59 cam_cs = 0;
hazheng 34:f79db3bc2f86 60
hazheng 34:f79db3bc2f86 61 g_spi_port.write(BURST_FIFO_READ & 0x7F);
hazheng 29:f87d8790f57d 62
hazheng 34:f79db3bc2f86 63 cam_cs = 1;
hazheng 34:f79db3bc2f86 64 }
hazheng 34:f79db3bc2f86 65
hazheng 34:f79db3bc2f86 66 inline uint16_t ardu_cam_spi_burst_read_16()
hazheng 34:f79db3bc2f86 67 {
hazheng 34:f79db3bc2f86 68 cam_cs = 0;
hazheng 29:f87d8790f57d 69
hazheng 34:f79db3bc2f86 70 uint16_t value = static_cast<uint16_t>(g_spi_port.write(0x00));
hazheng 34:f79db3bc2f86 71 value = (value << 8) & 0xFF00;
hazheng 34:f79db3bc2f86 72 value = value | static_cast<uint16_t>(g_spi_port.write(0x00));
hazheng 34:f79db3bc2f86 73
hazheng 34:f79db3bc2f86 74 cam_cs = 1;
hazheng 34:f79db3bc2f86 75 return value;
hazheng 29:f87d8790f57d 76 }
hazheng 29:f87d8790f57d 77 */
hazheng 29:f87d8790f57d 78
hazheng 29:f87d8790f57d 79
hazheng 28:271fc8445e89 80 bool ardu_cam_init()
hazheng 28:271fc8445e89 81 {
hazheng 32:5badeff825dc 82 char buf[20];
hazheng 32:5badeff825dc 83
hazheng 36:7e747e19f660 84 CamRegBuf * camReg = new CamRegBuf(g_core, CAM_SCCB_WRITE, CAM_SCCB_READ);
hazheng 36:7e747e19f660 85
hazheng 36:7e747e19f660 86 camReg->WriteRegSet(ResetProg);
hazheng 46:a5eb9bd3bb55 87 wait_ms(10);
hazheng 37:7074a6118d03 88 camReg->WriteRegSet(QVGA);
hazheng 44:15de535c4005 89 wait_ms(10);
hazheng 32:5badeff825dc 90
hazheng 36:7e747e19f660 91 #if defined(ARDUCAM_OV2640)
hazheng 32:5badeff825dc 92 camReg->SCCBWrite(0xff, 0x01);
hazheng 36:7e747e19f660 93 #endif
Bobymicjohn 54:f1f5648dfacf 94 //uint8_t camVer = camReg->SCCBRead(CAM_PID_ADDR);
Bobymicjohn 54:f1f5648dfacf 95 //sprintf(buf, "Cam VerH %#x", camVer);
Bobymicjohn 54:f1f5648dfacf 96 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 32:5badeff825dc 97
Bobymicjohn 54:f1f5648dfacf 98 //camVer = camReg->SCCBRead(CAM_VER_ADDR);
Bobymicjohn 54:f1f5648dfacf 99 //sprintf(buf, "Cam VerL %#x", camVer);
Bobymicjohn 54:f1f5648dfacf 100 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 36:7e747e19f660 101
hazheng 36:7e747e19f660 102
hazheng 32:5badeff825dc 103 delete camReg;
hazheng 32:5badeff825dc 104 camReg = NULL;
hazheng 32:5badeff825dc 105
hazheng 32:5badeff825dc 106
hazheng 32:5badeff825dc 107
hazheng 32:5badeff825dc 108
hazheng 34:f79db3bc2f86 109 uint8_t VerNum = ardu_cam_spi_read_8(0x40);
hazheng 34:f79db3bc2f86 110 VerNum = ardu_cam_spi_read_8(0x40);
hazheng 29:f87d8790f57d 111
hazheng 32:5badeff825dc 112
Bobymicjohn 54:f1f5648dfacf 113 //sprintf(buf, "Ardu Ver %#x", VerNum);
Bobymicjohn 54:f1f5648dfacf 114 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 29:f87d8790f57d 115
hazheng 34:f79db3bc2f86 116 ardu_cam_spi_write_8(ARDUCHIP_TEST1, ARDUCHIP_TEST_MSG);
hazheng 34:f79db3bc2f86 117 uint8_t testV = ardu_cam_spi_read_8(ARDUCHIP_TEST1);
hazheng 35:ac4fcca21560 118 if(VerNum != ARDUCHIP_VER_NUM || testV != ARDUCHIP_TEST_MSG)
hazheng 29:f87d8790f57d 119 {
Bobymicjohn 54:f1f5648dfacf 120 //g_core.GetUSBServer().PushReliableMsg('D', "CameraInit Fa");
hazheng 29:f87d8790f57d 121 return false;
hazheng 29:f87d8790f57d 122 }
Bobymicjohn 54:f1f5648dfacf 123 //g_core.GetUSBServer().PushReliableMsg('D', "CameraInit Su");
hazheng 29:f87d8790f57d 124
hazheng 35:ac4fcca21560 125 //ardu_cam_set_mode(MCU2LCD_MODE);
hazheng 43:0d1886f4848a 126 ardu_cam_spi_write_8(ARDUCHIP_CAP_CTRL, 0x00);
hazheng 44:15de535c4005 127 //ardu_cam_start_capture();
hazheng 29:f87d8790f57d 128
hazheng 28:271fc8445e89 129
hazheng 32:5badeff825dc 130 //unsigned char tempV = ardu_cam_read_reg(ARDUCHIP_MODE);
hazheng 32:5badeff825dc 131 //sprintf(buf, "Ardu Stat %#x", tempV);
hazheng 32:5badeff825dc 132 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 29:f87d8790f57d 133
hazheng 32:5badeff825dc 134 //tempV = ardu_cam_read_reg(ARDUCHIP_CAP_CTRL);
hazheng 32:5badeff825dc 135 //sprintf(buf, "Ardu FS1 %#x", tempV);
hazheng 32:5badeff825dc 136 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 44:15de535c4005 137 cal_black_calibrate();
hazheng 46:a5eb9bd3bb55 138 m_imgProcessThread = new Thread;
hazheng 46:a5eb9bd3bb55 139 m_imgProcessThread->start(callback(image_processing));
hazheng 29:f87d8790f57d 140
hazheng 29:f87d8790f57d 141 return true;
hazheng 29:f87d8790f57d 142 }
hazheng 29:f87d8790f57d 143
hazheng 29:f87d8790f57d 144 void ardu_cam_start_capture()
hazheng 29:f87d8790f57d 145 {
hazheng 34:f79db3bc2f86 146 ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_CLEAR_MASK);
hazheng 48:f76b5e252444 147
hazheng 34:f79db3bc2f86 148 ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_START_MASK);
hazheng 29:f87d8790f57d 149 }
hazheng 29:f87d8790f57d 150
hazheng 32:5badeff825dc 151 uint32_t ardu_cam_get_fifo_length()
hazheng 32:5badeff825dc 152 {
hazheng 32:5badeff825dc 153 uint32_t len1,len2,len3,length=0;
hazheng 34:f79db3bc2f86 154 len1 = ardu_cam_spi_read_8(FIFO_SIZE1);
hazheng 34:f79db3bc2f86 155 len2 = ardu_cam_spi_read_8(FIFO_SIZE2);
hazheng 34:f79db3bc2f86 156 len3 = ardu_cam_spi_read_8(FIFO_SIZE3) & 0x07;
hazheng 32:5badeff825dc 157 length = ((len3 << 16) | (len2 << 8) | len1) & 0x07ffff;
hazheng 32:5badeff825dc 158 return length;
hazheng 32:5badeff825dc 159 }
hazheng 32:5badeff825dc 160
hazheng 32:5badeff825dc 161 uint8_t ardu_cam_get_pixel()
hazheng 32:5badeff825dc 162 {
hazheng 34:f79db3bc2f86 163 uint16_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 34:f79db3bc2f86 164 uint16_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 34:f79db3bc2f86 165 //uint16_t VL = ardu_cam_spi_burst_read_16();
hazheng 32:5badeff825dc 166
hazheng 32:5badeff825dc 167 VL = (VL & 0x00FF) | ((VH << 8) & 0xFF00);
hazheng 33:e3fcc4d6bb9b 168 uint8_t ch = ((VL & 0xF800) >> 9);// << 2;
hazheng 33:e3fcc4d6bb9b 169 float pixel = (static_cast<float>(ch) * 0.21);
hazheng 32:5badeff825dc 170
hazheng 33:e3fcc4d6bb9b 171 ch = ((VL & 0x07E0) >> 3);// << 2;
hazheng 33:e3fcc4d6bb9b 172 pixel += (static_cast<float>(ch) * 0.72);
hazheng 32:5badeff825dc 173
hazheng 33:e3fcc4d6bb9b 174 ch = (VL & 0x001F) << 2;
hazheng 33:e3fcc4d6bb9b 175 pixel += (static_cast<float>(ch) * 0.07);
hazheng 32:5badeff825dc 176
hazheng 32:5badeff825dc 177 return static_cast<uint8_t>(pixel);
hazheng 32:5badeff825dc 178 }
hazheng 32:5badeff825dc 179
hazheng 32:5badeff825dc 180 void ardu_cam_print_debug()
hazheng 32:5badeff825dc 181 {
hazheng 32:5badeff825dc 182 uint32_t len = ardu_cam_get_fifo_length();
Bobymicjohn 54:f1f5648dfacf 183 //char buf[20];
Bobymicjohn 54:f1f5648dfacf 184 //sprintf(buf, "Cam L %#x", len);
Bobymicjohn 54:f1f5648dfacf 185 //g_core.GetUSBServer().PushReliableMsg('D', buf);
hazheng 32:5badeff825dc 186
hazheng 33:e3fcc4d6bb9b 187 //if(len < (RESOLUTION_HEIGHT * RESOLUTION_WIDTH * 2))
hazheng 33:e3fcc4d6bb9b 188 // return;
hazheng 33:e3fcc4d6bb9b 189
hazheng 33:e3fcc4d6bb9b 190 //Begin output the picture
hazheng 32:5badeff825dc 191 std::string lineBuf;
hazheng 37:7074a6118d03 192 #if defined(MANUAL_REDUCE_RESULOTION_BY2)
hazheng 37:7074a6118d03 193 lineBuf.resize((RESOLUTION_WIDTH / 2) + 1);
hazheng 37:7074a6118d03 194 #else
hazheng 33:e3fcc4d6bb9b 195 lineBuf.resize(RESOLUTION_WIDTH + 1);
hazheng 37:7074a6118d03 196 #endif
hazheng 32:5badeff825dc 197
hazheng 34:f79db3bc2f86 198 //ardu_cam_spi_set_burst();
hazheng 41:7b21c5e3599e 199
hazheng 42:c4e1606087ff 200 //ardu_cam_get_pixel(); //Get the first dummy pixel
hazheng 41:7b21c5e3599e 201
hazheng 43:0d1886f4848a 202
hazheng 33:e3fcc4d6bb9b 203 for (uint8_t i = 0; i < RESOLUTION_HEIGHT; ++i)
hazheng 32:5badeff825dc 204 {
hazheng 37:7074a6118d03 205 #if defined(MANUAL_REDUCE_RESULOTION_BY2)
hazheng 37:7074a6118d03 206 lineBuf[0] = i / 2;
hazheng 37:7074a6118d03 207
hazheng 37:7074a6118d03 208 for (int j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 37:7074a6118d03 209 {
hazheng 37:7074a6118d03 210 if(i % 2 == 0 || j % 2 == 0)
hazheng 37:7074a6118d03 211 {
hazheng 37:7074a6118d03 212 ardu_cam_get_pixel();
hazheng 37:7074a6118d03 213 }
hazheng 37:7074a6118d03 214 else
hazheng 37:7074a6118d03 215 {
hazheng 37:7074a6118d03 216 lineBuf[(j / 2) + 1] = ardu_cam_get_pixel();
hazheng 37:7074a6118d03 217 }
hazheng 37:7074a6118d03 218 }
hazheng 37:7074a6118d03 219
hazheng 37:7074a6118d03 220 if(i % 2 == 0)
hazheng 37:7074a6118d03 221 {
hazheng 37:7074a6118d03 222
hazheng 37:7074a6118d03 223 }
hazheng 37:7074a6118d03 224 else
hazheng 37:7074a6118d03 225 {
hazheng 37:7074a6118d03 226 g_core.GetUSBServer().PushReliableMsg('P', lineBuf);
hazheng 37:7074a6118d03 227 wait(0.35);
hazheng 37:7074a6118d03 228 }
hazheng 37:7074a6118d03 229
hazheng 37:7074a6118d03 230 #else
hazheng 37:7074a6118d03 231
hazheng 32:5badeff825dc 232 lineBuf[0] = i;
hazheng 32:5badeff825dc 233
hazheng 33:e3fcc4d6bb9b 234 for (int j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 32:5badeff825dc 235 {
hazheng 33:e3fcc4d6bb9b 236 //uint8_t p = ardu_cam_get_pixel();
hazheng 33:e3fcc4d6bb9b 237 lineBuf[j + 1] = ardu_cam_get_pixel();
hazheng 32:5badeff825dc 238 }
hazheng 32:5badeff825dc 239
Bobymicjohn 54:f1f5648dfacf 240 //g_core.GetUSBServer().PushReliableMsg('P', lineBuf);
hazheng 35:ac4fcca21560 241 wait(0.35);
hazheng 37:7074a6118d03 242
hazheng 37:7074a6118d03 243 #endif
hazheng 37:7074a6118d03 244
hazheng 32:5badeff825dc 245 }
hazheng 43:0d1886f4848a 246
hazheng 43:0d1886f4848a 247 }
hazheng 43:0d1886f4848a 248
hazheng 44:15de535c4005 249 uint8_t ardu_cam_is_capture_finished()
hazheng 44:15de535c4005 250 {
hazheng 44:15de535c4005 251 return (ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK);
hazheng 44:15de535c4005 252 }
hazheng 44:15de535c4005 253
hazheng 44:15de535c4005 254 void cal_black_calibrate()
hazheng 44:15de535c4005 255 {
hazheng 44:15de535c4005 256 ardu_cam_start_capture();
hazheng 44:15de535c4005 257
hazheng 44:15de535c4005 258 while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK));
hazheng 44:15de535c4005 259
hazheng 44:15de535c4005 260 float temp = 0.0f;
hazheng 44:15de535c4005 261 static uint16_t pixel = 0x00;
hazheng 44:15de535c4005 262 for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 44:15de535c4005 263 {
hazheng 44:15de535c4005 264 pixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8;
hazheng 44:15de535c4005 265 pixel = pixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 46:a5eb9bd3bb55 266
hazheng 44:15de535c4005 267 if(CAM_BLK_CAL_LEFT < j && j <= CAM_BLK_CAL_RIGHT)
hazheng 44:15de535c4005 268 {
hazheng 44:15de535c4005 269 temp += static_cast<uint8_t>((pixel >> 3) & 0x00FF);
hazheng 44:15de535c4005 270 }
hazheng 44:15de535c4005 271 }
hazheng 44:15de535c4005 272 temp = temp / (CAM_BLK_CAL_RIGHT - CAM_BLK_CAL_LEFT);
hazheng 44:15de535c4005 273 black_calibrate = temp * 0.7f;
hazheng 44:15de535c4005 274 }
hazheng 44:15de535c4005 275
hazheng 44:15de535c4005 276 inline void get_img_row_info(const uint8_t display, const uint8_t rowI, uint8_t * left, uint8_t * right)
hazheng 44:15de535c4005 277 {
hazheng 44:15de535c4005 278 *left = 0;
hazheng 44:15de535c4005 279 *right = RESOLUTION_WIDTH;
hazheng 44:15de535c4005 280 uint8_t isRightFound = 0;
hazheng 44:15de535c4005 281 /*
hazheng 44:15de535c4005 282 #ifdef IMAGE_DISPLAY_TO_SCREEN
hazheng 44:15de535c4005 283 if(display)
hazheng 44:15de535c4005 284 {
hazheng 44:15de535c4005 285 ardu_utft_set_camimg_row(CAM_ROI_UPPER_LIMIT - rowI);
hazheng 44:15de535c4005 286 }
hazheng 44:15de535c4005 287 #endif
hazheng 44:15de535c4005 288 */
hazheng 44:15de535c4005 289 static uint16_t pixel = 0x0000;
hazheng 44:15de535c4005 290 static uint8_t pGreen = 0x00;
hazheng 44:15de535c4005 291
hazheng 44:15de535c4005 292 for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 44:15de535c4005 293 {
hazheng 44:15de535c4005 294 pixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8;
hazheng 44:15de535c4005 295 pixel = pixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 46:a5eb9bd3bb55 296
hazheng 44:15de535c4005 297 pGreen = static_cast<uint8_t>((pixel >> 3) & 0x00FF);
hazheng 44:15de535c4005 298 if((pGreen < black_calibrate))
hazheng 44:15de535c4005 299 {
hazheng 44:15de535c4005 300 if(j < temp_mid_pos)
hazheng 44:15de535c4005 301 {
hazheng 44:15de535c4005 302 *left = j;
hazheng 46:a5eb9bd3bb55 303 #ifdef CAM_DISP_DEBUG
hazheng 46:a5eb9bd3bb55 304 ardu_utft_write_DATA(0xF8, 0x00);
hazheng 46:a5eb9bd3bb55 305 #endif
hazheng 44:15de535c4005 306 }
hazheng 44:15de535c4005 307 else if(!isRightFound)
hazheng 44:15de535c4005 308 {
hazheng 44:15de535c4005 309 *right = j;
hazheng 44:15de535c4005 310 isRightFound = 1;
hazheng 46:a5eb9bd3bb55 311 #ifdef CAM_DISP_DEBUG
hazheng 46:a5eb9bd3bb55 312 ardu_utft_write_DATA(0xF8, 0x00);
hazheng 46:a5eb9bd3bb55 313 #endif
hazheng 44:15de535c4005 314 }
hazheng 44:15de535c4005 315 }
hazheng 46:a5eb9bd3bb55 316 #ifdef CAM_DISP_DEBUG
hazheng 46:a5eb9bd3bb55 317 else
hazheng 46:a5eb9bd3bb55 318 {
hazheng 46:a5eb9bd3bb55 319 ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel));
hazheng 46:a5eb9bd3bb55 320 }
hazheng 50:c387c88141fb 321
hazheng 46:a5eb9bd3bb55 322 #elif defined(CAM_DISP_IMG)
hazheng 46:a5eb9bd3bb55 323 ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel));
hazheng 46:a5eb9bd3bb55 324 #endif
hazheng 44:15de535c4005 325 }
hazheng 44:15de535c4005 326 }
hazheng 44:15de535c4005 327
hazheng 44:15de535c4005 328 volatile const uint8_t* ardu_cam_get_center_array()
hazheng 44:15de535c4005 329 {
hazheng 44:15de535c4005 330 return centerLine;
hazheng 44:15de535c4005 331 }
hazheng 44:15de535c4005 332
hazheng 44:15de535c4005 333 void image_processing()
hazheng 44:15de535c4005 334 {
hazheng 44:15de535c4005 335 while(true)
hazheng 44:15de535c4005 336 {
hazheng 44:15de535c4005 337 ardu_cam_start_capture();
hazheng 44:15de535c4005 338
hazheng 44:15de535c4005 339 while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK));
hazheng 44:15de535c4005 340
hazheng 44:15de535c4005 341 temp_mid_pos = RESOLUTION_WIDTH / 2;
hazheng 44:15de535c4005 342 //cal_black_calibrate();
hazheng 44:15de535c4005 343 uint8_t leftPos = 0;
hazheng 44:15de535c4005 344 uint8_t rightPos = 0;
hazheng 44:15de535c4005 345 for (uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i)
hazheng 44:15de535c4005 346 {
hazheng 46:a5eb9bd3bb55 347 #if defined(CAM_DISP_IMG) or defined(CAM_DISP_DEBUG)
hazheng 46:a5eb9bd3bb55 348 ardu_utft_set_camimg_row(CAM_ROI_UPPER_LIMIT - i);
hazheng 46:a5eb9bd3bb55 349 #endif
hazheng 44:15de535c4005 350 get_img_row_info(0, i, &leftPos, &rightPos);
hazheng 44:15de535c4005 351 temp_mid_pos = (leftPos + rightPos) / 2;
hazheng 46:a5eb9bd3bb55 352
hazheng 46:a5eb9bd3bb55 353 #ifdef CAM_DISP_DEBUG_CENTER
hazheng 44:15de535c4005 354 ardu_utft_set_camimg_rowcol(CAM_ROI_UPPER_LIMIT - i, temp_mid_pos);
hazheng 44:15de535c4005 355 ardu_utft_write_DATA(0xF8, 0x00);
hazheng 44:15de535c4005 356 #endif
hazheng 46:a5eb9bd3bb55 357
hazheng 44:15de535c4005 358 centerLine[CAM_ROI_UPPER_LIMIT - i - 1] = temp_mid_pos;
hazheng 44:15de535c4005 359 }
hazheng 44:15de535c4005 360 }
hazheng 44:15de535c4005 361 }
hazheng 46:a5eb9bd3bb55 362
hazheng 46:a5eb9bd3bb55 363
hazheng 43:0d1886f4848a 364 void ardu_cam_display_img_utft()
hazheng 43:0d1886f4848a 365 {
hazheng 43:0d1886f4848a 366 ardu_cam_start_capture();
hazheng 44:15de535c4005 367
hazheng 44:15de535c4005 368 while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK));
hazheng 44:15de535c4005 369
hazheng 44:15de535c4005 370 temp_mid_pos = RESOLUTION_WIDTH / 2;
hazheng 46:a5eb9bd3bb55 371
hazheng 44:15de535c4005 372 for (uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i)
hazheng 43:0d1886f4848a 373 {
hazheng 46:a5eb9bd3bb55 374 ardu_utft_set_camimg_row(CAM_ROI_UPPER_LIMIT - i);
hazheng 46:a5eb9bd3bb55 375 for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j)
hazheng 46:a5eb9bd3bb55 376 {
hazheng 46:a5eb9bd3bb55 377 uint8_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 46:a5eb9bd3bb55 378 uint8_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ);
hazheng 46:a5eb9bd3bb55 379
hazheng 46:a5eb9bd3bb55 380 ardu_utft_write_DATA(VH, VL);
hazheng 46:a5eb9bd3bb55 381 }
hazheng 44:15de535c4005 382
hazheng 43:0d1886f4848a 383 }
hazheng 44:15de535c4005 384 }
hazheng 46:a5eb9bd3bb55 385
hazheng 46:a5eb9bd3bb55 386 #ifdef __cplusplus
hazheng 46:a5eb9bd3bb55 387 }
hazheng 46:a5eb9bd3bb55 388 #endif
hazheng 46:a5eb9bd3bb55 389
hazheng 46:a5eb9bd3bb55 390
hazheng 46:a5eb9bd3bb55 391