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 haofan Zheng

Branch:
Drift
Revision:
80:c85cb93713b3
Parent:
79:bdbac82c979b
Child:
81:32bd7a25a699
--- a/Hardwares/ArduTouch.cpp	Tue Apr 18 17:43:12 2017 +0000
+++ b/Hardwares/ArduTouch.cpp	Tue Apr 18 19:26:33 2017 +0000
@@ -19,17 +19,21 @@
 
 static DigitalOut touch_cs(PIN_ATC_TOUCH_CS, 1);
 static InterruptIn touch_irq(PIN_ATC_TOUCH_IRQ);
-static uint16_t touch_x = 0;
-static uint16_t touch_y = 0;
+static int16_t value_x = 0;
+static int16_t value_y = 0;
 
 static Timeout touch_recover_timer;
 
+static void (*touch_irq_func)(void) = NULL;
+static void (*touch_xy_func)(int16_t, int16_t) = NULL;
+
 void ardu_touch_recover();
 void ardu_touch_read();
+void ardu_touch_irq_handler();
 
 void ardu_touch_recover()
 {
-    touch_irq.fall(&ardu_touch_read);
+    touch_irq.fall(&ardu_touch_irq_handler);
 }
 
 inline uint16_t ardu_touch_read_data_16(uint8_t address)
@@ -48,7 +52,6 @@
 
 void ardu_touch_read()
 {
-    touch_irq.fall(NULL);
     unsigned long tx = 0;
     unsigned long ty = 0;                
 
@@ -57,29 +60,26 @@
         ty+=ardu_touch_read_data_16(0x90);
         tx+=ardu_touch_read_data_16(0xD0);
     }
-
+    
+    int touch_x, touch_y;
     touch_x = tx / TOUCH_PREC;
     touch_y = ty / TOUCH_PREC;
     
-    int value_x, value_y;
-    
-    value_x = ((touch_y - PixOffsY) / PixSizeY);
+    value_x = ((touch_y - PixOffsY) / PixSizeY) + 15;
     
     if (value_x < 0)
         value_x = 0;
     else if(value_x > 320)
         value_x = 320;
         
-    value_y = ((240 - ((touch_x - PixOffsX) / PixSizeX)) - 150) * 2;
+    value_y = (((240 - ((touch_x - PixOffsX) / PixSizeX)) - 150) * 2) + 15;
     
     if (value_y < 0)
         value_y = 0;
     else if(value_y > 240)
         value_x = 240;
         
-    LOGI("#%d, %d#", value_x, value_y);
-    
-    touch_recover_timer.attach(&ardu_touch_recover, 0.5f);
+    //LOGI("#%d, %d#", value_x, value_y);
 }
 
 /*
@@ -159,9 +159,43 @@
     touch_recover_timer.attach(&ardu_touch_recover, 0.5f);
 }
 */
+
+void ardu_touch_irq_handler()
+{
+    touch_irq.fall(NULL);
+    
+    if(touch_irq_func)
+    {
+        (*touch_irq_func)();
+    }
+    if(touch_xy_func)
+    {
+        ardu_touch_read();
+        (*touch_xy_func)(value_x, value_y);
+    }
+    /*
+    ardu_touch_read();
+    ardu_utft_set_xy(value_x, value_y, value_x, value_y);
+    ardu_utft_write_DATA(0xF8, 0x00);
+    */
+    
+    touch_recover_timer.attach(&ardu_touch_recover, 0.5f);
+}
+
+
 void ardu_touch_init()
 {
-    touch_irq.fall(&ardu_touch_read);
+    touch_irq.fall(&ardu_touch_irq_handler);
+}
+
+void ardu_touch_set_irq_function(void(*irq_func)(void))
+{
+    touch_irq_func = irq_func;
+}
+
+void ardu_touch_set_pos_function(void(*pos_func)(int16_t, int16_t))
+{
+    touch_xy_func = pos_func;
 }
 
 void ardu_touch_get_pos(int16_t* x, int16_t* y)