mbed Simple \'Roulette\' Game. Example program making use of 7-Segment Display Driver Library (via an 8bit Shift Register)

Dependencies:   mbed

Committer:
paul80nd
Date:
Sun May 01 17:26:59 2011 +0000
Revision:
0:7f9842ec56b9
First Revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
paul80nd 0:7f9842ec56b9 1 #include "mbed.h"
paul80nd 0:7f9842ec56b9 2 #include "7SegSRDriver.h"
paul80nd 0:7f9842ec56b9 3
paul80nd 0:7f9842ec56b9 4 /* mbed Simple 'Roulette' Game
paul80nd 0:7f9842ec56b9 5 * Making use of 7-Segment Display Driver Library (via an 8bit Shift Register)
paul80nd 0:7f9842ec56b9 6 * Copyright (c) 2011 Paul Law
paul80nd 0:7f9842ec56b9 7 *
paul80nd 0:7f9842ec56b9 8 * User chooses a number between 1 and 9 by pressing button A repeatedly
paul80nd 0:7f9842ec56b9 9 * - Selection is shown on second 7seg display
paul80nd 0:7f9842ec56b9 10 * User then presses button B to begin the game
paul80nd 0:7f9842ec56b9 11 * - Number between 1 and 9 is chosen at random
paul80nd 0:7f9842ec56b9 12 * - First display shows 'suspense' animation then shows number
paul80nd 0:7f9842ec56b9 13 * - If the numbers match the 2 displays flash and the blue leds flash
paul80nd 0:7f9842ec56b9 14 * - If the numbers don't match the 1st display flashes only
paul80nd 0:7f9842ec56b9 15 * - Another round of the game starts, ad infinitum
paul80nd 0:7f9842ec56b9 16 *
paul80nd 0:7f9842ec56b9 17 * Permission is hereby granted, free of charge, to any person obtaining a copy
paul80nd 0:7f9842ec56b9 18 * of this software and associated documentation files (the "Software"), to deal
paul80nd 0:7f9842ec56b9 19 * in the Software without restriction, including without limitation the rights
paul80nd 0:7f9842ec56b9 20 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
paul80nd 0:7f9842ec56b9 21 * copies of the Software, and to permit persons to whom the Software is
paul80nd 0:7f9842ec56b9 22 * furnished to do so, subject to the following conditions:
paul80nd 0:7f9842ec56b9 23 *
paul80nd 0:7f9842ec56b9 24 * The above copyright notice and this permission notice shall be included in
paul80nd 0:7f9842ec56b9 25 * all copies or substantial portions of the Software.
paul80nd 0:7f9842ec56b9 26 *
paul80nd 0:7f9842ec56b9 27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
paul80nd 0:7f9842ec56b9 28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
paul80nd 0:7f9842ec56b9 29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
paul80nd 0:7f9842ec56b9 30 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
paul80nd 0:7f9842ec56b9 31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
paul80nd 0:7f9842ec56b9 32 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
paul80nd 0:7f9842ec56b9 33 * THE SOFTWARE.
paul80nd 0:7f9842ec56b9 34 */
paul80nd 0:7f9842ec56b9 35
paul80nd 0:7f9842ec56b9 36 // Pin outs
paul80nd 0:7f9842ec56b9 37 // p17 - Play button +ve
paul80nd 0:7f9842ec56b9 38 // p18 - Select button +ve
paul80nd 0:7f9842ec56b9 39 // p22 - 1st Display Shift Register - Clock
paul80nd 0:7f9842ec56b9 40 // p23 - 1st Display Shift Register - Latch
paul80nd 0:7f9842ec56b9 41 // p24 - 1st Display Shift Register - Data
paul80nd 0:7f9842ec56b9 42 // p25 - 2nd Display Shift Register - Clock
paul80nd 0:7f9842ec56b9 43 // p26 - 2nd Display Shift Register - Latch
paul80nd 0:7f9842ec56b9 44 // p27 - 2nd Display Shift Register - Data
paul80nd 0:7f9842ec56b9 45 DigitalIn playButton(p17);
paul80nd 0:7f9842ec56b9 46 DigitalIn selectButton(p18);
paul80nd 0:7f9842ec56b9 47 SSegSRDriver lDisplay(p23,p21,p22, SSegSRDriver_COMN_ANODE);
paul80nd 0:7f9842ec56b9 48 SSegSRDriver rDisplay(p27,p25,p26, SSegSRDriver_COMN_ANODE);
paul80nd 0:7f9842ec56b9 49 DigitalOut leds[] = { (LED1),(LED2),(LED3),(LED4) };
paul80nd 0:7f9842ec56b9 50
paul80nd 0:7f9842ec56b9 51 // Display Roll Definitions (first element in array is number of items, followed by items
paul80nd 0:7f9842ec56b9 52 #define ROLL_CWURC 0
paul80nd 0:7f9842ec56b9 53 #define ROLL_CCWLC 1
paul80nd 0:7f9842ec56b9 54 #define ROLL_CCWLRC 2
paul80nd 0:7f9842ec56b9 55 #define ROLL_CWUC 3
paul80nd 0:7f9842ec56b9 56 #define ROLL_FIG8 4
paul80nd 0:7f9842ec56b9 57 #define ROLL_CWO 5
paul80nd 0:7f9842ec56b9 58 unsigned char rollDefs[6][9] = {
paul80nd 0:7f9842ec56b9 59 // A clockwise upper reverse C - abg
paul80nd 0:7f9842ec56b9 60 {3,2,4,128},
paul80nd 0:7f9842ec56b9 61 // An anticlockwise lower C - ged
paul80nd 0:7f9842ec56b9 62 {3,128,32,16},
paul80nd 0:7f9842ec56b9 63 // An anticlockwise lower reverse C - dcg
paul80nd 0:7f9842ec56b9 64 {3,16,8,128},
paul80nd 0:7f9842ec56b9 65 // A clockwise upper C - gfa
paul80nd 0:7f9842ec56b9 66 {3,128,64,2},
paul80nd 0:7f9842ec56b9 67 // A figure of 8 roll - abgedcgf
paul80nd 0:7f9842ec56b9 68 {8,2,4,128,32,16,8,128,64},
paul80nd 0:7f9842ec56b9 69 // A clockwise O roll - abcdef
paul80nd 0:7f9842ec56b9 70 {6,2,4,8,16,32,64}
paul80nd 0:7f9842ec56b9 71 };
paul80nd 0:7f9842ec56b9 72
paul80nd 0:7f9842ec56b9 73 // Performs a single roll animation on the given display
paul80nd 0:7f9842ec56b9 74 void rollDisplay(SSegSRDriver *display, unsigned char rollType, unsigned char repeats) {
paul80nd 0:7f9842ec56b9 75 for (int r=0; r<repeats; r++) {
paul80nd 0:7f9842ec56b9 76 for (int i=1; i<=rollDefs[rollType][0]; i++) {
paul80nd 0:7f9842ec56b9 77 display->write_raw(rollDefs[rollType][i]);
paul80nd 0:7f9842ec56b9 78 wait_ms(75);
paul80nd 0:7f9842ec56b9 79 }
paul80nd 0:7f9842ec56b9 80 }
paul80nd 0:7f9842ec56b9 81 display->clear();
paul80nd 0:7f9842ec56b9 82 }
paul80nd 0:7f9842ec56b9 83
paul80nd 0:7f9842ec56b9 84 int main() {
paul80nd 0:7f9842ec56b9 85
paul80nd 0:7f9842ec56b9 86 unsigned char guess;
paul80nd 0:7f9842ec56b9 87
paul80nd 0:7f9842ec56b9 88 while (1) {
paul80nd 0:7f9842ec56b9 89
paul80nd 0:7f9842ec56b9 90 lDisplay.clear();
paul80nd 0:7f9842ec56b9 91 rDisplay.clear();
paul80nd 0:7f9842ec56b9 92
paul80nd 0:7f9842ec56b9 93 // New game animation (figure of 8 across two displays)
paul80nd 0:7f9842ec56b9 94 rollDisplay(&rDisplay,ROLL_CWURC,1);
paul80nd 0:7f9842ec56b9 95 rollDisplay(&lDisplay,ROLL_CCWLC,1);
paul80nd 0:7f9842ec56b9 96 rollDisplay(&rDisplay,ROLL_CCWLRC,1);
paul80nd 0:7f9842ec56b9 97 rollDisplay(&lDisplay,ROLL_CWUC,1);
paul80nd 0:7f9842ec56b9 98
paul80nd 0:7f9842ec56b9 99 // User selects guess
paul80nd 0:7f9842ec56b9 100 guess = 1;
paul80nd 0:7f9842ec56b9 101 rDisplay.write(guess);
paul80nd 0:7f9842ec56b9 102 while (!playButton) {
paul80nd 0:7f9842ec56b9 103 if (selectButton) {
paul80nd 0:7f9842ec56b9 104 guess++;
paul80nd 0:7f9842ec56b9 105 if (guess==10) guess=1;
paul80nd 0:7f9842ec56b9 106 rDisplay.write(guess);
paul80nd 0:7f9842ec56b9 107 while (selectButton) {
paul80nd 0:7f9842ec56b9 108 wait(0.25);
paul80nd 0:7f9842ec56b9 109 }
paul80nd 0:7f9842ec56b9 110 }
paul80nd 0:7f9842ec56b9 111 wait(0.25);
paul80nd 0:7f9842ec56b9 112 }
paul80nd 0:7f9842ec56b9 113
paul80nd 0:7f9842ec56b9 114 // Choose random number and animate 'suspense'
paul80nd 0:7f9842ec56b9 115 int target = (rand() % 9)+1;
paul80nd 0:7f9842ec56b9 116 rollDisplay(&lDisplay,ROLL_CWO,2);
paul80nd 0:7f9842ec56b9 117
paul80nd 0:7f9842ec56b9 118 // Show number and flash (both and leds if win, just target if lost)
paul80nd 0:7f9842ec56b9 119 for (int i=0; i<5; i++) {
paul80nd 0:7f9842ec56b9 120 lDisplay.clear();
paul80nd 0:7f9842ec56b9 121 if (target == guess) {
paul80nd 0:7f9842ec56b9 122 rDisplay.clear();
paul80nd 0:7f9842ec56b9 123 leds[0] = leds[1] = leds[2] = leds[3] = 1;
paul80nd 0:7f9842ec56b9 124 }
paul80nd 0:7f9842ec56b9 125 wait_ms(250);
paul80nd 0:7f9842ec56b9 126 lDisplay.write(target);
paul80nd 0:7f9842ec56b9 127 if (target == guess) {
paul80nd 0:7f9842ec56b9 128 rDisplay.write(guess);
paul80nd 0:7f9842ec56b9 129 leds[0] = leds[1] = leds[2] = leds[3] = 0;
paul80nd 0:7f9842ec56b9 130 }
paul80nd 0:7f9842ec56b9 131 wait_ms(250);
paul80nd 0:7f9842ec56b9 132 }
paul80nd 0:7f9842ec56b9 133
paul80nd 0:7f9842ec56b9 134 // End of this game round
paul80nd 0:7f9842ec56b9 135 }
paul80nd 0:7f9842ec56b9 136 }