1D-Pong game based on a LED strip with 150 LPD6803-controlled pixels. Game keeps score for 'best-of-21' game. Written for KL25Z

Dependencies:   MODSERIAL mbed

Revision:
0:cde34c55fa20
Child:
1:7a2ec350cdf5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Aug 21 16:52:51 2013 +0000
@@ -0,0 +1,115 @@
+#include "mbed.h"
+
+#define NUMBER_OF_PIXELS 50
+#define PADDLE_LENGTH    5
+
+void Randomblinks(float seconds, bool colored = false);
+void PaddleDemo(float seconds, uint8_t red, uint8_t green, uint8_t blue);
+
+uint16_t totalstrip[NUMBER_OF_PIXELS];
+volatile int8_t paddlestart= 0;
+SPI ledstrip(PTD2,NC,PTD1);
+
+void UpdatePaddle(void)
+{
+    static uint8_t direction = 1;
+    if(direction) {
+        paddlestart++;
+    } else {
+        paddlestart--;
+    }
+
+    if(paddlestart > (NUMBER_OF_PIXELS - PADDLE_LENGTH))
+        direction = 0;
+    if(paddlestart < 0)
+        direction = 1;
+
+}
+
+void UpdateLEDstrip(void)
+{
+    uint8_t pixelcounter;
+    /*start by writing 32 zeroes */
+    ledstrip.write(0);
+    ledstrip.write(0);
+    ledstrip.write(0);
+    ledstrip.write(0);
+    for(pixelcounter = 0 ; pixelcounter < NUMBER_OF_PIXELS; pixelcounter++) {
+        ledstrip.write( uint8_t(totalstrip[pixelcounter]>>8));//uint8_t(temp16));//(totalstrip.ledcounter[pixelcounter].red << 2) | (totalstrip.ledcounter[pixelcounter].high << 7)  |(totalstrip.ledcounter[pixelcounter].green & 0x << 2)  );
+        ledstrip.write( uint8_t(totalstrip[pixelcounter]));//(*(uint16_t *)(&totalstrip[pixelcounter]))>>8);
+    }
+}
+
+void write_led(uint16_t * led, uint8_t red, uint8_t green, uint8_t blue)
+{
+    *led = (1<<15) | ((green >> 3)<<10) | ((red >>3)<< 5) | (blue >>3);
+}
+int main()
+{
+    Ticker updater;
+    Ticker paddlepos;
+    uint8_t ledcounter;
+    updater.attach(UpdateLEDstrip, .05);
+    paddlepos.attach(UpdatePaddle, .03);
+    ledstrip.format(8,0);        //15 bits, mode '0'
+    ledstrip.frequency(100000);
+    for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) {
+        write_led(&totalstrip[ledcounter], 0,0,0);
+    }
+    while(1) {
+        PaddleDemo(5,255,10,100);
+        Randomblinks(5, true);
+    }
+}
+
+void PaddleDemo(float seconds, uint8_t red, uint8_t green, uint8_t blue)
+{
+    uint8_t ledcounter;
+    Timer timer;
+    timer.start();
+    while( timer.read() < seconds)
+    {
+         for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) 
+         {
+            if((ledcounter >= paddlestart) && ( ledcounter <= paddlestart+PADDLE_LENGTH))
+                write_led(&totalstrip[ledcounter], red,green,blue);
+            else
+                write_led(&totalstrip[ledcounter], 0,0,0);
+        }
+    }
+}
+
+void Randomblinks(float seconds, bool colored)
+{
+    uint8_t ledcounter;
+    uint8_t test;
+    Timer timer;
+    timer.start();
+    while( timer.read() < seconds ) 
+    {
+        test = 50.0*rand()/(RAND_MAX*1.0);
+        for(ledcounter = 0; ledcounter < NUMBER_OF_PIXELS; ledcounter++) 
+        {
+            if(ledcounter == test) 
+            {
+                if(colored)
+                    write_led(&totalstrip[ledcounter], test*5,(test%10)*25,(test%15)*15);
+                else
+                    write_led(&totalstrip[ledcounter], 255,255,255);
+            }
+            else
+                write_led(&totalstrip[ledcounter], 0,0,0);
+        }
+    }
+}
+
+//DigitalOut myled(LED1);
+
+//int main() {
+//    while(1) {
+//        myled = 1;
+//        wait(0.2);
+//        myled = 0;
+//        wait(0.2);
+//    }
+//}