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

Dependencies:   C12832 mbed

Committer:
wuulong
Date:
Wed Jul 20 11:59:33 2016 +0000
Revision:
2:254e72dcfb52
Parent:
1:c9dba148697b
Child:
3:357d242c2ee7
Child:
4:0f4e8293e7d7
Fix boundry condition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wuulong 0:e0789676dfe5 1 /**
wuulong 1:c9dba148697b 2 README: The Lottery toy that used in LASS(http://lass-net.org/) Users and Developers conference 2016.
wuulong 0:e0789676dfe5 3 Each run will generate GIFT_MAXCNT non-repeat number, random range ( 1 to MAN_MAXCNT )
wuulong 1:c9dba148697b 4 Hardware: mbed LPC1768 + mbed Application Board
wuulong 0:e0789676dfe5 5 */
wuulong 0:e0789676dfe5 6 #include "mbed.h"
wuulong 0:e0789676dfe5 7 #include "C12832.h"
wuulong 0:e0789676dfe5 8
wuulong 0:e0789676dfe5 9
wuulong 0:e0789676dfe5 10 C12832 lcd(p5, p7, p6, p8, p11);
wuulong 0:e0789676dfe5 11 DigitalIn fire(p14);
wuulong 0:e0789676dfe5 12
wuulong 0:e0789676dfe5 13 BusOut leds(LED1,LED2,LED3,LED4);
wuulong 0:e0789676dfe5 14 AnalogIn RandomIn(p17); // use the random noise on this analog input to seed the random generator
wuulong 0:e0789676dfe5 15 #define GIFT_MAXCNT 24 // Max gift count
wuulong 0:e0789676dfe5 16 #define MAN_MAXCNT 120 // How many users
wuulong 0:e0789676dfe5 17
wuulong 0:e0789676dfe5 18
wuulong 0:e0789676dfe5 19 void SetLed(uint8_t ledID, bool on)
wuulong 0:e0789676dfe5 20 {
wuulong 0:e0789676dfe5 21 if (ledID <= 6) {
wuulong 0:e0789676dfe5 22 if (on)
wuulong 0:e0789676dfe5 23 leds = leds | (0x01 << ledID);
wuulong 0:e0789676dfe5 24 else
wuulong 0:e0789676dfe5 25 leds = leds & ~(0x01 << ledID);
wuulong 0:e0789676dfe5 26 }
wuulong 0:e0789676dfe5 27 }
wuulong 0:e0789676dfe5 28
wuulong 0:e0789676dfe5 29 void SweepAllLeds(bool leftToRight, float delay)
wuulong 0:e0789676dfe5 30 {
wuulong 0:e0789676dfe5 31 leds=0;
wuulong 0:e0789676dfe5 32 wait(delay);
wuulong 0:e0789676dfe5 33 for(int n=0; n<4; n++) {
wuulong 0:e0789676dfe5 34 rand();
wuulong 0:e0789676dfe5 35 SetLed(leftToRight?n:4-n, true);
wuulong 0:e0789676dfe5 36 wait(delay); // delay
wuulong 0:e0789676dfe5 37 }
wuulong 0:e0789676dfe5 38 }
wuulong 0:e0789676dfe5 39
wuulong 0:e0789676dfe5 40 int main()
wuulong 0:e0789676dfe5 41 {
wuulong 0:e0789676dfe5 42 int gift[GIFT_MAXCNT];
wuulong 0:e0789676dfe5 43
wuulong 0:e0789676dfe5 44 lcd.cls();
wuulong 0:e0789676dfe5 45 lcd.locate(0,3);
wuulong 0:e0789676dfe5 46 lcd.printf("LASS Conference");
wuulong 0:e0789676dfe5 47 lcd.locate(0,15);
wuulong 0:e0789676dfe5 48 lcd.printf("Lottery 1-120");
wuulong 0:e0789676dfe5 49 while (!fire) { // After botton pressed, generate random seed
wuulong 0:e0789676dfe5 50 SweepAllLeds(true,0.01);
wuulong 0:e0789676dfe5 51 uint32_t seedValue = 0;
wuulong 0:e0789676dfe5 52 uint16_t value;
wuulong 0:e0789676dfe5 53 uint8_t counter;
wuulong 0:e0789676dfe5 54
wuulong 0:e0789676dfe5 55 for (counter = 0; counter < 32; counter++) {
wuulong 0:e0789676dfe5 56 seedValue = seedValue<<1;
wuulong 0:e0789676dfe5 57 value = RandomIn.read_u16(); // reads a 10 bit ADC normalised to 16 bits.
wuulong 0:e0789676dfe5 58 if (value & 0x0040) // LSB of ADC output is a 1
wuulong 0:e0789676dfe5 59 seedValue++;
wuulong 0:e0789676dfe5 60 }
wuulong 0:e0789676dfe5 61 srand(seedValue);
wuulong 0:e0789676dfe5 62 }
wuulong 0:e0789676dfe5 63
wuulong 0:e0789676dfe5 64 // show some UI to feel like Lottery
wuulong 0:e0789676dfe5 65 lcd.cls();
wuulong 0:e0789676dfe5 66 lcd.locate(0,3);
wuulong 0:e0789676dfe5 67 lcd.printf("Begin Lottery...");
wuulong 0:e0789676dfe5 68 SweepAllLeds(true,0.1);
wuulong 0:e0789676dfe5 69 SweepAllLeds(true,0.1);
wuulong 0:e0789676dfe5 70 SweepAllLeds(true,0.1);
wuulong 0:e0789676dfe5 71 SweepAllLeds(true,0.1);
wuulong 0:e0789676dfe5 72 float twait=(rand()%10)/10;
wuulong 0:e0789676dfe5 73 wait(twait);
wuulong 0:e0789676dfe5 74 lcd.locate(0,3);
wuulong 0:e0789676dfe5 75 lcd.cls();
wuulong 0:e0789676dfe5 76 // Generate non-repeat users
wuulong 0:e0789676dfe5 77 for(int i=0;i<GIFT_MAXCNT;i++){
wuulong 0:e0789676dfe5 78 while(1){
wuulong 2:254e72dcfb52 79 int rand_num = rand()%MAN_MAXCNT+1;
wuulong 0:e0789676dfe5 80 int repeat=0;
wuulong 0:e0789676dfe5 81 for(int j=0;j<i;j++){
wuulong 0:e0789676dfe5 82 if( gift[j] == rand_num ) repeat=1;
wuulong 0:e0789676dfe5 83 }
wuulong 0:e0789676dfe5 84 if(repeat==0){
wuulong 0:e0789676dfe5 85 gift[i] = rand_num;
wuulong 0:e0789676dfe5 86 break;
wuulong 0:e0789676dfe5 87 }
wuulong 0:e0789676dfe5 88 }
wuulong 0:e0789676dfe5 89 }
wuulong 0:e0789676dfe5 90 // Show the results on LCD
wuulong 0:e0789676dfe5 91 for(int i=0;i<GIFT_MAXCNT;i++){
wuulong 0:e0789676dfe5 92 int x=(i % 8)*16;
wuulong 0:e0789676dfe5 93 int y = int(i/8)*10;
wuulong 0:e0789676dfe5 94 lcd.locate(x,y);
wuulong 0:e0789676dfe5 95 lcd.printf("%3d", gift[i]);
wuulong 0:e0789676dfe5 96 }
wuulong 0:e0789676dfe5 97
wuulong 0:e0789676dfe5 98 }