Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Author: Peter Csurgay
- * Created: 2018-02-10
- */
- const bool serial = false;
- uint8_t mode = 2; // 2: binary, 3: ternary guessing game
- const uint8_t stb = 2;
- const uint8_t clk = 3;
- const uint8_t dio = 4;
- const uint8_t numLed7[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- const uint8_t lookups[][2] = { {'0',numLed7[0]}, {'1',numLed7[1]}, {'2',numLed7[2]}, {'3',numLed7[3]}, {'4',numLed7[4]},
- {'5',numLed7[5]}, {'6',numLed7[6]}, {'7',numLed7[7]}, {'8',numLed7[8]}, {'9',numLed7[9]},
- {'Y',110}, {'E',121}, {'S',109}, {'P',115}, {'t',120}, {'C',57}, {'r',80}, {'O',63}, {'A', 119},
- {'b', 124}, {'i', 16}, {'n', 84}, {'G', 61}
- };
- void low() {
- digitalWrite(stb, LOW);
- }
- void high() {
- digitalWrite(stb, HIGH);
- delay(1);
- }
- void out(uint8_t data) {
- shiftOut(dio, clk, LSBFIRST, data);
- }
- void command(uint8_t data) {
- low(); out(data); high();
- }
- void addr(uint8_t a, uint8_t data) {
- low(); out(a); out(data); high();
- }
- void resetDisplay()
- {
- command(0x40); // autoincrement
- low();
- out(0xc0); // starting address 0
- for(uint8_t i = 0; i < 16; i++) out(0x00);
- high();
- }
- uint8_t readButtons(void)
- {
- uint8_t buttons = 0;
- low();
- out(0x42);
- pinMode(dio, INPUT);
- for (uint8_t i = 0; i < 4; i++) {
- uint8_t v = shiftIn(dio, clk, LSBFIRST) << i;
- buttons |= v;
- }
- pinMode(dio, OUTPUT);
- high();
- return buttons;
- }
- uint8_t cPos(uint8_t pos) {
- return 0xc0 + (pos<<1);
- }
- void setLed(uint8_t value, uint8_t pos)
- {
- // pinMode(dio, OUTPUT); // default mode anyway
- // command(0x44); // already set single mode, so this is default
- low();
- addr(cPos(pos) + 1, value);
- high();
- }
- void ledChase() {
- for (uint8_t i=0; i<8; i++) {
- addr(cPos(i)+1, 1);
- delay(70);
- resetDisplay();
- }
- }
- uint8_t lookup( char in ) {
- for (uint8_t i=0; i<sizeof(lookups)/sizeof(lookups[0]); i++)
- if (lookups[i][0]==in) return lookups[i][1];
- return 0xff;
- }
- void displayString(String ds, uint8_t pos, int dir) {
- // Serial.println(ds);
- for (uint8_t i = 0; i<ds.length(); i++) {
- uint8_t ind = i; if (dir==-1) ind = ds.length()-1-i;
- addr(cPos(pos+dir*i), // numLed7[ (int)(ds[ind])-48 ]);
- lookup( ds[ind]) );
- }
- }
- void displayDecimal(int decimal, uint8_t pos, int dir) { // direction: 1 l2r, -1 r2l
- displayString( String(decimal), pos, dir);
- }
- void sLog(String label, String value) {
- if (serial) { Serial.print(label); Serial.print(": "); Serial.println(value); }
- }
- long gameTime;
- int randDecimal, weightDecimal, steps, gameNo, saveButtons, idle;
- int score;
- uint8_t ledStates[] = { 0,0,0,0,0,0,0,0 };
- uint8_t ledLights[] = { 0,0,0,0,0,0,0,0 };
- void generateDecimal() {
- int res = 0, weight = 0;
- int topDigit = 7; if (mode==3) topDigit = 5;
- for (uint8_t i=0; i<=topDigit; i++) {
- uint8_t r = random(mode);
- int power=1; for (int p=1; p<=i; p++) power*=mode;
- res += power * r;
- weight += r;
- }
- randDecimal = res;
- weightDecimal = weight;
- sLog("randDecimal", String(randDecimal));
- }
- void newGame() {
- resetDisplay();
- for (uint8_t i=0; i<8; i++) ledStates[i]=0;
- generateDecimal();
- displayDecimal( randDecimal, 0, 1 );
- steps = 0;
- gameNo++;
- gameTime = millis();
- idle = 1;
- }
- void setup() {
- delay(1000);
- Serial.begin(9600);
- score = 0;
- gameNo = 0;
- randomSeed(analogRead(0));
- pinMode(stb, OUTPUT);
- pinMode(clk, OUTPUT);
- pinMode(dio, OUTPUT);
- command(0x89); // intensity min
- ledChase();
- command(0x44); // single address mode
- newGame();
- }
- void loop()
- {
- for (int i=0; i<8; i++) {
- if (mode==2) {
- ledLights[i] = ledStates[i];
- setLed(ledLights[i], i);
- }
- else if (mode==3) {
- if (ledStates[i]==2) ledLights[i] = 1-ledLights[i];
- else ledLights[i] = ledStates[i];
- setLed(ledLights[i], i);
- }
- }
- uint8_t buttons = readButtons();
- if (idle && buttons) { saveButtons = buttons; idle = 0; }
- if (!idle) { if (buttons > saveButtons) saveButtons = buttons; }
- if (buttons==0) { buttons = saveButtons; saveButtons = 0; idle = 1; }
- if (buttons > 0 && idle) {
- sLog("Buttons", String(buttons));
- steps++;
- if (buttons == 129) {
- steps--;
- ledChase(); gameNo=0; score=0;
- resetDisplay();
- displayString("rEStArt",0,1);
- delay(1000);
- newGame();
- } else if (buttons == 65) {
- steps--;
- ledChase();
- resetDisplay();
- displayString("t",0,1);
- displayDecimal(millis()/1000, 7, -1);
- delay(1000);
- } else if (buttons == 3) {
- steps--;
- ledChase();
- resetDisplay();
- displayString("binArY",0,1);
- mode = 2;
- delay(1000);
- } else if (buttons == 5) {
- steps--;
- ledChase();
- resetDisplay();
- displayString("tErnArY",0,1);
- mode = 3;
- delay(1000);
- } else if (buttons == 9) {
- steps--;
- ledChase();
- resetDisplay();
- displayString("GEnErAtE",0,1);
- delay(1000);
- newGame();
- } else {
- for (int i=0; i<8; i++) {
- int power=1; for(int p=1; p<=i; p++) power*=2;
- if (buttons == power) {
- ledStates[i]++; if (ledStates[i]>=mode) ledStates[i]=0;
- }
- }
- }
- int guess = 0;
- resetDisplay();
- displayDecimal( randDecimal, 0, 1);
- for(uint8_t pos = 0; pos < 8; pos++) {
- int power=1; for(int p=1; p<=7-pos; p++) power*=mode;
- guess += power * ledStates[pos];
- }
- Serial.println( guess );
- displayDecimal( guess, 7, -1 );
- if (randDecimal == guess) {
- sLog("Guess", "Bingo");
- sLog("gameNo", String(gameNo));
- sLog("gameTime", String(gameTime));
- sLog("millis()", String(millis()));
- long gameDuration = (int)((millis() - gameTime) / 1000);
- delay(500);
- score = ( (gameNo-1)*score + (steps-weightDecimal)*5+3*gameDuration ) / gameNo;
- sLog("steps", String(steps));
- sLog("weightDecimal", String(weightDecimal));
- sLog("gameDuration", String(gameDuration));
- sLog("score", String(score));
- /* resetDisplay();
- displayString("PrS", 0, 1);
- displayDecimal(weightDecimal, 4, 1);
- displayDecimal(steps, 7, -1);
- delay(2000);
- resetDisplay();
- displayString("SEC", 0, 1);
- displayDecimal(gameDuration, 7, -1);
- delay(2000);
- */ resetDisplay();
- displayString("SCOrE", 0, 1);
- delay(1000);
- resetDisplay();
- displayDecimal(gameNo, 0, 1);
- displayDecimal(score, 7, -1);
- delay(2000);
- newGame();
- }
- }
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement