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 13:21:23 2016 +0000
Revision:
4:0f4e8293e7d7
Parent:
2:254e72dcfb52
a refinement of the lottery algorithm from LJ

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 4:0f4e8293e7d7 43 int item[MAN_MAXCNT];
wuulong 0:e0789676dfe5 44
wuulong 0:e0789676dfe5 45 lcd.cls();
wuulong 0:e0789676dfe5 46 lcd.locate(0,3);
wuulong 0:e0789676dfe5 47 lcd.printf("LASS Conference");
wuulong 0:e0789676dfe5 48 lcd.locate(0,15);
wuulong 0:e0789676dfe5 49 lcd.printf("Lottery 1-120");
wuulong 0:e0789676dfe5 50 while (!fire) { // After botton pressed, generate random seed
wuulong 0:e0789676dfe5 51 SweepAllLeds(true,0.01);
wuulong 0:e0789676dfe5 52 uint32_t seedValue = 0;
wuulong 0:e0789676dfe5 53 uint16_t value;
wuulong 0:e0789676dfe5 54 uint8_t counter;
wuulong 0:e0789676dfe5 55
wuulong 0:e0789676dfe5 56 for (counter = 0; counter < 32; counter++) {
wuulong 0:e0789676dfe5 57 seedValue = seedValue<<1;
wuulong 0:e0789676dfe5 58 value = RandomIn.read_u16(); // reads a 10 bit ADC normalised to 16 bits.
wuulong 0:e0789676dfe5 59 if (value & 0x0040) // LSB of ADC output is a 1
wuulong 0:e0789676dfe5 60 seedValue++;
wuulong 0:e0789676dfe5 61 }
wuulong 0:e0789676dfe5 62 srand(seedValue);
wuulong 0:e0789676dfe5 63 }
wuulong 0:e0789676dfe5 64
wuulong 0:e0789676dfe5 65 // show some UI to feel like Lottery
wuulong 0:e0789676dfe5 66 lcd.cls();
wuulong 0:e0789676dfe5 67 lcd.locate(0,3);
wuulong 0:e0789676dfe5 68 lcd.printf("Begin Lottery...");
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 SweepAllLeds(true,0.1);
wuulong 0:e0789676dfe5 73 float twait=(rand()%10)/10;
wuulong 0:e0789676dfe5 74 wait(twait);
wuulong 0:e0789676dfe5 75 lcd.locate(0,3);
wuulong 0:e0789676dfe5 76 lcd.cls();
wuulong 4:0f4e8293e7d7 77
wuulong 0:e0789676dfe5 78 // Generate non-repeat users
wuulong 4:0f4e8293e7d7 79 for(int i=0;i<MAN_MAXCNT;i++){
wuulong 4:0f4e8293e7d7 80 item[i] = i;
wuulong 4:0f4e8293e7d7 81 }
wuulong 0:e0789676dfe5 82 for(int i=0;i<GIFT_MAXCNT;i++){
wuulong 4:0f4e8293e7d7 83 int rand_num = rand()%(MAN_MAXCNT-i);
wuulong 4:0f4e8293e7d7 84 gift[i] = item[rand_num] + 1;
wuulong 4:0f4e8293e7d7 85 if (rand_num != MAN_MAXCNT-i-1) {
wuulong 4:0f4e8293e7d7 86 item[rand_num] = item[MAN_MAXCNT-i-1];
wuulong 0:e0789676dfe5 87 }
wuulong 0:e0789676dfe5 88 }
wuulong 4:0f4e8293e7d7 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 }