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