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