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@97:0ed9ede9a995, 2017-04-19 (annotated)
- Committer:
- Bobymicjohn
- Date:
- Wed Apr 19 21:17:40 2017 +0000
- Revision:
- 97:0ed9ede9a995
- Parent:
- 96:ec89c4d1383d
- Child:
- 99:c6665262fd3d
Added Testing State.
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 | 57:0d8a155d511d | 3 | //#define SW_DEBUG |
hazheng | 29:f87d8790f57d | 4 | #include "GlobalVariable.h" |
hazheng | 56:7d3395ae022d | 5 | #include "SWCommon.h" |
hazheng | 56:7d3395ae022d | 6 | |
hazheng | 29:f87d8790f57d | 7 | |
hazheng | 32:5badeff825dc | 8 | #include "CamRegBuf.h" |
hazheng | 32:5badeff825dc | 9 | |
hazheng | 41:7b21c5e3599e | 10 | #include "ArduUTFT.h" |
hazheng | 41:7b21c5e3599e | 11 | |
hazheng | 32:5badeff825dc | 12 | #include <string> |
hazheng | 32:5badeff825dc | 13 | |
hazheng | 57:0d8a155d511d | 14 | #define CAM_BLK_CAL_ACTIVE |
hazheng | 44:15de535c4005 | 15 | |
hazheng | 46:a5eb9bd3bb55 | 16 | //#define CAM_DISP_DEBUG |
hazheng | 81:32bd7a25a699 | 17 | //#define CAM_DISP_DEBUG_CENTER |
hazheng | 81:32bd7a25a699 | 18 | //#define CAM_DISP_IMG |
hazheng | 46:a5eb9bd3bb55 | 19 | |
hazheng | 57:0d8a155d511d | 20 | #define IMG_PROC_SIGNAL 0xBB |
hazheng | 57:0d8a155d511d | 21 | |
hazheng | 46:a5eb9bd3bb55 | 22 | #ifdef __cplusplus |
hazheng | 46:a5eb9bd3bb55 | 23 | extern "C" { |
hazheng | 46:a5eb9bd3bb55 | 24 | #endif |
hazheng | 44:15de535c4005 | 25 | |
hazheng | 57:0d8a155d511d | 26 | const static uint8_t CAM_BLK_CAL_LEFT = ((RESOLUTION_WIDTH / 2) - 1); |
hazheng | 57:0d8a155d511d | 27 | const static uint8_t CAM_BLK_CAL_RIGHT = ((RESOLUTION_WIDTH / 2) + 1); |
hazheng | 70:311d32a596db | 28 | const static uint8_t TERMINATE_WIDTH = static_cast<uint8_t>(RESOLUTION_WIDTH * 0.28f); |
hazheng | 57:0d8a155d511d | 29 | |
hazheng | 46:a5eb9bd3bb55 | 30 | static DigitalOut cam_cs(PIN_ACC_CS, 1); |
hazheng | 29:f87d8790f57d | 31 | |
hazheng | 46:a5eb9bd3bb55 | 32 | static uint8_t temp_mid_pos = RESOLUTION_WIDTH / 2; |
hazheng | 46:a5eb9bd3bb55 | 33 | static uint8_t black_calibrate = 70; |
hazheng | 65:295c222fdf88 | 34 | static volatile uint8_t centerLine[CAM_ROI_UPPER_LIMIT * 2]; |
hazheng | 64:43ab429a37e0 | 35 | static volatile uint8_t is_encounter_terminate = 0; |
hazheng | 79:bdbac82c979b | 36 | static volatile uint8_t is_border_find = BOTH_NOT_FOUND; |
hazheng | 44:15de535c4005 | 37 | |
hazheng | 44:15de535c4005 | 38 | void cal_black_calibrate(); |
hazheng | 34:f79db3bc2f86 | 39 | |
hazheng | 34:f79db3bc2f86 | 40 | inline void ardu_cam_spi_write_8(int address, int value) |
hazheng | 29:f87d8790f57d | 41 | { |
hazheng | 34:f79db3bc2f86 | 42 | // take the SS pin low to select the chip: |
hazheng | 34:f79db3bc2f86 | 43 | cam_cs = 0; |
hazheng | 34:f79db3bc2f86 | 44 | // send in the address and value via SPI: |
hazheng | 34:f79db3bc2f86 | 45 | g_spi_port.write(address | 0x80); |
hazheng | 34:f79db3bc2f86 | 46 | g_spi_port.write(value); |
hazheng | 34:f79db3bc2f86 | 47 | // take the SS pin high to de-select the chip: |
hazheng | 34:f79db3bc2f86 | 48 | cam_cs = 1; |
hazheng | 34:f79db3bc2f86 | 49 | } |
hazheng | 34:f79db3bc2f86 | 50 | |
hazheng | 34:f79db3bc2f86 | 51 | inline uint8_t ardu_cam_spi_read_8(int address) |
hazheng | 34:f79db3bc2f86 | 52 | { |
hazheng | 34:f79db3bc2f86 | 53 | // take the SS pin low to select the chip: |
hazheng | 29:f87d8790f57d | 54 | cam_cs = 0; |
hazheng | 34:f79db3bc2f86 | 55 | // send in the address and value via SPI: |
hazheng | 34:f79db3bc2f86 | 56 | g_spi_port.write(address & 0x7F); |
hazheng | 34:f79db3bc2f86 | 57 | uint8_t value = static_cast<uint8_t>(g_spi_port.write(0x00)); |
hazheng | 34:f79db3bc2f86 | 58 | // take the SS pin high to de-select the chip: |
hazheng | 29:f87d8790f57d | 59 | cam_cs = 1; |
Bobymicjohn | 97:0ed9ede9a995 | 60 | |
hazheng | 34:f79db3bc2f86 | 61 | return value; |
hazheng | 34:f79db3bc2f86 | 62 | } |
hazheng | 29:f87d8790f57d | 63 | |
hazheng | 28:271fc8445e89 | 64 | bool ardu_cam_init() |
hazheng | 28:271fc8445e89 | 65 | { |
hazheng | 87:15fcf7891bf9 | 66 | CamRegBuf * camReg = new CamRegBuf(CAM_SCCB_WRITE, CAM_SCCB_READ); |
hazheng | 36:7e747e19f660 | 67 | |
hazheng | 36:7e747e19f660 | 68 | camReg->WriteRegSet(ResetProg); |
hazheng | 46:a5eb9bd3bb55 | 69 | wait_ms(10); |
hazheng | 37:7074a6118d03 | 70 | camReg->WriteRegSet(QVGA); |
hazheng | 44:15de535c4005 | 71 | wait_ms(10); |
hazheng | 32:5badeff825dc | 72 | |
hazheng | 36:7e747e19f660 | 73 | #if defined(ARDUCAM_OV2640) |
hazheng | 32:5badeff825dc | 74 | camReg->SCCBWrite(0xff, 0x01); |
hazheng | 36:7e747e19f660 | 75 | #endif |
hazheng | 32:5badeff825dc | 76 | |
hazheng | 32:5badeff825dc | 77 | delete camReg; |
hazheng | 32:5badeff825dc | 78 | camReg = NULL; |
hazheng | 32:5badeff825dc | 79 | |
Bobymicjohn | 97:0ed9ede9a995 | 80 | uint8_t VerNum = ardu_cam_spi_read_8(ARDUCHIP_VER); |
Bobymicjohn | 97:0ed9ede9a995 | 81 | VerNum = ardu_cam_spi_read_8(ARDUCHIP_VER); |
hazheng | 29:f87d8790f57d | 82 | |
hazheng | 34:f79db3bc2f86 | 83 | ardu_cam_spi_write_8(ARDUCHIP_TEST1, ARDUCHIP_TEST_MSG); |
hazheng | 34:f79db3bc2f86 | 84 | uint8_t testV = ardu_cam_spi_read_8(ARDUCHIP_TEST1); |
hazheng | 35:ac4fcca21560 | 85 | if(VerNum != ARDUCHIP_VER_NUM || testV != ARDUCHIP_TEST_MSG) |
hazheng | 29:f87d8790f57d | 86 | { |
hazheng | 29:f87d8790f57d | 87 | return false; |
hazheng | 29:f87d8790f57d | 88 | } |
hazheng | 28:271fc8445e89 | 89 | |
hazheng | 96:ec89c4d1383d | 90 | ardu_cam_spi_write_8(ARDUCHIP_CAP_CTRL, 0x00); |
hazheng | 29:f87d8790f57d | 91 | |
hazheng | 44:15de535c4005 | 92 | cal_black_calibrate(); |
hazheng | 29:f87d8790f57d | 93 | |
hazheng | 29:f87d8790f57d | 94 | return true; |
hazheng | 29:f87d8790f57d | 95 | } |
hazheng | 29:f87d8790f57d | 96 | |
hazheng | 29:f87d8790f57d | 97 | void ardu_cam_start_capture() |
hazheng | 29:f87d8790f57d | 98 | { |
hazheng | 34:f79db3bc2f86 | 99 | ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_CLEAR_MASK); |
hazheng | 48:f76b5e252444 | 100 | |
hazheng | 34:f79db3bc2f86 | 101 | ardu_cam_spi_write_8(ARDUCHIP_FIFO, FIFO_START_MASK); |
hazheng | 29:f87d8790f57d | 102 | } |
hazheng | 29:f87d8790f57d | 103 | |
hazheng | 32:5badeff825dc | 104 | uint32_t ardu_cam_get_fifo_length() |
hazheng | 32:5badeff825dc | 105 | { |
hazheng | 32:5badeff825dc | 106 | uint32_t len1,len2,len3,length=0; |
hazheng | 34:f79db3bc2f86 | 107 | len1 = ardu_cam_spi_read_8(FIFO_SIZE1); |
hazheng | 34:f79db3bc2f86 | 108 | len2 = ardu_cam_spi_read_8(FIFO_SIZE2); |
hazheng | 34:f79db3bc2f86 | 109 | len3 = ardu_cam_spi_read_8(FIFO_SIZE3) & 0x07; |
hazheng | 32:5badeff825dc | 110 | length = ((len3 << 16) | (len2 << 8) | len1) & 0x07ffff; |
hazheng | 32:5badeff825dc | 111 | return length; |
hazheng | 32:5badeff825dc | 112 | } |
hazheng | 32:5badeff825dc | 113 | |
hazheng | 32:5badeff825dc | 114 | uint8_t ardu_cam_get_pixel() |
hazheng | 32:5badeff825dc | 115 | { |
hazheng | 34:f79db3bc2f86 | 116 | uint16_t VH = ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
hazheng | 34:f79db3bc2f86 | 117 | uint16_t VL = ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
hazheng | 34:f79db3bc2f86 | 118 | //uint16_t VL = ardu_cam_spi_burst_read_16(); |
hazheng | 32:5badeff825dc | 119 | |
hazheng | 32:5badeff825dc | 120 | VL = (VL & 0x00FF) | ((VH << 8) & 0xFF00); |
hazheng | 33:e3fcc4d6bb9b | 121 | uint8_t ch = ((VL & 0xF800) >> 9);// << 2; |
hazheng | 33:e3fcc4d6bb9b | 122 | float pixel = (static_cast<float>(ch) * 0.21); |
hazheng | 32:5badeff825dc | 123 | |
hazheng | 33:e3fcc4d6bb9b | 124 | ch = ((VL & 0x07E0) >> 3);// << 2; |
hazheng | 33:e3fcc4d6bb9b | 125 | pixel += (static_cast<float>(ch) * 0.72); |
hazheng | 32:5badeff825dc | 126 | |
hazheng | 33:e3fcc4d6bb9b | 127 | ch = (VL & 0x001F) << 2; |
hazheng | 33:e3fcc4d6bb9b | 128 | pixel += (static_cast<float>(ch) * 0.07); |
hazheng | 32:5badeff825dc | 129 | |
hazheng | 32:5badeff825dc | 130 | return static_cast<uint8_t>(pixel); |
hazheng | 32:5badeff825dc | 131 | } |
hazheng | 41:7b21c5e3599e | 132 | |
hazheng | 44:15de535c4005 | 133 | uint8_t ardu_cam_is_capture_finished() |
hazheng | 44:15de535c4005 | 134 | { |
hazheng | 44:15de535c4005 | 135 | return (ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK); |
hazheng | 44:15de535c4005 | 136 | } |
hazheng | 44:15de535c4005 | 137 | |
hazheng | 44:15de535c4005 | 138 | void cal_black_calibrate() |
hazheng | 44:15de535c4005 | 139 | { |
hazheng | 44:15de535c4005 | 140 | ardu_cam_start_capture(); |
hazheng | 44:15de535c4005 | 141 | |
hazheng | 44:15de535c4005 | 142 | while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK)); |
hazheng | 44:15de535c4005 | 143 | |
hazheng | 44:15de535c4005 | 144 | float temp = 0.0f; |
hazheng | 44:15de535c4005 | 145 | static uint16_t pixel = 0x00; |
hazheng | 44:15de535c4005 | 146 | for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j) |
hazheng | 44:15de535c4005 | 147 | { |
hazheng | 44:15de535c4005 | 148 | pixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8; |
hazheng | 44:15de535c4005 | 149 | pixel = pixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
hazheng | 46:a5eb9bd3bb55 | 150 | |
hazheng | 44:15de535c4005 | 151 | if(CAM_BLK_CAL_LEFT < j && j <= CAM_BLK_CAL_RIGHT) |
hazheng | 44:15de535c4005 | 152 | { |
hazheng | 44:15de535c4005 | 153 | temp += static_cast<uint8_t>((pixel >> 3) & 0x00FF); |
hazheng | 44:15de535c4005 | 154 | } |
hazheng | 44:15de535c4005 | 155 | } |
hazheng | 44:15de535c4005 | 156 | temp = temp / (CAM_BLK_CAL_RIGHT - CAM_BLK_CAL_LEFT); |
hazheng | 44:15de535c4005 | 157 | black_calibrate = temp * 0.7f; |
hazheng | 44:15de535c4005 | 158 | } |
hazheng | 44:15de535c4005 | 159 | |
Bobymicjohn | 97:0ed9ede9a995 | 160 | inline void get_img_row_info(const uint8_t rowI, uint8_t * left, uint8_t * right, uint8_t* isBorderFound) |
hazheng | 44:15de535c4005 | 161 | { |
hazheng | 44:15de535c4005 | 162 | *left = 0; |
hazheng | 44:15de535c4005 | 163 | *right = RESOLUTION_WIDTH; |
hazheng | 65:295c222fdf88 | 164 | *isBorderFound = BOTH_NOT_FOUND; |
hazheng | 44:15de535c4005 | 165 | uint8_t isRightFound = 0; |
hazheng | 44:15de535c4005 | 166 | static uint16_t pixel = 0x0000; |
hazheng | 44:15de535c4005 | 167 | static uint8_t pGreen = 0x00; |
hazheng | 44:15de535c4005 | 168 | |
hazheng | 44:15de535c4005 | 169 | for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j) |
hazheng | 44:15de535c4005 | 170 | { |
hazheng | 44:15de535c4005 | 171 | pixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8; |
hazheng | 44:15de535c4005 | 172 | pixel = pixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
hazheng | 46:a5eb9bd3bb55 | 173 | |
hazheng | 57:0d8a155d511d | 174 | pGreen = static_cast<uint8_t>((pixel & 0x07E0) >> 3); |
hazheng | 44:15de535c4005 | 175 | if((pGreen < black_calibrate)) |
hazheng | 44:15de535c4005 | 176 | { |
hazheng | 44:15de535c4005 | 177 | if(j < temp_mid_pos) |
hazheng | 44:15de535c4005 | 178 | { |
hazheng | 65:295c222fdf88 | 179 | *isBorderFound = LEFT_FOUND; |
hazheng | 44:15de535c4005 | 180 | *left = j; |
hazheng | 46:a5eb9bd3bb55 | 181 | #ifdef CAM_DISP_DEBUG |
hazheng | 46:a5eb9bd3bb55 | 182 | ardu_utft_write_DATA(0xF8, 0x00); |
hazheng | 46:a5eb9bd3bb55 | 183 | #endif |
hazheng | 44:15de535c4005 | 184 | } |
hazheng | 44:15de535c4005 | 185 | else if(!isRightFound) |
hazheng | 44:15de535c4005 | 186 | { |
hazheng | 44:15de535c4005 | 187 | *right = j; |
hazheng | 44:15de535c4005 | 188 | isRightFound = 1; |
hazheng | 46:a5eb9bd3bb55 | 189 | #ifdef CAM_DISP_DEBUG |
hazheng | 46:a5eb9bd3bb55 | 190 | ardu_utft_write_DATA(0xF8, 0x00); |
hazheng | 46:a5eb9bd3bb55 | 191 | #endif |
hazheng | 44:15de535c4005 | 192 | } |
hazheng | 73:1dcf56e9f1d4 | 193 | #ifdef CAM_DISP_DEBUG |
hazheng | 73:1dcf56e9f1d4 | 194 | else |
hazheng | 73:1dcf56e9f1d4 | 195 | { |
hazheng | 73:1dcf56e9f1d4 | 196 | ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel)); |
hazheng | 73:1dcf56e9f1d4 | 197 | } |
hazheng | 73:1dcf56e9f1d4 | 198 | #endif |
hazheng | 44:15de535c4005 | 199 | } |
hazheng | 46:a5eb9bd3bb55 | 200 | #ifdef CAM_DISP_DEBUG |
hazheng | 46:a5eb9bd3bb55 | 201 | else |
hazheng | 46:a5eb9bd3bb55 | 202 | { |
hazheng | 46:a5eb9bd3bb55 | 203 | ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel)); |
hazheng | 46:a5eb9bd3bb55 | 204 | } |
hazheng | 50:c387c88141fb | 205 | |
hazheng | 46:a5eb9bd3bb55 | 206 | #elif defined(CAM_DISP_IMG) |
hazheng | 46:a5eb9bd3bb55 | 207 | ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel)); |
hazheng | 46:a5eb9bd3bb55 | 208 | #endif |
hazheng | 44:15de535c4005 | 209 | } |
hazheng | 65:295c222fdf88 | 210 | |
hazheng | 65:295c222fdf88 | 211 | if(*isBorderFound == LEFT_FOUND && isRightFound) |
hazheng | 65:295c222fdf88 | 212 | { |
hazheng | 65:295c222fdf88 | 213 | *isBorderFound = BOTH_FOUND; |
hazheng | 65:295c222fdf88 | 214 | } |
hazheng | 65:295c222fdf88 | 215 | else if(isRightFound) |
hazheng | 65:295c222fdf88 | 216 | { |
hazheng | 65:295c222fdf88 | 217 | *isBorderFound = RIGHT_FOUND; |
hazheng | 65:295c222fdf88 | 218 | } |
hazheng | 65:295c222fdf88 | 219 | |
hazheng | 44:15de535c4005 | 220 | } |
hazheng | 44:15de535c4005 | 221 | |
hazheng | 44:15de535c4005 | 222 | volatile const uint8_t* ardu_cam_get_center_array() |
hazheng | 44:15de535c4005 | 223 | { |
hazheng | 44:15de535c4005 | 224 | return centerLine; |
hazheng | 44:15de535c4005 | 225 | } |
hazheng | 44:15de535c4005 | 226 | |
hazheng | 44:15de535c4005 | 227 | void image_processing() |
hazheng | 44:15de535c4005 | 228 | { |
hazheng | 63:d9a81b3d69f5 | 229 | |
hazheng | 57:0d8a155d511d | 230 | //while(true) |
hazheng | 44:15de535c4005 | 231 | { |
hazheng | 44:15de535c4005 | 232 | ardu_cam_start_capture(); |
hazheng | 44:15de535c4005 | 233 | |
hazheng | 44:15de535c4005 | 234 | while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK)); |
hazheng | 44:15de535c4005 | 235 | |
hazheng | 44:15de535c4005 | 236 | temp_mid_pos = RESOLUTION_WIDTH / 2; |
hazheng | 79:bdbac82c979b | 237 | is_border_find = BOTH_NOT_FOUND; |
hazheng | 57:0d8a155d511d | 238 | #ifdef CAM_BLK_CAL_ACTIVE |
hazheng | 57:0d8a155d511d | 239 | static float calTemp = 0.0f; |
hazheng | 57:0d8a155d511d | 240 | calTemp = 0.0f; |
hazheng | 57:0d8a155d511d | 241 | static uint16_t greenPixel = 0x00; |
hazheng | 64:43ab429a37e0 | 242 | uint8_t isValid = 1; |
hazheng | 57:0d8a155d511d | 243 | for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j) |
hazheng | 57:0d8a155d511d | 244 | { |
hazheng | 57:0d8a155d511d | 245 | if(CAM_BLK_CAL_LEFT < j && j <= CAM_BLK_CAL_RIGHT) |
hazheng | 57:0d8a155d511d | 246 | { |
hazheng | 57:0d8a155d511d | 247 | greenPixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8; |
hazheng | 57:0d8a155d511d | 248 | greenPixel = greenPixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
hazheng | 57:0d8a155d511d | 249 | greenPixel = (greenPixel & 0x07E0) >> 3; |
hazheng | 64:43ab429a37e0 | 250 | if(static_cast<uint8_t>(greenPixel) < black_calibrate) |
hazheng | 64:43ab429a37e0 | 251 | { |
hazheng | 64:43ab429a37e0 | 252 | isValid = 0; |
hazheng | 64:43ab429a37e0 | 253 | } |
hazheng | 57:0d8a155d511d | 254 | calTemp += static_cast<uint8_t>(greenPixel); |
hazheng | 57:0d8a155d511d | 255 | } |
hazheng | 57:0d8a155d511d | 256 | else |
hazheng | 57:0d8a155d511d | 257 | { |
hazheng | 57:0d8a155d511d | 258 | ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
hazheng | 57:0d8a155d511d | 259 | ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
hazheng | 57:0d8a155d511d | 260 | } |
hazheng | 57:0d8a155d511d | 261 | } |
hazheng | 64:43ab429a37e0 | 262 | |
hazheng | 64:43ab429a37e0 | 263 | if(isValid) |
hazheng | 64:43ab429a37e0 | 264 | { |
hazheng | 64:43ab429a37e0 | 265 | calTemp = calTemp / static_cast<float>(CAM_BLK_CAL_RIGHT - CAM_BLK_CAL_LEFT); |
hazheng | 64:43ab429a37e0 | 266 | black_calibrate = static_cast<uint8_t>(calTemp * 0.7f); |
hazheng | 64:43ab429a37e0 | 267 | } |
hazheng | 57:0d8a155d511d | 268 | #endif |
hazheng | 44:15de535c4005 | 269 | //cal_black_calibrate(); |
hazheng | 44:15de535c4005 | 270 | uint8_t leftPos = 0; |
hazheng | 44:15de535c4005 | 271 | uint8_t rightPos = 0; |
hazheng | 65:295c222fdf88 | 272 | uint8_t isBorderFound = BOTH_NOT_FOUND; |
hazheng | 82:992ba6f31e24 | 273 | is_encounter_terminate = 0; |
hazheng | 44:15de535c4005 | 274 | for (uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i) |
hazheng | 44:15de535c4005 | 275 | { |
hazheng | 46:a5eb9bd3bb55 | 276 | #if defined(CAM_DISP_IMG) or defined(CAM_DISP_DEBUG) |
hazheng | 46:a5eb9bd3bb55 | 277 | ardu_utft_set_camimg_row(CAM_ROI_UPPER_LIMIT - i); |
hazheng | 46:a5eb9bd3bb55 | 278 | #endif |
Bobymicjohn | 97:0ed9ede9a995 | 279 | get_img_row_info(i, &leftPos, &rightPos, &isBorderFound); |
hazheng | 44:15de535c4005 | 280 | temp_mid_pos = (leftPos + rightPos) / 2; |
hazheng | 64:43ab429a37e0 | 281 | uint8_t dis_btw_left_right = rightPos - leftPos - 1; |
hazheng | 64:43ab429a37e0 | 282 | if(dis_btw_left_right < TERMINATE_WIDTH) |
hazheng | 64:43ab429a37e0 | 283 | { |
hazheng | 64:43ab429a37e0 | 284 | is_encounter_terminate = 1; |
hazheng | 64:43ab429a37e0 | 285 | } |
hazheng | 46:a5eb9bd3bb55 | 286 | |
hazheng | 46:a5eb9bd3bb55 | 287 | #ifdef CAM_DISP_DEBUG_CENTER |
hazheng | 44:15de535c4005 | 288 | ardu_utft_set_camimg_rowcol(CAM_ROI_UPPER_LIMIT - i, temp_mid_pos); |
hazheng | 44:15de535c4005 | 289 | ardu_utft_write_DATA(0xF8, 0x00); |
hazheng | 44:15de535c4005 | 290 | #endif |
hazheng | 46:a5eb9bd3bb55 | 291 | |
hazheng | 44:15de535c4005 | 292 | centerLine[CAM_ROI_UPPER_LIMIT - i - 1] = temp_mid_pos; |
hazheng | 65:295c222fdf88 | 293 | centerLine[(2 * CAM_ROI_UPPER_LIMIT) - i - 1] = isBorderFound; |
hazheng | 79:bdbac82c979b | 294 | switch(is_border_find) |
hazheng | 79:bdbac82c979b | 295 | { |
hazheng | 79:bdbac82c979b | 296 | case BOTH_NOT_FOUND: |
hazheng | 79:bdbac82c979b | 297 | switch(isBorderFound) |
hazheng | 79:bdbac82c979b | 298 | { |
hazheng | 79:bdbac82c979b | 299 | case LEFT_FOUND: |
hazheng | 79:bdbac82c979b | 300 | is_border_find = LEFT_FOUND; |
hazheng | 79:bdbac82c979b | 301 | break; |
hazheng | 79:bdbac82c979b | 302 | case RIGHT_FOUND: |
hazheng | 79:bdbac82c979b | 303 | is_border_find = RIGHT_FOUND; |
hazheng | 79:bdbac82c979b | 304 | break; |
hazheng | 79:bdbac82c979b | 305 | case BOTH_FOUND: |
hazheng | 79:bdbac82c979b | 306 | is_border_find = BOTH_FOUND; |
hazheng | 79:bdbac82c979b | 307 | break; |
hazheng | 79:bdbac82c979b | 308 | default: |
hazheng | 79:bdbac82c979b | 309 | break; |
hazheng | 79:bdbac82c979b | 310 | } |
hazheng | 79:bdbac82c979b | 311 | break; |
hazheng | 79:bdbac82c979b | 312 | case LEFT_FOUND: |
hazheng | 79:bdbac82c979b | 313 | switch(isBorderFound) |
hazheng | 79:bdbac82c979b | 314 | { |
hazheng | 79:bdbac82c979b | 315 | case RIGHT_FOUND: |
hazheng | 79:bdbac82c979b | 316 | case BOTH_FOUND: |
hazheng | 79:bdbac82c979b | 317 | is_border_find = BOTH_FOUND; |
hazheng | 79:bdbac82c979b | 318 | break; |
hazheng | 79:bdbac82c979b | 319 | default: |
hazheng | 79:bdbac82c979b | 320 | break; |
hazheng | 79:bdbac82c979b | 321 | } |
hazheng | 79:bdbac82c979b | 322 | break; |
hazheng | 79:bdbac82c979b | 323 | case RIGHT_FOUND: |
hazheng | 79:bdbac82c979b | 324 | switch(isBorderFound) |
hazheng | 79:bdbac82c979b | 325 | { |
hazheng | 79:bdbac82c979b | 326 | case LEFT_FOUND: |
hazheng | 79:bdbac82c979b | 327 | case BOTH_FOUND: |
hazheng | 79:bdbac82c979b | 328 | is_border_find = BOTH_FOUND; |
hazheng | 79:bdbac82c979b | 329 | break; |
hazheng | 79:bdbac82c979b | 330 | default: |
hazheng | 79:bdbac82c979b | 331 | break; |
hazheng | 79:bdbac82c979b | 332 | } |
hazheng | 79:bdbac82c979b | 333 | break; |
hazheng | 79:bdbac82c979b | 334 | case BOTH_FOUND: |
hazheng | 79:bdbac82c979b | 335 | default: |
hazheng | 79:bdbac82c979b | 336 | break; |
hazheng | 79:bdbac82c979b | 337 | } |
hazheng | 44:15de535c4005 | 338 | } |
hazheng | 44:15de535c4005 | 339 | } |
hazheng | 44:15de535c4005 | 340 | } |
hazheng | 46:a5eb9bd3bb55 | 341 | |
hazheng | 46:a5eb9bd3bb55 | 342 | |
Bobymicjohn | 97:0ed9ede9a995 | 343 | inline void get_img_row_disp(const uint8_t rowI, uint8_t * left, uint8_t * right) |
Bobymicjohn | 97:0ed9ede9a995 | 344 | { |
Bobymicjohn | 97:0ed9ede9a995 | 345 | *left = 0; |
Bobymicjohn | 97:0ed9ede9a995 | 346 | *right = RESOLUTION_WIDTH; |
Bobymicjohn | 97:0ed9ede9a995 | 347 | uint8_t isRightFound = 0; |
Bobymicjohn | 97:0ed9ede9a995 | 348 | static uint16_t pixel = 0x0000; |
Bobymicjohn | 97:0ed9ede9a995 | 349 | static uint8_t pGreen = 0x00; |
Bobymicjohn | 97:0ed9ede9a995 | 350 | |
Bobymicjohn | 97:0ed9ede9a995 | 351 | for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j) |
Bobymicjohn | 97:0ed9ede9a995 | 352 | { |
Bobymicjohn | 97:0ed9ede9a995 | 353 | pixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8; |
Bobymicjohn | 97:0ed9ede9a995 | 354 | pixel = pixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
Bobymicjohn | 97:0ed9ede9a995 | 355 | |
Bobymicjohn | 97:0ed9ede9a995 | 356 | pGreen = static_cast<uint8_t>((pixel & 0x07E0) >> 3); |
Bobymicjohn | 97:0ed9ede9a995 | 357 | if((pGreen < black_calibrate)) |
Bobymicjohn | 97:0ed9ede9a995 | 358 | { |
Bobymicjohn | 97:0ed9ede9a995 | 359 | if(j < temp_mid_pos) |
Bobymicjohn | 97:0ed9ede9a995 | 360 | { |
Bobymicjohn | 97:0ed9ede9a995 | 361 | *left = j; |
Bobymicjohn | 97:0ed9ede9a995 | 362 | ardu_utft_write_DATA(0xF8, 0x00); |
Bobymicjohn | 97:0ed9ede9a995 | 363 | } |
Bobymicjohn | 97:0ed9ede9a995 | 364 | else if(!isRightFound) |
Bobymicjohn | 97:0ed9ede9a995 | 365 | { |
Bobymicjohn | 97:0ed9ede9a995 | 366 | *right = j; |
Bobymicjohn | 97:0ed9ede9a995 | 367 | isRightFound = 1; |
Bobymicjohn | 97:0ed9ede9a995 | 368 | ardu_utft_write_DATA(0xF8, 0x00); |
Bobymicjohn | 97:0ed9ede9a995 | 369 | } |
Bobymicjohn | 97:0ed9ede9a995 | 370 | else |
Bobymicjohn | 97:0ed9ede9a995 | 371 | { |
Bobymicjohn | 97:0ed9ede9a995 | 372 | ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel)); |
Bobymicjohn | 97:0ed9ede9a995 | 373 | } |
Bobymicjohn | 97:0ed9ede9a995 | 374 | } |
Bobymicjohn | 97:0ed9ede9a995 | 375 | else |
Bobymicjohn | 97:0ed9ede9a995 | 376 | { |
Bobymicjohn | 97:0ed9ede9a995 | 377 | ardu_utft_write_DATA(static_cast<uint8_t>(pixel >> 8), static_cast<uint8_t>(pixel)); |
Bobymicjohn | 97:0ed9ede9a995 | 378 | } |
Bobymicjohn | 97:0ed9ede9a995 | 379 | } |
Bobymicjohn | 97:0ed9ede9a995 | 380 | |
Bobymicjohn | 97:0ed9ede9a995 | 381 | } |
Bobymicjohn | 97:0ed9ede9a995 | 382 | |
hazheng | 43:0d1886f4848a | 383 | void ardu_cam_display_img_utft() |
hazheng | 43:0d1886f4848a | 384 | { |
hazheng | 43:0d1886f4848a | 385 | ardu_cam_start_capture(); |
hazheng | 44:15de535c4005 | 386 | |
hazheng | 44:15de535c4005 | 387 | while (!(ardu_cam_spi_read_8(ARDUCHIP_TRIG) & CAP_DONE_MASK)); |
hazheng | 44:15de535c4005 | 388 | |
hazheng | 44:15de535c4005 | 389 | temp_mid_pos = RESOLUTION_WIDTH / 2; |
Bobymicjohn | 97:0ed9ede9a995 | 390 | #ifdef CAM_BLK_CAL_ACTIVE |
Bobymicjohn | 97:0ed9ede9a995 | 391 | static float calTemp = 0.0f; |
Bobymicjohn | 97:0ed9ede9a995 | 392 | calTemp = 0.0f; |
Bobymicjohn | 97:0ed9ede9a995 | 393 | static uint16_t greenPixel = 0x00; |
Bobymicjohn | 97:0ed9ede9a995 | 394 | uint8_t isValid = 1; |
Bobymicjohn | 97:0ed9ede9a995 | 395 | for (uint8_t j = 0; j < RESOLUTION_WIDTH; ++j) |
Bobymicjohn | 97:0ed9ede9a995 | 396 | { |
Bobymicjohn | 97:0ed9ede9a995 | 397 | if(CAM_BLK_CAL_LEFT < j && j <= CAM_BLK_CAL_RIGHT) |
Bobymicjohn | 97:0ed9ede9a995 | 398 | { |
Bobymicjohn | 97:0ed9ede9a995 | 399 | greenPixel = static_cast<uint16_t>(ardu_cam_spi_read_8(SINGLE_FIFO_READ)) << 8; |
Bobymicjohn | 97:0ed9ede9a995 | 400 | greenPixel = greenPixel | ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
Bobymicjohn | 97:0ed9ede9a995 | 401 | greenPixel = (greenPixel & 0x07E0) >> 3; |
Bobymicjohn | 97:0ed9ede9a995 | 402 | if(static_cast<uint8_t>(greenPixel) < black_calibrate) |
Bobymicjohn | 97:0ed9ede9a995 | 403 | { |
Bobymicjohn | 97:0ed9ede9a995 | 404 | isValid = 0; |
Bobymicjohn | 97:0ed9ede9a995 | 405 | } |
Bobymicjohn | 97:0ed9ede9a995 | 406 | calTemp += static_cast<uint8_t>(greenPixel); |
Bobymicjohn | 97:0ed9ede9a995 | 407 | } |
Bobymicjohn | 97:0ed9ede9a995 | 408 | else |
Bobymicjohn | 97:0ed9ede9a995 | 409 | { |
Bobymicjohn | 97:0ed9ede9a995 | 410 | ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
Bobymicjohn | 97:0ed9ede9a995 | 411 | ardu_cam_spi_read_8(SINGLE_FIFO_READ); |
Bobymicjohn | 97:0ed9ede9a995 | 412 | } |
Bobymicjohn | 97:0ed9ede9a995 | 413 | } |
hazheng | 46:a5eb9bd3bb55 | 414 | |
Bobymicjohn | 97:0ed9ede9a995 | 415 | if(isValid) |
Bobymicjohn | 97:0ed9ede9a995 | 416 | { |
Bobymicjohn | 97:0ed9ede9a995 | 417 | calTemp = calTemp / static_cast<float>(CAM_BLK_CAL_RIGHT - CAM_BLK_CAL_LEFT); |
Bobymicjohn | 97:0ed9ede9a995 | 418 | black_calibrate = static_cast<uint8_t>(calTemp * 0.7f); |
Bobymicjohn | 97:0ed9ede9a995 | 419 | } |
Bobymicjohn | 97:0ed9ede9a995 | 420 | #endif |
Bobymicjohn | 97:0ed9ede9a995 | 421 | //cal_black_calibrate(); |
Bobymicjohn | 97:0ed9ede9a995 | 422 | uint8_t leftPos = 0; |
Bobymicjohn | 97:0ed9ede9a995 | 423 | uint8_t rightPos = 0; |
Bobymicjohn | 97:0ed9ede9a995 | 424 | is_encounter_terminate = 0; |
hazheng | 44:15de535c4005 | 425 | for (uint8_t i = 0; i < CAM_ROI_UPPER_LIMIT; ++i) |
hazheng | 43:0d1886f4848a | 426 | { |
hazheng | 46:a5eb9bd3bb55 | 427 | ardu_utft_set_camimg_row(CAM_ROI_UPPER_LIMIT - i); |
hazheng | 44:15de535c4005 | 428 | |
Bobymicjohn | 97:0ed9ede9a995 | 429 | get_img_row_disp(i, &leftPos, &rightPos); |
Bobymicjohn | 97:0ed9ede9a995 | 430 | temp_mid_pos = (leftPos + rightPos) / 2; |
Bobymicjohn | 97:0ed9ede9a995 | 431 | |
Bobymicjohn | 97:0ed9ede9a995 | 432 | ardu_utft_set_camimg_rowcol(CAM_ROI_UPPER_LIMIT - i, temp_mid_pos); |
Bobymicjohn | 97:0ed9ede9a995 | 433 | ardu_utft_write_DATA(0xF8, 0x00); |
Bobymicjohn | 97:0ed9ede9a995 | 434 | |
hazheng | 43:0d1886f4848a | 435 | } |
hazheng | 44:15de535c4005 | 436 | } |
hazheng | 64:43ab429a37e0 | 437 | |
hazheng | 64:43ab429a37e0 | 438 | uint8_t ardu_cam_get_is_encounter_terminate() |
hazheng | 64:43ab429a37e0 | 439 | { |
hazheng | 64:43ab429a37e0 | 440 | return is_encounter_terminate; |
hazheng | 64:43ab429a37e0 | 441 | } |
hazheng | 79:bdbac82c979b | 442 | |
hazheng | 79:bdbac82c979b | 443 | uint8_t ardu_cam_get_is_border_found() |
hazheng | 79:bdbac82c979b | 444 | { |
hazheng | 79:bdbac82c979b | 445 | return is_border_find; |
hazheng | 79:bdbac82c979b | 446 | } |
hazheng | 96:ec89c4d1383d | 447 | |
Bobymicjohn | 97:0ed9ede9a995 | 448 | uint8_t ardu_cam_get_ver_num() |
Bobymicjohn | 97:0ed9ede9a995 | 449 | { |
Bobymicjohn | 97:0ed9ede9a995 | 450 | return ardu_cam_spi_read_8(ARDUCHIP_VER); |
Bobymicjohn | 97:0ed9ede9a995 | 451 | } |
Bobymicjohn | 97:0ed9ede9a995 | 452 | |
hazheng | 46:a5eb9bd3bb55 | 453 | #ifdef __cplusplus |
hazheng | 46:a5eb9bd3bb55 | 454 | } |
hazheng | 46:a5eb9bd3bb55 | 455 | #endif |
hazheng | 46:a5eb9bd3bb55 | 456 | |
hazheng | 46:a5eb9bd3bb55 | 457 | |
hazheng | 46:a5eb9bd3bb55 | 458 |