Ling-Jyh Chen / Mbed 2 deprecated app_board-Lottery

Dependencies:   C12832 mbed

Fork of app_board-Lottery by WuLung Hsu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 README: The Lottery toy that used in LASS(http://lass-net.org/) Users and Developers conference 2016.
00003     Each run will generate GIFT_MAXCNT non-repeat number, random range ( 1 to MAN_MAXCNT )
00004 Hardware: mbed LPC1768 + mbed Application Board
00005 */
00006 #include "mbed.h"
00007 #include "C12832.h"
00008 
00009 
00010 C12832 lcd(p5, p7, p6, p8, p11);
00011 DigitalIn fire(p14);
00012 
00013 BusOut leds(LED1,LED2,LED3,LED4);
00014 AnalogIn RandomIn(p17); // use the random noise on this analog input to seed the random generator
00015 #define GIFT_MAXCNT 24 // Max gift count 
00016 #define MAN_MAXCNT 120 // How many users 
00017 
00018 
00019 void SetLed(uint8_t ledID, bool on)
00020 {
00021     if (ledID <= 6) {
00022         if (on)
00023             leds = leds | (0x01 << ledID);
00024         else
00025             leds = leds & ~(0x01 << ledID);
00026     }
00027 }
00028 
00029 void SweepAllLeds(bool leftToRight, float delay)
00030 {
00031     leds=0;
00032     wait(delay);
00033     for(int n=0; n<4; n++) {
00034         rand();
00035         SetLed(leftToRight?n:4-n, true);
00036         wait(delay); // delay
00037     }
00038 }
00039 
00040 int main()
00041 {
00042     int gift[GIFT_MAXCNT];
00043     int item[MAN_MAXCNT];
00044 
00045     lcd.cls();
00046     lcd.locate(0,3);
00047     lcd.printf("LASS Conference");
00048     lcd.locate(0,15);
00049     lcd.printf("Lottery 1-120");
00050     while (!fire) { // After botton pressed, generate random seed
00051         SweepAllLeds(true,0.01);
00052         uint32_t seedValue = 0;
00053         uint16_t value;
00054         uint8_t counter;
00055 
00056         for (counter = 0; counter < 32; counter++) {
00057             seedValue = seedValue<<1;
00058             value = RandomIn.read_u16(); // reads a 10 bit ADC normalised to 16 bits.
00059             if (value & 0x0040)          // LSB of ADC output is a 1
00060                 seedValue++;
00061         }
00062         srand(seedValue);
00063     }
00064 
00065     // show some UI to feel like Lottery
00066     lcd.cls();
00067     lcd.locate(0,3);
00068     lcd.printf("Begin Lottery...");
00069     SweepAllLeds(true,0.1);
00070     SweepAllLeds(true,0.1);
00071     SweepAllLeds(true,0.1);
00072     SweepAllLeds(true,0.1);
00073     float twait=(rand()%10)/10;
00074     wait(twait);
00075     lcd.locate(0,3);
00076     lcd.cls();
00077     
00078     // Generate non-repeat users 
00079     for(int i=0;i<MAN_MAXCNT;i++){
00080         item[i] = i;
00081     }
00082     for(int i=0;i<GIFT_MAXCNT;i++){
00083         int rand_num = rand()%(MAN_MAXCNT-i);
00084         gift[i] = item[rand_num] + 1;
00085         if (rand_num != MAN_MAXCNT-i-1) {
00086             item[rand_num] = item[MAN_MAXCNT-i-1];
00087         }
00088     }
00089 
00090     // Show the results on LCD
00091     for(int i=0;i<GIFT_MAXCNT;i++){
00092         int x=(i % 8)*16;
00093         int y = int(i/8)*10;
00094         lcd.locate(x,y);
00095         lcd.printf("%3d", gift[i]);
00096     }
00097 
00098 }