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/ArduUTFT.cpp@41:7b21c5e3599e, 2017-03-23 (annotated)
- Committer:
- hazheng
- Date:
- Thu Mar 23 23:17:51 2017 +0000
- Revision:
- 41:7b21c5e3599e
- Parent:
- 40:be98219930e4
- Child:
- 42:c4e1606087ff
Made the TFT screen working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hazheng | 40:be98219930e4 | 1 | #include "ArduUTFT.h" |
hazheng | 40:be98219930e4 | 2 | |
hazheng | 40:be98219930e4 | 3 | #include "GlobalVariable.h" |
hazheng | 40:be98219930e4 | 4 | #include "SWUSBServer.h" |
hazheng | 40:be98219930e4 | 5 | |
hazheng | 40:be98219930e4 | 6 | extern SPI g_spi_port; |
hazheng | 40:be98219930e4 | 7 | |
hazheng | 40:be98219930e4 | 8 | #define UTFT_DISP_X_SIZE 239 |
hazheng | 40:be98219930e4 | 9 | #define UTFT_DISP_Y_SIZE 319 |
hazheng | 40:be98219930e4 | 10 | #define UTFT_DISP_TRANS_MODE 8 |
hazheng | 40:be98219930e4 | 11 | |
hazheng | 40:be98219930e4 | 12 | |
hazheng | 40:be98219930e4 | 13 | #define swap(type, i, j) {type t = i; i = j; j = t;} |
hazheng | 40:be98219930e4 | 14 | |
hazheng | 40:be98219930e4 | 15 | |
hazheng | 40:be98219930e4 | 16 | namespace |
hazheng | 40:be98219930e4 | 17 | { |
hazheng | 40:be98219930e4 | 18 | uint8_t * current_font = NULL; |
hazheng | 40:be98219930e4 | 19 | uint8_t font_x_size = 0; |
hazheng | 40:be98219930e4 | 20 | uint8_t font_y_size = 0; |
hazheng | 40:be98219930e4 | 21 | uint8_t font_offset = 0; |
hazheng | 40:be98219930e4 | 22 | uint8_t font_numchars = 0; |
hazheng | 40:be98219930e4 | 23 | |
hazheng | 40:be98219930e4 | 24 | uint8_t front_color_high = 0; |
hazheng | 40:be98219930e4 | 25 | uint8_t front_color_low = 0; |
hazheng | 40:be98219930e4 | 26 | |
hazheng | 40:be98219930e4 | 27 | } |
hazheng | 40:be98219930e4 | 28 | |
hazheng | 41:7b21c5e3599e | 29 | DigitalOut utft_cs(PIN_AUC_CS, 1); |
hazheng | 41:7b21c5e3599e | 30 | |
hazheng | 40:be98219930e4 | 31 | inline void ardu_cam_spi_write_8(int address, int value) |
hazheng | 40:be98219930e4 | 32 | { |
hazheng | 40:be98219930e4 | 33 | utft_cs = 0; |
hazheng | 40:be98219930e4 | 34 | g_spi_port.write(address | 0x80); |
hazheng | 40:be98219930e4 | 35 | g_spi_port.write(value); |
hazheng | 40:be98219930e4 | 36 | utft_cs = 1; |
hazheng | 40:be98219930e4 | 37 | } |
hazheng | 40:be98219930e4 | 38 | |
hazheng | 40:be98219930e4 | 39 | inline uint8_t ardu_cam_spi_read_8(int address) |
hazheng | 40:be98219930e4 | 40 | { |
hazheng | 40:be98219930e4 | 41 | utft_cs = 0; |
hazheng | 40:be98219930e4 | 42 | g_spi_port.write(address & 0x7F); |
hazheng | 40:be98219930e4 | 43 | uint8_t value = static_cast<uint8_t>(g_spi_port.write(0x00)); |
hazheng | 40:be98219930e4 | 44 | utft_cs = 1; |
hazheng | 40:be98219930e4 | 45 | return value; |
hazheng | 40:be98219930e4 | 46 | } |
hazheng | 40:be98219930e4 | 47 | |
hazheng | 41:7b21c5e3599e | 48 | inline void ardu_utft_write_COM_internal(uint8_t VL) |
hazheng | 40:be98219930e4 | 49 | { |
hazheng | 40:be98219930e4 | 50 | utft_cs = 0; |
hazheng | 40:be98219930e4 | 51 | g_spi_port.write(0xBE); |
hazheng | 40:be98219930e4 | 52 | g_spi_port.write(VL); |
hazheng | 40:be98219930e4 | 53 | utft_cs = 1; |
hazheng | 40:be98219930e4 | 54 | } |
hazheng | 40:be98219930e4 | 55 | |
hazheng | 41:7b21c5e3599e | 56 | inline void ardu_utft_write_DATA_internal(uint8_t VH, uint8_t VL) |
hazheng | 40:be98219930e4 | 57 | { |
hazheng | 40:be98219930e4 | 58 | utft_cs = 0; |
hazheng | 40:be98219930e4 | 59 | g_spi_port.write(0xBF); |
hazheng | 40:be98219930e4 | 60 | g_spi_port.write(VH); |
hazheng | 41:7b21c5e3599e | 61 | utft_cs = 1; |
hazheng | 41:7b21c5e3599e | 62 | utft_cs = 0; |
hazheng | 40:be98219930e4 | 63 | g_spi_port.write(0xBF); |
hazheng | 40:be98219930e4 | 64 | g_spi_port.write(VL); |
hazheng | 40:be98219930e4 | 65 | utft_cs = 1; |
hazheng | 40:be98219930e4 | 66 | } |
hazheng | 40:be98219930e4 | 67 | |
hazheng | 41:7b21c5e3599e | 68 | inline void ardu_utft_write_COM_DATA_internal(uint8_t com1, uint16_t dat1) |
hazheng | 40:be98219930e4 | 69 | { |
hazheng | 41:7b21c5e3599e | 70 | ardu_utft_write_COM_internal(com1); |
hazheng | 41:7b21c5e3599e | 71 | ardu_utft_write_DATA_internal((dat1 >> 8) & 0xFF, dat1); |
hazheng | 41:7b21c5e3599e | 72 | } |
hazheng | 41:7b21c5e3599e | 73 | |
hazheng | 41:7b21c5e3599e | 74 | void ardu_utft_write_DATA(uint8_t VH, uint8_t VL) |
hazheng | 41:7b21c5e3599e | 75 | { |
hazheng | 41:7b21c5e3599e | 76 | ardu_utft_write_DATA_internal(VH, VL); |
hazheng | 40:be98219930e4 | 77 | } |
hazheng | 40:be98219930e4 | 78 | |
hazheng | 40:be98219930e4 | 79 | void ardu_utft_init() |
hazheng | 40:be98219930e4 | 80 | { |
hazheng | 40:be98219930e4 | 81 | char buf[20]; |
hazheng | 40:be98219930e4 | 82 | uint8_t VerNum = ardu_cam_spi_read_8(0x40); |
hazheng | 40:be98219930e4 | 83 | VerNum = ardu_cam_spi_read_8(0x40); |
hazheng | 40:be98219930e4 | 84 | |
hazheng | 40:be98219930e4 | 85 | |
hazheng | 40:be98219930e4 | 86 | sprintf(buf, "UTFT Ver %#x", VerNum); |
hazheng | 40:be98219930e4 | 87 | g_core.GetUSBServer().PushReliableMsg('D', buf); |
hazheng | 40:be98219930e4 | 88 | |
hazheng | 40:be98219930e4 | 89 | ardu_cam_spi_write_8(ARDUCHIP_TEST1_UTFT, ARDUCHIP_TEST_MSG_UTFT); |
hazheng | 40:be98219930e4 | 90 | uint8_t testV = ardu_cam_spi_read_8(ARDUCHIP_TEST1_UTFT); |
hazheng | 40:be98219930e4 | 91 | if(VerNum != ARDUCHIP_VER_NUM_UTFT || testV != ARDUCHIP_TEST_MSG_UTFT) |
hazheng | 40:be98219930e4 | 92 | { |
hazheng | 40:be98219930e4 | 93 | g_core.GetUSBServer().PushReliableMsg('D', "UTFTInit Fai"); |
hazheng | 40:be98219930e4 | 94 | } |
hazheng | 40:be98219930e4 | 95 | else |
hazheng | 40:be98219930e4 | 96 | { |
hazheng | 40:be98219930e4 | 97 | g_core.GetUSBServer().PushReliableMsg('D', "UTFTInit Suc"); |
hazheng | 40:be98219930e4 | 98 | } |
hazheng | 40:be98219930e4 | 99 | |
hazheng | 40:be98219930e4 | 100 | ardu_cam_set_mode(MCU2LCD_MODE); |
hazheng | 41:7b21c5e3599e | 101 | ardu_utft_write_COM_DATA_internal(0x00,0x0001); |
hazheng | 41:7b21c5e3599e | 102 | ardu_utft_write_COM_DATA_internal(0x03,0xA8A4); |
hazheng | 41:7b21c5e3599e | 103 | ardu_utft_write_COM_DATA_internal(0x0C,0x0000); |
hazheng | 41:7b21c5e3599e | 104 | ardu_utft_write_COM_DATA_internal(0x0D,0x080C); |
hazheng | 41:7b21c5e3599e | 105 | ardu_utft_write_COM_DATA_internal(0x0E,0x2B00); |
hazheng | 41:7b21c5e3599e | 106 | ardu_utft_write_COM_DATA_internal(0x1E,0x00B7); |
hazheng | 41:7b21c5e3599e | 107 | ardu_utft_write_COM_DATA_internal(0x01,0x6B3F); //693F |
hazheng | 41:7b21c5e3599e | 108 | ardu_utft_write_COM_DATA_internal(0x02,0x0600); |
hazheng | 41:7b21c5e3599e | 109 | ardu_utft_write_COM_DATA_internal(0x10,0x0000); |
hazheng | 41:7b21c5e3599e | 110 | ardu_utft_write_COM_DATA_internal(0x11,0x6078); |
hazheng | 41:7b21c5e3599e | 111 | ardu_utft_write_COM_DATA_internal(0x05,0x0000); |
hazheng | 41:7b21c5e3599e | 112 | ardu_utft_write_COM_DATA_internal(0x06,0x0000); |
hazheng | 41:7b21c5e3599e | 113 | ardu_utft_write_COM_DATA_internal(0x16,0xEF1C); |
hazheng | 41:7b21c5e3599e | 114 | ardu_utft_write_COM_DATA_internal(0x17,0x0003); |
hazheng | 41:7b21c5e3599e | 115 | ardu_utft_write_COM_DATA_internal(0x07,0x0233); |
hazheng | 41:7b21c5e3599e | 116 | ardu_utft_write_COM_DATA_internal(0x0B,0x0000); |
hazheng | 41:7b21c5e3599e | 117 | ardu_utft_write_COM_DATA_internal(0x0F,0x0000); |
hazheng | 41:7b21c5e3599e | 118 | ardu_utft_write_COM_DATA_internal(0x41,0x0000); |
hazheng | 41:7b21c5e3599e | 119 | ardu_utft_write_COM_DATA_internal(0x42,0x0000); |
hazheng | 41:7b21c5e3599e | 120 | ardu_utft_write_COM_DATA_internal(0x48,0x0000); |
hazheng | 41:7b21c5e3599e | 121 | ardu_utft_write_COM_DATA_internal(0x49,0x013F); |
hazheng | 41:7b21c5e3599e | 122 | ardu_utft_write_COM_DATA_internal(0x4A,0x0000); |
hazheng | 41:7b21c5e3599e | 123 | ardu_utft_write_COM_DATA_internal(0x4B,0x0000); |
hazheng | 41:7b21c5e3599e | 124 | ardu_utft_write_COM_DATA_internal(0x44,0xEF00); |
hazheng | 41:7b21c5e3599e | 125 | ardu_utft_write_COM_DATA_internal(0x45,0x0000); |
hazheng | 41:7b21c5e3599e | 126 | ardu_utft_write_COM_DATA_internal(0x46,0x013F); |
hazheng | 41:7b21c5e3599e | 127 | ardu_utft_write_COM_DATA_internal(0x30,0x0707); |
hazheng | 41:7b21c5e3599e | 128 | ardu_utft_write_COM_DATA_internal(0x31,0x0204); |
hazheng | 41:7b21c5e3599e | 129 | ardu_utft_write_COM_DATA_internal(0x32,0x0204); |
hazheng | 41:7b21c5e3599e | 130 | ardu_utft_write_COM_DATA_internal(0x33,0x0502); |
hazheng | 41:7b21c5e3599e | 131 | ardu_utft_write_COM_DATA_internal(0x34,0x0507); |
hazheng | 41:7b21c5e3599e | 132 | ardu_utft_write_COM_DATA_internal(0x35,0x0204); |
hazheng | 41:7b21c5e3599e | 133 | ardu_utft_write_COM_DATA_internal(0x36,0x0204); |
hazheng | 41:7b21c5e3599e | 134 | ardu_utft_write_COM_DATA_internal(0x37,0x0502); |
hazheng | 41:7b21c5e3599e | 135 | ardu_utft_write_COM_DATA_internal(0x3A,0x0302); |
hazheng | 41:7b21c5e3599e | 136 | ardu_utft_write_COM_DATA_internal(0x3B,0x0302); |
hazheng | 41:7b21c5e3599e | 137 | ardu_utft_write_COM_DATA_internal(0x23,0x0000); |
hazheng | 41:7b21c5e3599e | 138 | ardu_utft_write_COM_DATA_internal(0x24,0x0000); |
hazheng | 41:7b21c5e3599e | 139 | ardu_utft_write_COM_DATA_internal(0x25,0x8000); |
hazheng | 41:7b21c5e3599e | 140 | ardu_utft_write_COM_DATA_internal(0x4f,0x0000); |
hazheng | 41:7b21c5e3599e | 141 | ardu_utft_write_COM_DATA_internal(0x4e,0x0000); |
hazheng | 41:7b21c5e3599e | 142 | ardu_utft_write_COM_internal(0x22); |
hazheng | 41:7b21c5e3599e | 143 | |
hazheng | 41:7b21c5e3599e | 144 | //ardu_cam_spi_write_8(0x03, 0x04); //////////////// |
hazheng | 41:7b21c5e3599e | 145 | |
hazheng | 40:be98219930e4 | 146 | //setColor(255, 255, 255); |
hazheng | 40:be98219930e4 | 147 | //setBackColor(0, 0, 0); |
hazheng | 40:be98219930e4 | 148 | current_font = NULL; |
hazheng | 40:be98219930e4 | 149 | ardu_utft_clr_scr(); |
hazheng | 40:be98219930e4 | 150 | |
hazheng | 41:7b21c5e3599e | 151 | ardu_utft_set_color(255, 0, 255); |
hazheng | 41:7b21c5e3599e | 152 | //ardu_utft_draw_rect(150, 150, 20, 20); |
hazheng | 40:be98219930e4 | 153 | |
hazheng | 40:be98219930e4 | 154 | } |
hazheng | 40:be98219930e4 | 155 | |
hazheng | 40:be98219930e4 | 156 | void ardu_cam_set_mode(uint8_t mode) |
hazheng | 40:be98219930e4 | 157 | { |
hazheng | 40:be98219930e4 | 158 | ///////////////////////////////// |
hazheng | 40:be98219930e4 | 159 | //Careful here!! |
hazheng | 41:7b21c5e3599e | 160 | ardu_cam_spi_write_8(0x02, 0x00); |
hazheng | 41:7b21c5e3599e | 161 | |
hazheng | 40:be98219930e4 | 162 | } |
hazheng | 40:be98219930e4 | 163 | |
hazheng | 40:be98219930e4 | 164 | void ardu_utft_clr_scr() |
hazheng | 40:be98219930e4 | 165 | { |
hazheng | 40:be98219930e4 | 166 | ardu_utft_reset_xy(); |
hazheng | 40:be98219930e4 | 167 | |
hazheng | 40:be98219930e4 | 168 | long i; |
hazheng | 40:be98219930e4 | 169 | for (i = 0; i < ((UTFT_DISP_X_SIZE + 1) * (UTFT_DISP_Y_SIZE + 1)); ++i) |
hazheng | 40:be98219930e4 | 170 | { |
hazheng | 41:7b21c5e3599e | 171 | ardu_utft_write_DATA_internal(0x00,0x00); |
hazheng | 40:be98219930e4 | 172 | } |
hazheng | 40:be98219930e4 | 173 | } |
hazheng | 40:be98219930e4 | 174 | |
hazheng | 40:be98219930e4 | 175 | void ardu_utft_reset_xy() |
hazheng | 40:be98219930e4 | 176 | { |
hazheng | 40:be98219930e4 | 177 | ardu_utft_set_xy(0, 0, UTFT_DISP_Y_SIZE, UTFT_DISP_X_SIZE); |
hazheng | 40:be98219930e4 | 178 | } |
hazheng | 40:be98219930e4 | 179 | |
hazheng | 40:be98219930e4 | 180 | void ardu_utft_set_xy(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) |
hazheng | 40:be98219930e4 | 181 | { |
hazheng | 40:be98219930e4 | 182 | ///////////////////////////// |
hazheng | 40:be98219930e4 | 183 | //Possible to be simplified |
hazheng | 40:be98219930e4 | 184 | swap(uint16_t, x1, y1); |
hazheng | 40:be98219930e4 | 185 | swap(uint16_t, x2, y2); |
hazheng | 40:be98219930e4 | 186 | y1 = UTFT_DISP_Y_SIZE - y1; |
hazheng | 40:be98219930e4 | 187 | y2 = UTFT_DISP_Y_SIZE - y2; |
hazheng | 40:be98219930e4 | 188 | swap(uint16_t, y1, y2); |
hazheng | 40:be98219930e4 | 189 | ///////////////////////////// |
hazheng | 40:be98219930e4 | 190 | |
hazheng | 41:7b21c5e3599e | 191 | ardu_utft_write_COM_DATA_internal(0x44, (x2 << 8) + x1); |
hazheng | 41:7b21c5e3599e | 192 | ardu_utft_write_COM_DATA_internal(0x45, y1); |
hazheng | 41:7b21c5e3599e | 193 | ardu_utft_write_COM_DATA_internal(0x46, y2); |
hazheng | 41:7b21c5e3599e | 194 | ardu_utft_write_COM_DATA_internal(0x4e, x1); |
hazheng | 41:7b21c5e3599e | 195 | ardu_utft_write_COM_DATA_internal(0x4f, y1); |
hazheng | 41:7b21c5e3599e | 196 | ardu_utft_write_COM_internal(0x22); |
hazheng | 40:be98219930e4 | 197 | } |
hazheng | 40:be98219930e4 | 198 | |
hazheng | 40:be98219930e4 | 199 | void ardu_utft_set_color(uint8_t r, uint8_t g, uint8_t b) |
hazheng | 40:be98219930e4 | 200 | { |
hazheng | 40:be98219930e4 | 201 | front_color_high = ((r & 248) | g >> 5); |
hazheng | 40:be98219930e4 | 202 | front_color_low = ((g & 28) << 3 | b >> 3); |
hazheng | 40:be98219930e4 | 203 | } |
hazheng | 40:be98219930e4 | 204 | |
hazheng | 40:be98219930e4 | 205 | void ardu_utft_draw_rect(int x1, int y1, int x2, int y2) |
hazheng | 40:be98219930e4 | 206 | { |
hazheng | 40:be98219930e4 | 207 | if (x1>x2) |
hazheng | 40:be98219930e4 | 208 | { |
hazheng | 40:be98219930e4 | 209 | swap(int, x1, x2); |
hazheng | 40:be98219930e4 | 210 | } |
hazheng | 40:be98219930e4 | 211 | if (y1>y2) |
hazheng | 40:be98219930e4 | 212 | { |
hazheng | 40:be98219930e4 | 213 | swap(int, y1, y2); |
hazheng | 40:be98219930e4 | 214 | } |
hazheng | 40:be98219930e4 | 215 | |
hazheng | 40:be98219930e4 | 216 | ardu_utft_draw_hline(x1, y1, x2 - x1); |
hazheng | 40:be98219930e4 | 217 | ardu_utft_draw_hline(x1, y2, x2 - x1); |
hazheng | 40:be98219930e4 | 218 | ardu_utft_draw_vline(x1, y1, y2 - y1); |
hazheng | 40:be98219930e4 | 219 | ardu_utft_draw_vline(x2, y1, y2 - y1); |
hazheng | 40:be98219930e4 | 220 | } |
hazheng | 40:be98219930e4 | 221 | |
hazheng | 40:be98219930e4 | 222 | void ardu_utft_draw_hline(int x, int y, int l) |
hazheng | 40:be98219930e4 | 223 | { |
hazheng | 40:be98219930e4 | 224 | if (l<0) |
hazheng | 40:be98219930e4 | 225 | { |
hazheng | 40:be98219930e4 | 226 | l = -l; |
hazheng | 40:be98219930e4 | 227 | x -= l; |
hazheng | 40:be98219930e4 | 228 | } |
hazheng | 40:be98219930e4 | 229 | |
hazheng | 40:be98219930e4 | 230 | ardu_utft_set_xy(x, y, x+l, y); |
hazheng | 40:be98219930e4 | 231 | |
hazheng | 40:be98219930e4 | 232 | for (int i = 0; i < l + 1; ++i) |
hazheng | 40:be98219930e4 | 233 | { |
hazheng | 41:7b21c5e3599e | 234 | ardu_utft_write_DATA_internal(front_color_high, front_color_low); |
hazheng | 40:be98219930e4 | 235 | } |
hazheng | 40:be98219930e4 | 236 | |
hazheng | 40:be98219930e4 | 237 | ardu_utft_reset_xy(); |
hazheng | 40:be98219930e4 | 238 | } |
hazheng | 40:be98219930e4 | 239 | |
hazheng | 40:be98219930e4 | 240 | void ardu_utft_draw_vline(int x, int y, int l) |
hazheng | 40:be98219930e4 | 241 | { |
hazheng | 40:be98219930e4 | 242 | if (l<0) |
hazheng | 40:be98219930e4 | 243 | { |
hazheng | 40:be98219930e4 | 244 | l = -l; |
hazheng | 40:be98219930e4 | 245 | y -= l; |
hazheng | 40:be98219930e4 | 246 | } |
hazheng | 40:be98219930e4 | 247 | |
hazheng | 40:be98219930e4 | 248 | ardu_utft_set_xy(x, y, x, y+l); |
hazheng | 40:be98219930e4 | 249 | |
hazheng | 40:be98219930e4 | 250 | for (int i = 0; i < l + 1; ++i) |
hazheng | 40:be98219930e4 | 251 | { |
hazheng | 41:7b21c5e3599e | 252 | ardu_utft_write_DATA_internal(front_color_high, front_color_low); |
hazheng | 40:be98219930e4 | 253 | } |
hazheng | 40:be98219930e4 | 254 | |
hazheng | 40:be98219930e4 | 255 | ardu_utft_reset_xy(); |
hazheng | 40:be98219930e4 | 256 | } |