NXP Rapid IoT prototyping kit port of Silabs "hungry gecko" smake-like game. https://os.mbed.com/teams/SiliconLabs/code/Hungry_gecko/

Dependencies:   lib_sx9500 GraphicsDisplay ColorMemLCD Large_fonts

See a detailed description of this project on Hackster.io . https://www.hackster.io/marcomerli/riotwear-snake-ca6dfc

Revision:
81:737dff75e013
Parent:
80:77210aa1ad9c
diff -r 77210aa1ad9c -r 737dff75e013 main.cpp
--- a/main.cpp	Thu Dec 26 16:47:58 2019 +0000
+++ b/main.cpp	Fri Dec 27 16:04:44 2019 +0000
@@ -39,26 +39,41 @@
 #include "string"
 #include "Prototype24x27.h"
 #include "Prototype_num21x25.h"
-#include "meirio_num11x14.h"
+// #include "meirio_num11x14.h"
 
+#if(TOUCH_EN)
+#include "sx9500.h"
+#endif
 
 /**************************** Define I/O **************************************/
 
 /* LEDS */
 DigitalOut led_blue(LED_BLUE, 1); // 1 = OFF
 DigitalOut led_red(LED_RED, 1);
+DigitalOut led_green(PTE7,1);     // 1 --> OFF, 0 --> ON
 
 /* Rapid IoT Buttons */
+#if(!TOUCH_EN)
 // InterruptIn in(PTA4);
 InterruptIn in(PTE28);
 InterruptIn inB1(PTE9);
 // InterruptIn SW1(PTE8);
 // DigitalIn SW3(PTE10);
 // DigitalIn SW4(PTE28);
+#endif // !TOUCH_EN
 
+/* LCD */ 
 DigitalOut     lcd_light(PTE12, 1); // 1 = ON, 0 = OFF
 ColorMemLCD    display( PTB22, PTB23, PTB21, PTB20, PTD15,  PTD9, "TFT");
 
+/* Touch sensor */
+#if(TOUCH_EN)
+I2C i2c0(I2C_SDA , I2C_SCL );    // I2C_SCL = PTC10,  I2C_SDA = PTC11,
+SX9500 touch(i2c0, PTA24, PTA9); // TOUCH_TXEN = PTA24, TOUCH_INT = PTA9
+DigitalOut touch_rst(PTA2,1);    // TOUCH_RST = PTA2
+InterruptIn touch_int(PTA9);
+#endif
+
 /**************************** Define Timers ***********************************/
 
 // LowPowerTicker ticker;
@@ -95,6 +110,60 @@
 /* Set the game mode */
 Modes mode = PLAY;
 
+#if(TOUCH_EN)
+void in_handler_touch()
+{
+    /*
+SX9500_TouchState_t ts;
+
+    if (PBenabled) {
+        ts = touch.read_proximity_sensors();      
+
+        if(ts.downPressed && (dir != UP) )
+            dir = DOWN;            
+        if(ts.rightPressed && (dir != LEFT) )
+            dir = RIGHT;
+        if(ts.upPressed && (dir != DOWN) )
+            dir = UP;
+        if(ts.leftPressed && (dir != RIGHT) )
+            dir = LEFT;
+        // if(!ts.downPressed && !ts.rightPressed && !ts.upPressed && !ts.leftPressed )
+     
+        PBenabled = false;
+        touch.service();
+    }
+*/
+
+led_green = !led_green;
+
+}
+
+void touch_service()
+{
+SX9500_TouchState_t ts;
+
+    if (PBenabled) {
+        ts = touch.read_proximity_sensors();      
+
+        if(ts.downPressed && (dir != UP) )
+            dir = DOWN;            
+        if(ts.rightPressed && (dir != LEFT) )
+            dir = RIGHT;
+        if(ts.upPressed && (dir != DOWN) )
+            dir = UP;
+        if(ts.leftPressed && (dir != RIGHT) )
+            dir = LEFT;
+        // if(!ts.downPressed && !ts.rightPressed && !ts.upPressed && !ts.leftPressed )
+     
+        PBenabled = false;
+        touch.service();
+    }
+
+// led_green = !led_green;
+
+}
+
+#else // TOUCH_EN
 void in_handler_B0()
 {
     /* Only change the direction if push button is enabled */
@@ -138,7 +207,7 @@
         PBenabled = false;
     }
 }
-
+#endif // TOUCH_EN
 
 /* Callback functions */
 #define POLLING_LOOP 3
@@ -149,7 +218,7 @@
     if(idx==0) {
         display.polling();
         idx = POLLING_LOOP;
-        led_blue = !led_blue;
+        // led_blue = !led_blue;
     }
 
 }
@@ -161,7 +230,7 @@
     /* Enable push buttons if the display is refreshed */
     PBenabled = refreshed;
 
-    led_red = !led_red;
+    // led_red = !led_red;
 }
 
 void refreshCallback(void)
@@ -302,9 +371,20 @@
 /**************************** MAIN ********************************************/
 int main()
 {
+#if(TOUCH_EN)
+    wait(1);  
+    touch.reset();        
+    wait(0.3); // wait until the reset has finished
+    touch.init();
+    wait(0.3);
+    touch.set_active(true);    
+    wait(0.3);
+    touch_int.fall(in_handler_touch);
+#else // TOUCH_EN
     /* Initialize pushbutton handlers */
     in.fall(in_handler_B0);
     inB1.fall(in_handler_B1);
+#endif // TOUCH_EN
 
     /* Enable the LCD */
     display.command_AllClear();
@@ -342,6 +422,10 @@
         tickerCallback();
         #endif
 
+        #if(TOUCH_EN)
+        touch_service();
+        #endif
+
         if (updateDisplay && refreshed && (mode==PLAY)) {
             updateDisplay = false;
             refreshed = false;