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/ArduTouch.cpp@79:bdbac82c979b, 2017-04-18 (annotated)
- Committer:
- hazheng
- Date:
- Tue Apr 18 17:43:12 2017 +0000
- Branch:
- Drift
- Revision:
- 79:bdbac82c979b
- Parent:
- 78:9f20bf037db6
- Child:
- 80:c85cb93713b3
Finished the basic function of the touch screen.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hazheng | 78:9f20bf037db6 | 1 | #include "ArduTouch.h" |
hazheng | 78:9f20bf037db6 | 2 | |
hazheng | 79:bdbac82c979b | 3 | #include "GlobalVariable.h" |
hazheng | 79:bdbac82c979b | 4 | #include "PinAssignment.h" |
hazheng | 79:bdbac82c979b | 5 | |
hazheng | 79:bdbac82c979b | 6 | #define SW_DEBUG |
hazheng | 79:bdbac82c979b | 7 | #include "SWCommon.h" |
hazheng | 78:9f20bf037db6 | 8 | |
hazheng | 78:9f20bf037db6 | 9 | #ifdef __cplusplus |
hazheng | 78:9f20bf037db6 | 10 | extern "C" { |
hazheng | 78:9f20bf037db6 | 11 | #endif |
hazheng | 78:9f20bf037db6 | 12 | |
hazheng | 79:bdbac82c979b | 13 | #define TOUCH_PREC 10 |
hazheng | 78:9f20bf037db6 | 14 | |
hazheng | 79:bdbac82c979b | 15 | #define PixSizeX 13.78 |
hazheng | 79:bdbac82c979b | 16 | #define PixOffsX 411 |
hazheng | 79:bdbac82c979b | 17 | #define PixSizeY 4.5 |
hazheng | 79:bdbac82c979b | 18 | #define PixOffsY 378 |
hazheng | 79:bdbac82c979b | 19 | |
hazheng | 79:bdbac82c979b | 20 | static DigitalOut touch_cs(PIN_ATC_TOUCH_CS, 1); |
hazheng | 79:bdbac82c979b | 21 | static InterruptIn touch_irq(PIN_ATC_TOUCH_IRQ); |
hazheng | 79:bdbac82c979b | 22 | static uint16_t touch_x = 0; |
hazheng | 79:bdbac82c979b | 23 | static uint16_t touch_y = 0; |
hazheng | 79:bdbac82c979b | 24 | |
hazheng | 79:bdbac82c979b | 25 | static Timeout touch_recover_timer; |
hazheng | 79:bdbac82c979b | 26 | |
hazheng | 79:bdbac82c979b | 27 | void ardu_touch_recover(); |
hazheng | 79:bdbac82c979b | 28 | void ardu_touch_read(); |
hazheng | 79:bdbac82c979b | 29 | |
hazheng | 79:bdbac82c979b | 30 | void ardu_touch_recover() |
hazheng | 79:bdbac82c979b | 31 | { |
hazheng | 79:bdbac82c979b | 32 | touch_irq.fall(&ardu_touch_read); |
hazheng | 79:bdbac82c979b | 33 | } |
hazheng | 79:bdbac82c979b | 34 | |
hazheng | 79:bdbac82c979b | 35 | inline uint16_t ardu_touch_read_data_16(uint8_t address) |
hazheng | 79:bdbac82c979b | 36 | { |
hazheng | 79:bdbac82c979b | 37 | uint16_t result; |
hazheng | 79:bdbac82c979b | 38 | |
hazheng | 79:bdbac82c979b | 39 | touch_cs = 0; |
hazheng | 79:bdbac82c979b | 40 | g_spi_port.write(address); |
hazheng | 79:bdbac82c979b | 41 | result = static_cast<uint8_t>(g_spi_port.write(0x00)) << 8; |
hazheng | 79:bdbac82c979b | 42 | result |= static_cast<uint8_t>(g_spi_port.write(0x00)); |
hazheng | 79:bdbac82c979b | 43 | result = result >> 4; |
hazheng | 79:bdbac82c979b | 44 | touch_cs = 1; |
hazheng | 79:bdbac82c979b | 45 | |
hazheng | 79:bdbac82c979b | 46 | return result; |
hazheng | 79:bdbac82c979b | 47 | } |
hazheng | 79:bdbac82c979b | 48 | |
hazheng | 79:bdbac82c979b | 49 | void ardu_touch_read() |
hazheng | 79:bdbac82c979b | 50 | { |
hazheng | 79:bdbac82c979b | 51 | touch_irq.fall(NULL); |
hazheng | 79:bdbac82c979b | 52 | unsigned long tx = 0; |
hazheng | 79:bdbac82c979b | 53 | unsigned long ty = 0; |
hazheng | 79:bdbac82c979b | 54 | |
hazheng | 79:bdbac82c979b | 55 | for (int i = 0; i < TOUCH_PREC; ++i) |
hazheng | 79:bdbac82c979b | 56 | { |
hazheng | 79:bdbac82c979b | 57 | ty+=ardu_touch_read_data_16(0x90); |
hazheng | 79:bdbac82c979b | 58 | tx+=ardu_touch_read_data_16(0xD0); |
hazheng | 79:bdbac82c979b | 59 | } |
hazheng | 79:bdbac82c979b | 60 | |
hazheng | 79:bdbac82c979b | 61 | touch_x = tx / TOUCH_PREC; |
hazheng | 79:bdbac82c979b | 62 | touch_y = ty / TOUCH_PREC; |
hazheng | 79:bdbac82c979b | 63 | |
hazheng | 79:bdbac82c979b | 64 | int value_x, value_y; |
hazheng | 79:bdbac82c979b | 65 | |
hazheng | 79:bdbac82c979b | 66 | value_x = ((touch_y - PixOffsY) / PixSizeY); |
hazheng | 79:bdbac82c979b | 67 | |
hazheng | 79:bdbac82c979b | 68 | if (value_x < 0) |
hazheng | 79:bdbac82c979b | 69 | value_x = 0; |
hazheng | 79:bdbac82c979b | 70 | else if(value_x > 320) |
hazheng | 79:bdbac82c979b | 71 | value_x = 320; |
hazheng | 79:bdbac82c979b | 72 | |
hazheng | 79:bdbac82c979b | 73 | value_y = ((240 - ((touch_x - PixOffsX) / PixSizeX)) - 150) * 2; |
hazheng | 79:bdbac82c979b | 74 | |
hazheng | 79:bdbac82c979b | 75 | if (value_y < 0) |
hazheng | 79:bdbac82c979b | 76 | value_y = 0; |
hazheng | 79:bdbac82c979b | 77 | else if(value_y > 240) |
hazheng | 79:bdbac82c979b | 78 | value_x = 240; |
hazheng | 79:bdbac82c979b | 79 | |
hazheng | 79:bdbac82c979b | 80 | LOGI("#%d, %d#", value_x, value_y); |
hazheng | 79:bdbac82c979b | 81 | |
hazheng | 79:bdbac82c979b | 82 | touch_recover_timer.attach(&ardu_touch_recover, 0.5f); |
hazheng | 79:bdbac82c979b | 83 | } |
hazheng | 79:bdbac82c979b | 84 | |
hazheng | 79:bdbac82c979b | 85 | /* |
hazheng | 79:bdbac82c979b | 86 | inline uint16_t ardu_touch_read_data_16(uint8_t cmd) |
hazheng | 79:bdbac82c979b | 87 | { |
hazheng | 79:bdbac82c979b | 88 | uint16_t result; |
hazheng | 79:bdbac82c979b | 89 | touch_cs = 0; |
hazheng | 79:bdbac82c979b | 90 | g_spi_port.write(cmd); |
hazheng | 79:bdbac82c979b | 91 | wait_us(1); |
hazheng | 79:bdbac82c979b | 92 | result = static_cast<uint8_t>(g_spi_port.write(0x00)) << 8; |
hazheng | 79:bdbac82c979b | 93 | result |= static_cast<uint8_t>(g_spi_port.write(0x00)); |
hazheng | 79:bdbac82c979b | 94 | result = result >> 4; |
hazheng | 79:bdbac82c979b | 95 | touch_cs = 1; |
hazheng | 79:bdbac82c979b | 96 | return result; |
hazheng | 79:bdbac82c979b | 97 | } |
hazheng | 79:bdbac82c979b | 98 | |
hazheng | 79:bdbac82c979b | 99 | #define LOST_VAL 1 |
hazheng | 79:bdbac82c979b | 100 | inline uint16_t ardu_touch_read_pos(uint8_t cmd) |
hazheng | 79:bdbac82c979b | 101 | { |
hazheng | 79:bdbac82c979b | 102 | uint16_t buf[TOUCH_PREC]; |
hazheng | 79:bdbac82c979b | 103 | uint16_t sum = 0; |
hazheng | 79:bdbac82c979b | 104 | uint16_t temp; |
hazheng | 79:bdbac82c979b | 105 | for(int i = 0; i < TOUCH_PREC; ++i) |
hazheng | 79:bdbac82c979b | 106 | { |
hazheng | 79:bdbac82c979b | 107 | buf[i] = ardu_touch_read_data_16(cmd); |
hazheng | 79:bdbac82c979b | 108 | } |
hazheng | 79:bdbac82c979b | 109 | for(int i = 0; i < TOUCH_PREC - 1; ++i) |
hazheng | 79:bdbac82c979b | 110 | { |
hazheng | 79:bdbac82c979b | 111 | for(int j = i + 1; j < TOUCH_PREC; ++j) |
hazheng | 79:bdbac82c979b | 112 | { |
hazheng | 79:bdbac82c979b | 113 | if(buf[i] > buf[j]) |
hazheng | 79:bdbac82c979b | 114 | { |
hazheng | 79:bdbac82c979b | 115 | temp = buf[i]; |
hazheng | 79:bdbac82c979b | 116 | buf[i] = buf[j]; |
hazheng | 79:bdbac82c979b | 117 | buf[j] = temp; |
hazheng | 79:bdbac82c979b | 118 | } |
hazheng | 79:bdbac82c979b | 119 | } |
hazheng | 79:bdbac82c979b | 120 | } |
hazheng | 79:bdbac82c979b | 121 | for(int i = LOST_VAL; i < TOUCH_PREC - LOST_VAL; ++i) |
hazheng | 79:bdbac82c979b | 122 | { |
hazheng | 79:bdbac82c979b | 123 | sum += buf[i]; |
hazheng | 79:bdbac82c979b | 124 | } |
hazheng | 79:bdbac82c979b | 125 | return (sum / (TOUCH_PREC - (2 * LOST_VAL))); |
hazheng | 79:bdbac82c979b | 126 | } |
hazheng | 79:bdbac82c979b | 127 | |
hazheng | 79:bdbac82c979b | 128 | void ardu_touch_read() |
hazheng | 79:bdbac82c979b | 129 | { |
hazheng | 79:bdbac82c979b | 130 | touch_irq.fall(NULL); |
hazheng | 79:bdbac82c979b | 131 | |
hazheng | 79:bdbac82c979b | 132 | touch_x = ardu_touch_read_pos(0X90); |
hazheng | 79:bdbac82c979b | 133 | touch_y = ardu_touch_read_pos(0XD0); |
hazheng | 79:bdbac82c979b | 134 | |
hazheng | 79:bdbac82c979b | 135 | int value_x, value_y; |
hazheng | 79:bdbac82c979b | 136 | |
hazheng | 79:bdbac82c979b | 137 | if (PixSizeY<0) |
hazheng | 79:bdbac82c979b | 138 | value_x = 400 - ((touch_y - PixOffsY) / -PixSizeY); |
hazheng | 79:bdbac82c979b | 139 | else |
hazheng | 79:bdbac82c979b | 140 | value_x = ((touch_y - PixOffsY) / PixSizeY); |
hazheng | 79:bdbac82c979b | 141 | |
hazheng | 79:bdbac82c979b | 142 | if (value_x < 0) |
hazheng | 79:bdbac82c979b | 143 | value_x = 0; |
hazheng | 79:bdbac82c979b | 144 | |
hazheng | 79:bdbac82c979b | 145 | if (PixSizeX>=0) |
hazheng | 79:bdbac82c979b | 146 | { |
hazheng | 79:bdbac82c979b | 147 | value_y = 240 - ((touch_x - PixOffsX) / PixSizeX); |
hazheng | 79:bdbac82c979b | 148 | } |
hazheng | 79:bdbac82c979b | 149 | else |
hazheng | 79:bdbac82c979b | 150 | { |
hazheng | 79:bdbac82c979b | 151 | value_y = (touch_x - PixOffsX) / -(PixSizeX); |
hazheng | 79:bdbac82c979b | 152 | } |
hazheng | 79:bdbac82c979b | 153 | |
hazheng | 79:bdbac82c979b | 154 | if (value_y < 0) |
hazheng | 79:bdbac82c979b | 155 | value_y = 0; |
hazheng | 79:bdbac82c979b | 156 | |
hazheng | 79:bdbac82c979b | 157 | LOGI("#%d, %d#", value_x, value_y); |
hazheng | 79:bdbac82c979b | 158 | |
hazheng | 79:bdbac82c979b | 159 | touch_recover_timer.attach(&ardu_touch_recover, 0.5f); |
hazheng | 79:bdbac82c979b | 160 | } |
hazheng | 79:bdbac82c979b | 161 | */ |
hazheng | 79:bdbac82c979b | 162 | void ardu_touch_init() |
hazheng | 79:bdbac82c979b | 163 | { |
hazheng | 79:bdbac82c979b | 164 | touch_irq.fall(&ardu_touch_read); |
hazheng | 79:bdbac82c979b | 165 | } |
hazheng | 79:bdbac82c979b | 166 | |
hazheng | 79:bdbac82c979b | 167 | void ardu_touch_get_pos(int16_t* x, int16_t* y) |
hazheng | 79:bdbac82c979b | 168 | { |
hazheng | 79:bdbac82c979b | 169 | |
hazheng | 79:bdbac82c979b | 170 | } |
hazheng | 78:9f20bf037db6 | 171 | |
hazheng | 78:9f20bf037db6 | 172 | #ifdef __cplusplus |
hazheng | 78:9f20bf037db6 | 173 | } |
hazheng | 78:9f20bf037db6 | 174 | #endif |