|
| 1 | +#include "HT16K33.h" |
| 2 | +#include "Tetris.h" |
| 3 | +#include "Snake.h" |
| 4 | +#include "Paint.h" |
| 5 | + |
| 6 | +// ADC clock configurations |
| 7 | +#define ADPS_16 _BV(ADPS2) |
| 8 | +#define ADPS_32 _BV(ADPS2)|_BV(ADPS0) |
| 9 | +#define ADPS_128 _BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0) |
| 10 | + |
| 11 | +// Buttons: bits 0-6 in the first byte of the key-scan matrix |
| 12 | +#define LEFT 6 |
| 13 | +#define SELECT 1 |
| 14 | +#define DOWN 2 |
| 15 | +#define UP 5 |
| 16 | +#define RIGHT 0 |
| 17 | +#define BRIGHTNESS_UP 4 |
| 18 | +#define BRIGHTNESS_DOWN 3 |
| 19 | + |
| 20 | +// Objects |
| 21 | +HT16K33 ht(0x70); // I2C address of the HT16K33 IC |
| 22 | +Tetris tetris; |
| 23 | +Snake snake; |
| 24 | +Paint paint(3,3); // initilize cursor position to (3,3) |
| 25 | + |
| 26 | +// Mode: 0-Undecided; 1-Tetris; 2-Snake; 3-Paint |
| 27 | +uint8_t mode=0; |
| 28 | + |
| 29 | +// Interrupt Variables |
| 30 | +uint8_t interruptCounter2Hz = 0; |
| 31 | + |
| 32 | +ISR(TIMER1_OVF_vect){ |
| 33 | + // things that need to be done at 50Hz |
| 34 | + ht.readButtons(); |
| 35 | + processButtons(); |
| 36 | + switch(mode) { |
| 37 | + case 0: |
| 38 | + ht.storeToBuffer(getMenu()); |
| 39 | + break; |
| 40 | + case 1: |
| 41 | + ht.storeToBuffer(tetris.getActiveBoard()); |
| 42 | + break; |
| 43 | + case 2: |
| 44 | + ht.storeToBuffer(snake.getActiveBoard()); |
| 45 | + break; |
| 46 | + case 3: |
| 47 | + ht.storeToBuffer(paint.getActiveCanvas()); |
| 48 | + } |
| 49 | + ht.refreshDisplay(); |
| 50 | + |
| 51 | + // things that happen less frequently |
| 52 | + interruptCounter2Hz++; |
| 53 | + if (interruptCounter2Hz==25) { |
| 54 | + interruptCounter2Hz=0; |
| 55 | + paint.flashCursor(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void processButtons() { |
| 60 | + // These two buttons are fixed |
| 61 | + if (ht.allowToMove(BRIGHTNESS_DOWN,25,6) && ht.getButtonHoldTime(SELECT)==0) ht.decreaseBrightness(); |
| 62 | + if (ht.allowToMove(BRIGHTNESS_UP,25,6) && ht.getButtonHoldTime(SELECT)==0) ht.increaseBrightness(); |
| 63 | + |
| 64 | + // change to the previous/next program by pressing holding select and press the brightness control buttons |
| 65 | + if (mode!=0 && ht.getButtonHoldTime(SELECT)>10) { |
| 66 | + if (ht.getButtonFirstPress(BRIGHTNESS_UP)) { |
| 67 | + changeOption(-1); |
| 68 | + changeMode(); |
| 69 | + } |
| 70 | + else if (ht.getButtonFirstPress(BRIGHTNESS_DOWN)){ |
| 71 | + changeOption(1); |
| 72 | + changeMode(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // control for different programs |
| 77 | + switch(mode) { |
| 78 | + case 0: |
| 79 | + if (ht.getButtonFirstPress(LEFT)) changeOption(-1); |
| 80 | + if (ht.getButtonFirstPress(RIGHT)) changeOption(1); |
| 81 | + if (ht.getButtonFirstPress(SELECT)) changeMode(); |
| 82 | + break; |
| 83 | + case 1: |
| 84 | + if (tetris.gameRunning) { |
| 85 | + if (ht.allowToMove(UP,100,4)) tetris.rotatePiece(); |
| 86 | + if (ht.allowToMove(DOWN,15,2)) tetris.movePiece(0,1); |
| 87 | + if (ht.allowToMove(LEFT,15,2)) tetris.movePiece(-1,0); |
| 88 | + if (ht.allowToMove(RIGHT,15,2)) tetris.movePiece(1,0); |
| 89 | + if (ht.getButtonFirstPress(SELECT)) tetris.dropPiece(); |
| 90 | + } |
| 91 | + else { |
| 92 | + if (ht.getButtonFirstPress(SELECT)) tetris.init(); |
| 93 | + } |
| 94 | + break; |
| 95 | + case 2: |
| 96 | + if (snake.gameRunning) { |
| 97 | + if (ht.getButtonFirstPress(UP)) snake.changeDirection(0,-1); |
| 98 | + if (ht.getButtonFirstPress(DOWN)) snake.changeDirection(0,1); |
| 99 | + if (ht.getButtonFirstPress(LEFT)) snake.changeDirection(-1,0); |
| 100 | + if (ht.getButtonFirstPress(RIGHT)) snake.changeDirection(1,0); |
| 101 | + } |
| 102 | + else { |
| 103 | + if (ht.getButtonFirstPress(SELECT)) snake.init(); |
| 104 | + } |
| 105 | + break; |
| 106 | + case 3: |
| 107 | + if (ht.allowToMove(UP,25,2)) paint.moveCursor(0,-1); |
| 108 | + if (ht.allowToMove(DOWN,25,2)) paint.moveCursor(0,1); |
| 109 | + if (ht.allowToMove(LEFT,25,2)) paint.moveCursor(-1,0); |
| 110 | + if (ht.allowToMove(RIGHT,25,2)) paint.moveCursor(1,0); |
| 111 | + if (ht.getButtonFirstPress(SELECT)) paint.draw(); |
| 112 | + if (ht.getButtonHoldTime(SELECT)==150) paint.clearCanvas(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void setup(){ |
| 117 | + // Set randomSeed; the first few random() are not used because they never change |
| 118 | + randomSeed(analogRead(A0)); |
| 119 | + for (uint8_t i=0;i<4;i++) random(); |
| 120 | + |
| 121 | + // Initiate objects |
| 122 | + Serial.begin(115200); |
| 123 | + ht.init(); |
| 124 | + tetris.init(); |
| 125 | + snake.init(); |
| 126 | + |
| 127 | + //ADC: 16MHz/16=1MHz |
| 128 | + ADCSRA &= ~ADPS_128; // clean out ADPS |
| 129 | + ADCSRA |= ADPS_32; // set Division Factor |
| 130 | + |
| 131 | + // Timer1 overflow interrupt setup |
| 132 | + TCNT1 = 0; // reset the counter |
| 133 | + ICR1 = 19999; // top value of the counter (16*10^6Hz/2000000*20000us/8-1) -> 1/20000us = 50Hz |
| 134 | + TCCR1A = 0; // clear TCCR1A (not used) |
| 135 | + TCCR1B = _BV(WGM13) | _BV(CS11); // mode 8(PWM), set prescalar=8; |
| 136 | + TIMSK1 = _BV(TOIE1); // Enable Overflow Interrupt |
| 137 | +} |
| 138 | + |
| 139 | +void loop(){ |
| 140 | + // For both the tetris and snake, they have a function called "run" as the main game loop. |
| 141 | + // For the paint program, all the actions are handled in the timer interrupt, so there is nothing to do in the main loop |
| 142 | + switch(mode) { |
| 143 | + case 0: |
| 144 | + break; |
| 145 | + case 1: |
| 146 | + tetris.run(); break; |
| 147 | + case 2: |
| 148 | + snake.run(); break; |
| 149 | + case 3: |
| 150 | + ; |
| 151 | + } |
| 152 | +} |
0 commit comments