The Lottery toy that used in LASS(http://lass-net.org/) Users and Developers conference 2016

Dependencies:   C12832 mbed

Revision:
0:e0789676dfe5
Child:
1:c9dba148697b
diff -r 000000000000 -r e0789676dfe5 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jul 20 09:15:23 2016 +0000
@@ -0,0 +1,97 @@
+/**
+README: The Lottery toy that used in LASS Users and Developers conference.
+    Each run will generate GIFT_MAXCNT non-repeat number, random range ( 1 to MAN_MAXCNT )
+*/
+#include "mbed.h"
+#include "C12832.h"
+
+
+C12832 lcd(p5, p7, p6, p8, p11);
+DigitalIn fire(p14);
+
+BusOut leds(LED1,LED2,LED3,LED4);
+AnalogIn RandomIn(p17); // use the random noise on this analog input to seed the random generator
+#define GIFT_MAXCNT 24 // Max gift count 
+#define MAN_MAXCNT 120 // How many users 
+
+
+void SetLed(uint8_t ledID, bool on)
+{
+    if (ledID <= 6) {
+        if (on)
+            leds = leds | (0x01 << ledID);
+        else
+            leds = leds & ~(0x01 << ledID);
+    }
+}
+
+void SweepAllLeds(bool leftToRight, float delay)
+{
+    leds=0;
+    wait(delay);
+    for(int n=0; n<4; n++) {
+        rand();
+        SetLed(leftToRight?n:4-n, true);
+        wait(delay); // delay
+    }
+}
+
+int main()
+{
+    int gift[GIFT_MAXCNT];
+
+    lcd.cls();
+    lcd.locate(0,3);
+    lcd.printf("LASS Conference");
+    lcd.locate(0,15);
+    lcd.printf("Lottery 1-120");
+    while (!fire) { // After botton pressed, generate random seed
+        SweepAllLeds(true,0.01);
+        uint32_t seedValue = 0;
+        uint16_t value;
+        uint8_t counter;
+
+        for (counter = 0; counter < 32; counter++) {
+            seedValue = seedValue<<1;
+            value = RandomIn.read_u16(); // reads a 10 bit ADC normalised to 16 bits.
+            if (value & 0x0040)          // LSB of ADC output is a 1
+                seedValue++;
+        }
+        srand(seedValue);
+    }
+
+    // show some UI to feel like Lottery
+    lcd.cls();
+    lcd.locate(0,3);
+    lcd.printf("Begin Lottery...");
+    SweepAllLeds(true,0.1);
+    SweepAllLeds(true,0.1);
+    SweepAllLeds(true,0.1);
+    SweepAllLeds(true,0.1);
+    float twait=(rand()%10)/10;
+    wait(twait);
+    lcd.locate(0,3);
+    lcd.cls();
+    // Generate non-repeat users 
+    for(int i=0;i<GIFT_MAXCNT;i++){
+        while(1){
+            int rand_num = rand()%MAN_MAXCNT;
+            int repeat=0;
+            for(int j=0;j<i;j++){
+                if( gift[j] == rand_num ) repeat=1;
+            }
+            if(repeat==0){
+                gift[i] = rand_num;
+                break;
+            }
+        }
+    }
+    // Show the results on LCD
+    for(int i=0;i<GIFT_MAXCNT;i++){
+        int x=(i % 8)*16;
+        int y = int(i/8)*10;
+        lcd.locate(x,y);
+        lcd.printf("%3d", gift[i]);
+    }
+
+}