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
- Committer:
- hazheng
- Date:
- 2017-03-22
- Revision:
- 40:be98219930e4
- Child:
- 41:7b21c5e3599e
File content as of revision 40:be98219930e4:
#include "ArduUTFT.h" #include "GlobalVariable.h" #include "SWUSBServer.h" extern SPI g_spi_port; #define UTFT_DISP_X_SIZE 239 #define UTFT_DISP_Y_SIZE 319 #define UTFT_DISP_TRANS_MODE 8 #define swap(type, i, j) {type t = i; i = j; j = t;} namespace { uint8_t * current_font = NULL; uint8_t font_x_size = 0; uint8_t font_y_size = 0; uint8_t font_offset = 0; uint8_t font_numchars = 0; uint8_t front_color_high = 0; uint8_t front_color_low = 0; DigitalOut utft_cs(PIN_AUC_CS); } inline void ardu_cam_spi_write_8(int address, int value) { utft_cs = 0; g_spi_port.write(address | 0x80); g_spi_port.write(value); utft_cs = 1; } inline uint8_t ardu_cam_spi_read_8(int address) { utft_cs = 0; g_spi_port.write(address & 0x7F); uint8_t value = static_cast<uint8_t>(g_spi_port.write(0x00)); utft_cs = 1; return value; } inline void ardu_utft_write_COM(uint8_t VL) { utft_cs = 0; g_spi_port.write(0xBE); g_spi_port.write(VL); utft_cs = 1; } inline void ardu_utft_write_DATA(uint8_t VH, uint8_t VL) { utft_cs = 0; g_spi_port.write(0xBF); g_spi_port.write(VH); g_spi_port.write(0xBF); g_spi_port.write(VL); utft_cs = 1; } inline void ardu_utft_write_COM_DATA(uint8_t com1, uint16_t dat1) { ardu_utft_write_COM(com1); ardu_utft_write_DATA(dat1 >> 8, dat1); } void ardu_utft_init() { char buf[20]; uint8_t VerNum = ardu_cam_spi_read_8(0x40); VerNum = ardu_cam_spi_read_8(0x40); sprintf(buf, "UTFT Ver %#x", VerNum); g_core.GetUSBServer().PushReliableMsg('D', buf); ardu_cam_spi_write_8(ARDUCHIP_TEST1_UTFT, ARDUCHIP_TEST_MSG_UTFT); uint8_t testV = ardu_cam_spi_read_8(ARDUCHIP_TEST1_UTFT); if(VerNum != ARDUCHIP_VER_NUM_UTFT || testV != ARDUCHIP_TEST_MSG_UTFT) { g_core.GetUSBServer().PushReliableMsg('D', "UTFTInit Fai"); } else { g_core.GetUSBServer().PushReliableMsg('D', "UTFTInit Suc"); } ardu_cam_set_mode(MCU2LCD_MODE); utft_cs = 1; ardu_utft_write_COM_DATA(0x00,0x0001); ardu_utft_write_COM_DATA(0x03,0xA8A4); ardu_utft_write_COM_DATA(0x0C,0x0000); ardu_utft_write_COM_DATA(0x0D,0x080C); ardu_utft_write_COM_DATA(0x0E,0x2B00); ardu_utft_write_COM_DATA(0x1E,0x00B7); ardu_utft_write_COM_DATA(0x01,0x693F); ardu_utft_write_COM_DATA(0x02,0x0600); ardu_utft_write_COM_DATA(0x10,0x0000); ardu_utft_write_COM_DATA(0x11,0x6078); ardu_utft_write_COM_DATA(0x05,0x0000); ardu_utft_write_COM_DATA(0x06,0x0000); ardu_utft_write_COM_DATA(0x16,0xEF1C); ardu_utft_write_COM_DATA(0x17,0x0003); ardu_utft_write_COM_DATA(0x07,0x0233); ardu_utft_write_COM_DATA(0x0B,0x0000); ardu_utft_write_COM_DATA(0x0F,0x0000); ardu_utft_write_COM_DATA(0x41,0x0000); ardu_utft_write_COM_DATA(0x42,0x0000); ardu_utft_write_COM_DATA(0x48,0x0000); ardu_utft_write_COM_DATA(0x49,0x013F); ardu_utft_write_COM_DATA(0x4A,0x0000); ardu_utft_write_COM_DATA(0x4B,0x0000); ardu_utft_write_COM_DATA(0x44,0xEF00); ardu_utft_write_COM_DATA(0x45,0x0000); ardu_utft_write_COM_DATA(0x46,0x013F); ardu_utft_write_COM_DATA(0x30,0x0707); ardu_utft_write_COM_DATA(0x31,0x0204); ardu_utft_write_COM_DATA(0x32,0x0204); ardu_utft_write_COM_DATA(0x33,0x0502); ardu_utft_write_COM_DATA(0x34,0x0507); ardu_utft_write_COM_DATA(0x35,0x0204); ardu_utft_write_COM_DATA(0x36,0x0204); ardu_utft_write_COM_DATA(0x37,0x0502); ardu_utft_write_COM_DATA(0x3A,0x0302); ardu_utft_write_COM_DATA(0x3B,0x0302); ardu_utft_write_COM_DATA(0x23,0x0000); ardu_utft_write_COM_DATA(0x24,0x0000); ardu_utft_write_COM_DATA(0x25,0x8000); ardu_utft_write_COM_DATA(0x4f,0x0000); ardu_utft_write_COM_DATA(0x4e,0x0000); ardu_utft_write_COM(0x22); //setColor(255, 255, 255); //setBackColor(0, 0, 0); current_font = NULL; ardu_utft_clr_scr(); ardu_utft_set_color(20, 0, 20); ardu_utft_draw_rect(150, 150, 20, 20); } void ardu_cam_set_mode(uint8_t mode) { ///////////////////////////////// //Careful here!! ardu_cam_spi_write_8(ARDUCHIP_MODE, 0x04); } void ardu_utft_clr_scr() { ardu_utft_reset_xy(); long i; for (i = 0; i < ((UTFT_DISP_X_SIZE + 1) * (UTFT_DISP_Y_SIZE + 1)); ++i) { ////////////////////////// //Can be simplified later!! if (UTFT_DISP_TRANS_MODE != 1) { ardu_utft_write_DATA(0x00,0x00); } else { ardu_utft_write_DATA(1,0); ardu_utft_write_DATA(1,0); } } } void ardu_utft_reset_xy() { ardu_utft_set_xy(0, 0, UTFT_DISP_Y_SIZE, UTFT_DISP_X_SIZE); } void ardu_utft_set_xy(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { ///////////////////////////// //Possible to be simplified swap(uint16_t, x1, y1); swap(uint16_t, x2, y2); y1 = UTFT_DISP_Y_SIZE - y1; y2 = UTFT_DISP_Y_SIZE - y2; swap(uint16_t, y1, y2); ///////////////////////////// ardu_utft_write_COM_DATA(0x44, (x2 << 8) + x1); ardu_utft_write_COM_DATA(0x45, y1); ardu_utft_write_COM_DATA(0x46, y2); ardu_utft_write_COM_DATA(0x4e, x1); ardu_utft_write_COM_DATA(0x4f, y1); ardu_utft_write_COM(0x22); } void ardu_utft_set_color(uint8_t r, uint8_t g, uint8_t b) { front_color_high = ((r & 248) | g >> 5); front_color_low = ((g & 28) << 3 | b >> 3); } void ardu_utft_draw_rect(int x1, int y1, int x2, int y2) { if (x1>x2) { swap(int, x1, x2); } if (y1>y2) { swap(int, y1, y2); } ardu_utft_draw_hline(x1, y1, x2 - x1); ardu_utft_draw_hline(x1, y2, x2 - x1); ardu_utft_draw_vline(x1, y1, y2 - y1); ardu_utft_draw_vline(x2, y1, y2 - y1); } void ardu_utft_draw_hline(int x, int y, int l) { if (l<0) { l = -l; x -= l; } ardu_utft_set_xy(x, y, x+l, y); for (int i = 0; i < l + 1; ++i) { ardu_utft_write_DATA(front_color_high, front_color_low); } ardu_utft_reset_xy(); } void ardu_utft_draw_vline(int x, int y, int l) { if (l<0) { l = -l; y -= l; } ardu_utft_set_xy(x, y, x, y+l); for (int i = 0; i < l + 1; ++i) { ardu_utft_write_DATA(front_color_high, front_color_low); } ardu_utft_reset_xy(); }