Louis Mayencourt / Mbed OS NRFBOY
Committer:
lmayencou
Date:
Sat Jan 07 12:43:36 2017 +0000
Revision:
3:df305b314063
Parent:
2:e3ef9f476913
Child:
7:fb7e549d1cf6
add frame timer - game working - bug with high scores

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lmayencou 3:df305b314063 1 #include "mbed.h"
lmayencou 1:c53e766082b4 2 #include "abstractarduboy.h"
lmayencou 1:c53e766082b4 3
lmayencou 3:df305b314063 4 Ticker timer;
lmayencou 1:c53e766082b4 5 SPI _spi(p30, p31, p29); // mosi, miso, sclk
lmayencou 1:c53e766082b4 6
lmayencou 1:c53e766082b4 7 DigitalOut _cs(p3);
lmayencou 1:c53e766082b4 8 DigitalOut _dc(p28);
lmayencou 1:c53e766082b4 9 DigitalOut _rst(p4);
lmayencou 1:c53e766082b4 10
lmayencou 3:df305b314063 11 DigitalIn _left(BUTTON1);
lmayencou 3:df305b314063 12 DigitalIn _right(BUTTON4);
lmayencou 3:df305b314063 13 DigitalIn _A(BUTTON2);
lmayencou 3:df305b314063 14 DigitalIn _B(BUTTON3);
lmayencou 3:df305b314063 15
lmayencou 3:df305b314063 16 unsigned long _millis;
lmayencou 3:df305b314063 17
lmayencou 3:df305b314063 18 void gameTimerCallback(void)
lmayencou 3:df305b314063 19 {
lmayencou 3:df305b314063 20 _millis++;
lmayencou 3:df305b314063 21 }
lmayencou 3:df305b314063 22
lmayencou 2:e3ef9f476913 23 class MbedBoy : public AbstractArduboy, public Stream
lmayencou 1:c53e766082b4 24 {
lmayencou 1:c53e766082b4 25 public:
lmayencou 1:c53e766082b4 26 MbedBoy(){};
lmayencou 1:c53e766082b4 27
lmayencou 1:c53e766082b4 28 void start()
lmayencou 1:c53e766082b4 29 {
lmayencou 3:df305b314063 30 // init systime
lmayencou 3:df305b314063 31 timer.attach(gameTimerCallback, 0.001); /* Blink LED every second */
lmayencou 3:df305b314063 32
lmayencou 1:c53e766082b4 33 // init SPI
lmayencou 1:c53e766082b4 34 _spi.format(8,3);
lmayencou 1:c53e766082b4 35 _spi.frequency(1000000);
lmayencou 1:c53e766082b4 36
lmayencou 3:df305b314063 37 // init pin
lmayencou 3:df305b314063 38 _left.mode(PullUp);
lmayencou 3:df305b314063 39 _right.mode(PullUp);
lmayencou 3:df305b314063 40 _A.mode(PullUp);
lmayencou 3:df305b314063 41 _B.mode(PullUp);
lmayencou 3:df305b314063 42
lmayencou 1:c53e766082b4 43 // init pin LCD
lmayencou 1:c53e766082b4 44 _dc = 0;
lmayencou 1:c53e766082b4 45 _cs = 0;
lmayencou 1:c53e766082b4 46 _rst = 1;
lmayencou 1:c53e766082b4 47 wait_ms(1);
lmayencou 1:c53e766082b4 48 _rst = 0;
lmayencou 1:c53e766082b4 49 wait_ms(10);
lmayencou 1:c53e766082b4 50 _rst = 1;
lmayencou 1:c53e766082b4 51
lmayencou 1:c53e766082b4 52 bootLCD();
lmayencou 1:c53e766082b4 53 }
lmayencou 1:c53e766082b4 54
lmayencou 1:c53e766082b4 55 long getTime()
lmayencou 1:c53e766082b4 56 {
lmayencou 3:df305b314063 57 return _millis;
lmayencou 1:c53e766082b4 58 }
lmayencou 1:c53e766082b4 59
lmayencou 1:c53e766082b4 60 void drawScreen(const unsigned char *image)
lmayencou 1:c53e766082b4 61 {
lmayencou 1:c53e766082b4 62 for (int i = 0; i < (HEIGHT*WIDTH)/8; i++)
lmayencou 1:c53e766082b4 63 {
lmayencou 1:c53e766082b4 64 _spi.write(image[i]);
lmayencou 1:c53e766082b4 65 }
lmayencou 1:c53e766082b4 66 }
lmayencou 1:c53e766082b4 67
lmayencou 1:c53e766082b4 68 uint8_t getInput()
lmayencou 1:c53e766082b4 69 {
lmayencou 3:df305b314063 70 return !_left + ((!_right) << 1) + ((!_A) << 4) + ((!_B) << 5);
lmayencou 1:c53e766082b4 71 }
lmayencou 1:c53e766082b4 72
lmayencou 1:c53e766082b4 73 void LCDCommandMode()
lmayencou 1:c53e766082b4 74 {
lmayencou 1:c53e766082b4 75 _cs = 1;
lmayencou 1:c53e766082b4 76 _dc = 0;
lmayencou 1:c53e766082b4 77 _cs = 0;
lmayencou 1:c53e766082b4 78 }
lmayencou 1:c53e766082b4 79
lmayencou 1:c53e766082b4 80 void LCDDataMode()
lmayencou 1:c53e766082b4 81 {
lmayencou 1:c53e766082b4 82 _dc = 1;
lmayencou 1:c53e766082b4 83 _cs = 0;
lmayencou 1:c53e766082b4 84 }
lmayencou 1:c53e766082b4 85
lmayencou 1:c53e766082b4 86 void bootLCD()
lmayencou 1:c53e766082b4 87 {
lmayencou 1:c53e766082b4 88 LCDCommandMode();
lmayencou 1:c53e766082b4 89 _spi.write(0xAE); // Display Off
lmayencou 1:c53e766082b4 90 _spi.write(0XD5); // Set Display Clock Divisor v
lmayencou 1:c53e766082b4 91 _spi.write(0xF0); // 0x80 is default
lmayencou 1:c53e766082b4 92 _spi.write(0xA8); // Set Multiplex Ratio v
lmayencou 1:c53e766082b4 93 _spi.write(0x3F);
lmayencou 1:c53e766082b4 94 _spi.write(0xD3); // Set Display Offset v
lmayencou 1:c53e766082b4 95 _spi.write(0x0);
lmayencou 1:c53e766082b4 96 _spi.write(0x40); // Set Start Line (0)
lmayencou 1:c53e766082b4 97 _spi.write(0x8D); // Charge Pump Setting v
lmayencou 1:c53e766082b4 98 _spi.write(0x14); // Enable
lmayencou 1:c53e766082b4 99 // why are we running this next pair twice?
lmayencou 1:c53e766082b4 100 _spi.write(0x20); // Set Memory Mode v
lmayencou 1:c53e766082b4 101 _spi.write(0x00); // Horizontal Addressing
lmayencou 1:c53e766082b4 102 _spi.write(0xA1); // Set Segment Re-map (A0) | (b0001)
lmayencou 1:c53e766082b4 103 _spi.write(0xC8); // Set COM Output Scan Direction
lmayencou 1:c53e766082b4 104 _spi.write(0xDA); // Set COM Pins v
lmayencou 1:c53e766082b4 105 _spi.write(0x12);
lmayencou 1:c53e766082b4 106 _spi.write(0x81); // Set Contrast v
lmayencou 1:c53e766082b4 107 _spi.write(0xCF);
lmayencou 1:c53e766082b4 108 _spi.write(0xD9); // Set Precharge
lmayencou 1:c53e766082b4 109 _spi.write(0xF1);
lmayencou 1:c53e766082b4 110 _spi.write(0xDB); // Set VCom Detect
lmayencou 1:c53e766082b4 111 _spi.write(0x40);
lmayencou 1:c53e766082b4 112 _spi.write(0xA4); // Entire Display ON
lmayencou 1:c53e766082b4 113 _spi.write(0xA6); // Set normal/inverse display
lmayencou 1:c53e766082b4 114 _spi.write(0xAF); // Display On
lmayencou 1:c53e766082b4 115
lmayencou 1:c53e766082b4 116 LCDCommandMode();
lmayencou 1:c53e766082b4 117 _spi.write(0x20); // set display mode
lmayencou 1:c53e766082b4 118 _spi.write(0x00); // horizontal addressing mode
lmayencou 1:c53e766082b4 119
lmayencou 1:c53e766082b4 120 _spi.write(0x21); // set col address
lmayencou 1:c53e766082b4 121 _spi.write(0x00);
lmayencou 1:c53e766082b4 122 _spi.write(COLUMN_ADDRESS_END);
lmayencou 1:c53e766082b4 123
lmayencou 1:c53e766082b4 124 _spi.write(0x22); // set page address
lmayencou 1:c53e766082b4 125 _spi.write(0x00);
lmayencou 1:c53e766082b4 126 _spi.write(PAGE_ADDRESS_END);
lmayencou 1:c53e766082b4 127
lmayencou 1:c53e766082b4 128 LCDDataMode();
lmayencou 1:c53e766082b4 129 }
lmayencou 1:c53e766082b4 130
lmayencou 1:c53e766082b4 131 // Stream implementation - provides printf() interface
lmayencou 1:c53e766082b4 132 // You would otherwise be forced to use writeChar()
lmayencou 1:c53e766082b4 133 virtual int _putc(int value) { return writeChar(value); };
lmayencou 1:c53e766082b4 134 virtual int _getc() { return -1; };
lmayencou 1:c53e766082b4 135 };