pong game added to the main sketch

Dependencies:   RTC-DS1307 SPI_TFT_ILI9341 TFT_fonts mbed tsi_sensor

Fork of MainSketch by IoT Ox

Files at this revision

API Documentation at this revision

Comitter:
mlin
Date:
Wed May 24 03:11:29 2017 +0000
Parent:
8:b9aa9fdf286b
Child:
10:9d9b3b9b28b8
Commit message:
Imported libraries as headers and combined

Changed in this revision

Poll.h Show annotated file Show diff for this revision Revisions of this file
RTC-DS1307.lib Show annotated file Show diff for this revision Revisions of this file
RTC.h Show annotated file Show diff for this revision Revisions of this file
debug.h Show annotated file Show diff for this revision Revisions of this file
libs.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
render.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Poll.h	Wed May 24 03:11:29 2017 +0000
@@ -0,0 +1,75 @@
+
+int readX()
+{
+    int delta=0,xv1=0,xv2=0,k=0;
+    int temp1=0,temp2=0;
+
+    AnalogIn yp(PTB3);
+    AnalogIn ym(PTB2);
+    DigitalOut xp(PTB0);
+    DigitalOut xm(PTB1);
+
+    xp=1; // set positive sdie of x high
+    xm=0;
+    // dont need to do anyhting to set low side as it should be fine.
+    // but do need to disconnect yp
+    //yp.PinMode(PullNone)
+    delta = 0;
+    for(k=0; k<10; k++) { // make 10 readings to average
+        temp1= (int)ym.read_u16();
+        temp2 = (int)yp.read_u16();
+        xv1+=temp1;  // get value
+        xv2+=temp2; // get other value
+        delta+= abs(temp1-temp2)/10; //gets individual differences
+       // pc.printf("val1 = %d - val2 = %d-diff = %d\n\r",temp1,temp2,temp1-temp2);
+        //observing behaviour when touching / nt touching
+        
+    }
+    //delta=abs(xv2-xv1)/10;
+    if(delta<300) touching=1;
+    else touching=0;
+    pc.printf("delta=%d \t %d\n\r",delta,touching);
+    xp=0;
+    xm=0;
+    return (240 - (240 * ((xv1 + xv2) / 2 / 10 - 5800)) / 51200); //returns the average of both
+    //return(xv2/10); //maybe better to return the average of both....
+}
+// subroutine to read y values - has different pin functions ..
+int readY()
+{
+    DigitalOut yp(PTB3);
+    DigitalOut ym(PTB2);
+    AnalogIn xp(PTB0);
+    AnalogIn xm(PTB1);
+    int delta=0,yv1=0,yv2=0,k=0;
+    int temp1=0,temp2=0;
+    yp=1; // set positive sdie of x high
+    ym=0;
+    // dont need to do anyhting to set low side as it should be fine.
+    // but do need to disconnect yp
+    //yp.PinMode(PullNone)
+    delta = 0;
+    for(k=0; k<10; k++) { // make 10 readings to average
+        temp1= (int)xm.read_u16();
+        temp2 = (int)xp.read_u16();
+        yv1+=temp1;  // get value
+        yv2+=temp2; // get other value
+        delta+= abs(temp1-temp2)/10;
+        //pc.printf("val1 = %d - val2 = %d-diff = %d\n\r",temp1,temp2,temp1-temp2);
+    }
+    //int yval=(int)xm.read_u16();  // get value
+    //pc.printf("yval=%d",yval);
+    yp=0;
+    ym=0;
+    return (320 - (320 * ((yv1 + yv2)/ 20 - 3000)) / 58300);   // returns Y
+//    return(yval);
+
+}
+
+
+
+void poll(){
+    int xp,yp = 0;
+    xp=readX();
+    yp=readY();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTC-DS1307.lib	Wed May 24 03:11:29 2017 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/Iot-Ox/code/RTC-DS1307/#984b0ce5c236
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTC.h	Wed May 24 03:11:29 2017 +0000
@@ -0,0 +1,59 @@
+ #include "Rtc_Ds1307.h"
+Rtc_Ds1307 rtc(I2C_SDA, I2C_SCL);
+Rtc_Ds1307::Time_rtc tm_c = {};
+Rtc_Ds1307::Time_rtc tm_a = {};
+char buffer[128];
+int readptr = 0;
+
+
+void read_time(){
+ //  perform read
+    pc.printf("Performing read operation\n\r");
+    int n = 0;
+    do   {
+        n++;
+    } while(!rtc.getTime(tm_c));
+pc.printf("The current time is : %02d:%02d:%02d\n\r", tm_c.hour, tm_c.min, tm_c.sec);
+pc.printf("The current date is : %s, %02d/%02d/%04d\n\r", rtc.weekdayToString(tm_c.wday), tm_c.mon, tm_c.date, tm_c.year);
+pc.printf("Tried to read %d times\n\r",n);   
+}
+
+void write(){
+//  perform write
+    pc.printf("Enter the date (date 0..31)");
+    pc.scanf("%d", &tm_c.date);
+    pc.printf("Enter the date (month 1..12)");
+    pc.scanf("%d", &tm_c.mon);
+    pc.printf("Enter the date (year)");
+    pc.scanf("%d", &tm_c.year);
+    pc.printf("Enter the time (hours 0..23)");
+    pc.scanf("%d", &tm_c.hour);
+    pc.printf("Enter the time (minutes 0..59)");
+    pc.scanf("%d", &tm_c.min);
+    pc.printf("Enter the time (seconds 0..59)");
+    pc.scanf("%d", &tm_c.sec);
+    pc.printf("Performing write operation\n\r");
+    
+    while(pc.readable()) 
+    
+        pc.getc();
+    int n = 0;
+    while (!rtc.setTime(tm_c, true, false)){  //set the Time in RTC Param : ( Time container, Start Clock, Twelve Hour Clock)
+        n++;        
+    }
+    pc.printf("Tried to write %d times\n\r",n);           
+}
+
+//other RTC functions:
+//rtc.startClock();
+//rtc.stopClock();
+//square wave
+//            int rs;
+//            pc.printf("Please specify the frequency : [0 = 1Hz, 1 = 4.096kHz, 2 = 8.192kHz, 3 = 32.768kHz] ");
+//            scanf("%d", &rs);
+//            pc.printf("Enabling the output with %d option\n", rs);
+//            rtc.setSquareWaveOutput(true, (Rtc_Ds1307::SqwRateSelect_t)rs);
+
+
+//square wave off
+//rtc.setSquareWaveOutput(false, Rtc_Ds1307::RS1Hz);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/debug.h	Wed May 24 03:11:29 2017 +0000
@@ -0,0 +1,16 @@
+#ifndef __DEBUG_H__
+#define __DEBUG_H__
+
+
+#ifdef DEBUG
+#define INFO(x, ...) std::printf("[INFO: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[WARN: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
+#define ERR(x, ...) std::printf("[ERR: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
+#else
+#define INFO(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#endif
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libs.h	Wed May 24 03:11:29 2017 +0000
@@ -0,0 +1,6 @@
+#include "mbed.h"
+Serial pc(USBTX,USBRX,"pc");
+#include "render.h"
+#include "Poll.h"
+#include "RTC.h"
+
--- a/main.cpp	Tue May 23 23:04:03 2017 +0000
+++ b/main.cpp	Wed May 24 03:11:29 2017 +0000
@@ -1,106 +1,9 @@
-// example to test the TFT Display from Mikroelectronika
-
-#include "stdio.h"
-#include "mbed.h"
-#include "SPI_TFT_ILI9341.h"
-#include "string"
-#include "Arial12x12.h"
-#include "Arial24x23.h"
-#include "Arial28x28.h"
-#include "font_big.h"
-
+#include "libs.h"
+int f_state, f_button;
+bool f_alarm;
 DigitalIn sw_w(SW3);
-Serial pc(USBTX,USBRX,"pc");
-
-
-
-
-// the display has a backlight switch on board
-//DigitalOut LCD_LED(PTA4);   // may not be needed on mikroelectronika board
-//DigitalOut pwr(PTD7); // ditto
-
-// the TFT is connected to SPI pin 5-7
-//SPI_TFT_ILI9341 TFT(p5, p6, p7, p8, p9, p10,"TFT"); // mosi, miso, sclk, cs, reset, dc for lpc1768
-SPI_TFT_ILI9341 TFT(PTD6, PTD7, PTD5, PTD2, PTD4, PTA13,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
-//NB better combination to use a coherent 2x4 block for lcd
-//   SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTA16, PTA17, PTC16,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
-//   DigitalOut LCD_LED(PTC17);
-int touching=0;
-
-// Subroutine to read the x location of the touch point
-// need to set x+ to 3V and ground x- then read analogue voltage on ym
-//nb need to add a check for actual touch as opposed to random crap
-int readX()
-{
-    int delta=0,xv1=0,xv2=0,k=0;
-    int temp1=0,temp2=0;
-
-    AnalogIn yp(PTB3);
-    AnalogIn ym(PTB2);
-    DigitalOut xp(PTB0);
-    DigitalOut xm(PTB1);
 
-    xp=1; // set positive sdie of x high
-    xm=0;
-    // dont need to do anyhting to set low side as it should be fine.
-    // but do need to disconnect yp
-    //yp.PinMode(PullNone)
-    delta = 0;
-    for(k=0; k<10; k++) { // make 10 readings to average
-        temp1= (int)ym.read_u16();
-        temp2 = (int)yp.read_u16();
-        xv1+=temp1;  // get value
-        xv2+=temp2; // get other value
-        delta+= abs(temp1-temp2)/10;
-        pc.printf("val1 = %d - val2 = %d-diff = %d\n\r",temp1,temp2,temp1-temp2);
-    }
-    //delta=abs(xv2-xv1)/10;
-    if(delta<300) touching=1;
-    else touching=0;
-    pc.printf("delta=%d \t %d\n\r",delta,touching);
-    xp=0;
-    xm=0;
-    return(xv2/10); //maybe better to return the average of both....
-}
-// subroutine to read y values - has different pin functions ..
-int readY()
-{
-    DigitalOut yp(PTB3);
-    DigitalOut ym(PTB2);
-    AnalogIn xp(PTB0);
-    AnalogIn xm(PTB1);
-
-    yp=1; // set positive sdie of x high
-    ym=0;
-    // dont need to do anyhting to set low side as it should be fine.
-    // but do need to disconnect yp
-    //yp.PinMode(PullNone)
-    int yval=(int)xm.read_u16();  // get value
-    pc.printf("yval=%d",yval);
-    yp=0;
-    ym=0;
-    return(yval);
-
-}
-
-void drawbuttons()
-{
-    TFT.fillrect(0,0,50,50,Red);
-    TFT.fillrect(50,0,100,50,Green);
-    TFT.fillrect(100,0,150,50,Blue);
-    TFT.fillrect(150,0,200,50,White);
-    TFT.fillrect(200,0,240,50,Black);
-
-    TFT.rect(0,0,50,50,White);
-    TFT.rect(50,0,100,50,White);
-    TFT.rect(100,0,150,50,White);
-    TFT.rect(150,0,200,50,White);
-    TFT.rect(200,0,240,50,White);
-
-}
-
-
-int main()
+void main()
 {
    // pc.baud(115200);
     int color=0;
@@ -117,55 +20,21 @@
     TFT.foreground(White);    // set chars to white
     TFT.cls();                // clear the screen
 
-    //first show the 4 directions
-    TFT.set_orientation(0);
-    TFT.background(Black);
-    TFT.cls();
-
+    
     TFT.set_font((unsigned char*) Arial12x12);
-    TFT.locate(0,0);
-    printf(" 0 Hello Mbed 0");
-    TFT.set_orientation(1);
-    TFT.locate(0,0);
-    printf(" 1 Hello Mbed 1");
-    TFT.set_orientation(2);
-    TFT.locate(0,0);
-    printf(" 2 Hello Mbed 2");
-    TFT.set_orientation(3);
-    TFT.locate(0,0);
-    printf(" 3 Hello Mbed 3");
-    TFT.set_orientation(3);
-    TFT.set_font((unsigned char*) Arial24x23);
-    TFT.locate(50,100);
-    TFT.printf("TFT orientation 3");
-    TFT.set_orientation(0);
-
-    for(i=0; i<10; i++) {
-        wait(0.2);        // wait one seconds
-        TFT.locate(50,160);
-        TFT.printf("count %d",i);
-    }
-    TFT.set_orientation(0);
-    TFT.cls();
-    //   LCD_LED = 1;
-    //cornwer markers
-    TFT.fillcircle(10,10,5,0xffff);
-    TFT.fillcircle(230,10,5,0xffff);
-    TFT.fillcircle(230,310,5,0xffff);
-    TFT.fillcircle(10,310,5,0xffff);
     drawbuttons();
     while(1==1) {
-        while (sw_w == 1) {}
-        xpos=readX();
-        ypos=readY();
+      
+        xp=readX();
+        yp=readY();
         // top chunk of the screen is the button area //
         // 0<y<50 is palette area //
 
         //pc.printf("xpos=%d\t,\typo=%d",xpos,ypos);
-        xp=(240*(xpos-5800))/51200;
-        yp=320-(320*(ypos-3000))/58300;
+       // xp=(240*(xpos-5800))/51200;
+       // yp=320-(320*(ypos-3000))/58300;
         if(touching==1) pc.printf("\txp=%d\t,\typo=%d\n\r",xp,yp);
-        if(xp>5 && yp>50 && touching==1) TFT.fillcircle(xp,yp,2,color);
+        if(xp>5 && yp>50 && touching==1) TFT.fillcircle(yp,xp,2,color);
         if(yp<50) { // color buttons
             sw=(int)xp/50;
             switch(sw) {
@@ -192,7 +61,7 @@
             // if(xp>100 && xp<150) color=0x001f;
         }
 
-        wait(1);
+        wait(0.01);
 
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/render.h	Wed May 24 03:11:29 2017 +0000
@@ -0,0 +1,50 @@
+#include "stdio.h"
+#include "SPI_TFT_ILI9341.h"
+#include "string"
+#include "Arial12x12.h"
+#include "Arial24x23.h"
+#include "Arial28x28.h"
+#include "font_big.h"
+
+// example to test the TFT Display from Mikroelectronika
+
+
+
+
+
+
+
+
+// the display has a backlight switch on board
+//DigitalOut LCD_LED(PTA4);   // may not be needed on mikroelectronika board
+//DigitalOut pwr(PTD7); // ditto
+
+// the TFT is connected to SPI pin 5-7
+//SPI_TFT_ILI9341 TFT(p5, p6, p7, p8, p9, p10,"TFT"); // mosi, miso, sclk, cs, reset, dc for lpc1768
+SPI_TFT_ILI9341 TFT(PTD6, PTD7, PTD5, PTD2, PTD4, PTA13,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
+//NB better combination to use a coherent 2x4 block for lcd
+//   SPI_TFT_ILI9341 TFT(PTD2, PTD3, PTD1, PTA16, PTA17, PTC16,"TFT"); // mosi, miso, sclk, cs, reset, dc for frdmkl25z
+//   DigitalOut LCD_LED(PTC17);
+int touching=0;
+
+// Subroutine to read the x location of the touch point
+// need to set x+ to 3V and ground x- then read analogue voltage on ym
+//nb need to add a check for actual touch as opposed to random crap
+
+
+
+
+void drawbuttons()
+{
+  TFT.fillrect(0,0,50,50,Red);
+    TFT.fillrect(0,50,50,100,Green);
+    TFT.fillrect(0,100,50,150,Blue);
+    TFT.fillrect(0,150,50,200,White);
+    TFT.fillrect(0,200,50,250,Black);
+
+    TFT.rect(0,0,50,50,White);
+    TFT.rect(0,50,50,100,White);
+    TFT.rect(0,100,50,150,White);
+    TFT.rect(0,150,50,200,White);
+    TFT.rect(0,200,50,250,White);
+}
\ No newline at end of file