Arduboy2 Library  6.0.0
Arduboy2Core.h
Go to the documentation of this file.
1 
7 #ifndef ARDUBOY2_CORE_H
8 #define ARDUBOY2_CORE_H
9 
10 #include <Arduino.h>
11 #include <avr/power.h>
12 #include <avr/sleep.h>
13 
14 
15 // main hardware compile flags
16 
17 #if !defined(ARDUBOY_10) && !defined(AB_DEVKIT)
18 /* defaults to Arduboy Release 1.0 if not using a boards.txt file
19  *
20  * we default to Arduboy Release 1.0 if a compile flag has not been
21  * passed to us from a boards.txt file
22  *
23  * if you wish to compile for the devkit without using a boards.txt
24  * file simply comment out the ARDUBOY_10 define and uncomment
25  * the AB_DEVKIT define like this:
26  *
27  * // #define ARDUBOY_10
28  * #define AB_DEVKIT
29  */
30 #define ARDUBOY_10 //< compile for the production Arduboy v1.0
31 // #define AB_DEVKIT //< compile for the official dev kit
32 #endif
33 
34 #define RGB_ON LOW
35 #define RGB_OFF HIGH
37 // ----- Arduboy pins -----
38 #ifdef ARDUBOY_10
39 
40 #define PIN_CS 12 // Display CS Arduino pin number
41 #define CS_PORT PORTD // Display CS port
42 #define CS_BIT PORTD6 // Display CS physical bit number
43 
44 #define PIN_DC 4 // Display D/C Arduino pin number
45 #define DC_PORT PORTD // Display D/C port
46 #define DC_BIT PORTD4 // Display D/C physical bit number
47 
48 #define PIN_RST 6 // Display reset Arduino pin number
49 #define RST_PORT PORTD // Display reset port
50 #define RST_BIT PORTD7 // Display reset physical bit number
51 
52 #define RED_LED 10
53 #define GREEN_LED 11
54 #define BLUE_LED 9
56 #define RED_LED_PORT PORTB
57 #define RED_LED_BIT PORTB6
58 
59 #define GREEN_LED_PORT PORTB
60 #define GREEN_LED_BIT PORTB7
61 
62 #define BLUE_LED_PORT PORTB
63 #define BLUE_LED_BIT PORTB5
64 
65 // bit values for button states
66 // these are determined by the buttonsState() function
67 #define LEFT_BUTTON _BV(5)
68 #define RIGHT_BUTTON _BV(6)
69 #define UP_BUTTON _BV(7)
70 #define DOWN_BUTTON _BV(4)
71 #define A_BUTTON _BV(3)
72 #define B_BUTTON _BV(2)
74 #define PIN_LEFT_BUTTON A2
75 #define LEFT_BUTTON_PORT PORTF
76 #define LEFT_BUTTON_PORTIN PINF
77 #define LEFT_BUTTON_DDR DDRF
78 #define LEFT_BUTTON_BIT PORTF5
79 
80 #define PIN_RIGHT_BUTTON A1
81 #define RIGHT_BUTTON_PORT PORTF
82 #define RIGHT_BUTTON_PORTIN PINF
83 #define RIGHT_BUTTON_DDR DDRF
84 #define RIGHT_BUTTON_BIT PORTF6
85 
86 #define PIN_UP_BUTTON A0
87 #define UP_BUTTON_PORT PORTF
88 #define UP_BUTTON_PORTIN PINF
89 #define UP_BUTTON_DDR DDRF
90 #define UP_BUTTON_BIT PORTF7
91 
92 #define PIN_DOWN_BUTTON A3
93 #define DOWN_BUTTON_PORT PORTF
94 #define DOWN_BUTTON_PORTIN PINF
95 #define DOWN_BUTTON_DDR DDRF
96 #define DOWN_BUTTON_BIT PORTF4
97 
98 #define PIN_A_BUTTON 7
99 #define A_BUTTON_PORT PORTE
100 #define A_BUTTON_PORTIN PINE
101 #define A_BUTTON_DDR DDRE
102 #define A_BUTTON_BIT PORTE6
103 
104 #define PIN_B_BUTTON 8
105 #define B_BUTTON_PORT PORTB
106 #define B_BUTTON_PORTIN PINB
107 #define B_BUTTON_DDR DDRB
108 #define B_BUTTON_BIT PORTB4
109 
110 #define PIN_SPEAKER_1 5
111 #define PIN_SPEAKER_2 13
113 #define SPEAKER_1_PORT PORTC
114 #define SPEAKER_1_DDR DDRC
115 #define SPEAKER_1_BIT PORTC6
116 
117 #define SPEAKER_2_PORT PORTC
118 #define SPEAKER_2_DDR DDRC
119 #define SPEAKER_2_BIT PORTC7
120 // -----------------------
121 
122 // ----- DevKit pins -----
123 #elif defined(AB_DEVKIT)
124 
125 #define PIN_CS 6 // Display CS Arduino pin number
126 #define CS_PORT PORTD // Display CS port
127 #define CS_BIT PORTD7 // Display CS physical bit number
128 
129 #define PIN_DC 4 // Display D/C Arduino pin number
130 #define DC_PORT PORTD // Display D/C port
131 #define DC_BIT PORTD4 // Display D/C physical bit number
132 
133 #define PIN_RST 12 // Display reset Arduino pin number
134 #define RST_PORT PORTD // Display reset port
135 #define RST_BIT PORTD6 // Display reset physical bit number
136 
137 // map all LEDs to the single TX LED on DEVKIT
138 #define RED_LED 17
139 #define GREEN_LED 17
140 #define BLUE_LED 17
141 
142 #define BLUE_LED_PORT PORTB
143 #define BLUE_LED_BIT PORTB0
144 
145 // bit values for button states
146 // these are determined by the buttonsState() function
147 #define LEFT_BUTTON _BV(5)
148 #define RIGHT_BUTTON _BV(2)
149 #define UP_BUTTON _BV(4)
150 #define DOWN_BUTTON _BV(6)
151 #define A_BUTTON _BV(1)
152 #define B_BUTTON _BV(0)
153 
154 // pin values for buttons, probably shouldn't use these
155 #define PIN_LEFT_BUTTON 9
156 #define LEFT_BUTTON_PORT PORTB
157 #define LEFT_BUTTON_PORTIN PINB
158 #define LEFT_BUTTON_DDR DDRB
159 #define LEFT_BUTTON_BIT PORTB5
160 
161 #define PIN_RIGHT_BUTTON 5
162 #define RIGHT_BUTTON_PORT PORTC
163 #define RIGHT_BUTTON_PORTIN PINC
164 #define RIGHT_BUTTON_DDR DDRC
165 #define RIGHT_BUTTON_BIT PORTC6
166 
167 #define PIN_UP_BUTTON 8
168 #define UP_BUTTON_PORT PORTB
169 #define UP_BUTTON_PORTIN PINB
170 #define UP_BUTTON_DDR DDRB
171 #define UP_BUTTON_BIT PORTB4
172 
173 #define PIN_DOWN_BUTTON 10
174 #define DOWN_BUTTON_PORT PORTB
175 #define DOWN_BUTTON_PORTIN PINB
176 #define DOWN_BUTTON_DDR DDRB
177 #define DOWN_BUTTON_BIT PORTB6
178 
179 #define PIN_A_BUTTON A0
180 #define A_BUTTON_PORT PORTF
181 #define A_BUTTON_PORTIN PINF
182 #define A_BUTTON_DDR DDRF
183 #define A_BUTTON_BIT PORTF7
184 
185 #define PIN_B_BUTTON A1
186 #define B_BUTTON_PORT PORTF
187 #define B_BUTTON_PORTIN PINF
188 #define B_BUTTON_DDR DDRF
189 #define B_BUTTON_BIT PORTF6
190 
191 #define PIN_SPEAKER_1 A2
192 #define SPEAKER_1_PORT PORTF
193 #define SPEAKER_1_DDR DDRF
194 #define SPEAKER_1_BIT PORTF5
195 // SPEAKER_2 is purposely not defined for DEVKIT as it could potentially
196 // be dangerous and fry your hardware (because of the devkit wiring).
197 //
198 // Reference: https://github.com/Arduboy/Arduboy/issues/108
199 
200 #endif
201 // --------------------
202 
203 // ----- Pins common on Arduboy and DevKit -----
204 
205 // Unconnected analog input used for noise by generateRandomSeed()
206 #define RAND_SEED_IN A4
207 #define RAND_SEED_IN_PORT PORTF
208 #define RAND_SEED_IN_BIT PORTF1
209 // Value for ADMUX to read the random seed pin: 2.56V reference, ADC1
210 #define RAND_SEED_IN_ADMUX (_BV(REFS0) | _BV(REFS1) | _BV(MUX0))
211 
212 // SPI interface
213 #define SPI_MISO_PORT PORTB
214 #define SPI_MISO_BIT PORTB3
215 
216 #define SPI_MOSI_PORT PORTB
217 #define SPI_MOSI_BIT PORTB2
218 
219 #define SPI_SCK_PORT PORTB
220 #define SPI_SCK_BIT PORTB1
221 
222 #define SPI_SS_PORT PORTB
223 #define SPI_SS_BIT PORTB0
224 // --------------------
225 
226 // OLED hardware (SSD1306)
227 
228 #define OLED_PIXELS_INVERTED 0xA7 // All pixels inverted
229 #define OLED_PIXELS_NORMAL 0xA6 // All pixels normal
230 
231 #define OLED_ALL_PIXELS_ON 0xA5 // all pixels on
232 #define OLED_PIXELS_FROM_RAM 0xA4 // pixels mapped to display RAM contents
233 
234 #define OLED_VERTICAL_FLIPPED 0xC0 // reversed COM scan direction
235 #define OLED_VERTICAL_NORMAL 0xC8 // normal COM scan direction
236 
237 #define OLED_HORIZ_FLIPPED 0xA0 // reversed segment re-map
238 #define OLED_HORIZ_NORMAL 0xA1 // normal segment re-map
239 
240 // -----
241 
242 #define WIDTH 128
243 #define HEIGHT 64
245 #define COLUMN_ADDRESS_END (WIDTH - 1) & 127 // 128 pixels wide
246 #define PAGE_ADDRESS_END ((HEIGHT/8)-1) & 7 // 8 pages high
247 
304 #define ARDUBOY_NO_USB int main() __attribute__ ((OS_main)); \
305 int main() { \
306  Arduboy2NoUSB::mainNoUSB(); \
307  return 0; \
308 }
309 
310 // A replacement for the Arduino main() function that eliminates the USB code.
311 // Used by the ARDUBOY_NO_USB macro.
312 class Arduboy2NoUSB
313 {
314  friend int main();
315 
316  private:
317  static void mainNoUSB();
318 };
319 
320 
335 class Arduboy2Core : public Arduboy2NoUSB
336 {
337  friend class Arduboy2Ex;
338 
339  public:
340 
351  static void idle();
352 
367  static void LCDDataMode();
368 
391  static void LCDCommandMode();
392 
406  static void SPItransfer(uint8_t data);
407 
428  static uint8_t SPItransferAndRead(uint8_t data);
429 
441  static void displayOff();
442 
457  static void displayOn();
458 
464  static constexpr uint8_t width() { return WIDTH; }
465 
471  static constexpr uint8_t height() { return HEIGHT; }
472 
486  static uint8_t buttonsState();
487 
518  static void paint8Pixels(uint8_t pixels);
519 
536  static void paintScreen(const uint8_t *image);
537 
560  static void paintScreen(uint8_t image[], bool clear = false);
561 
569  static void blank();
570 
586  static void invert(bool inverse);
587 
607  static void allPixelsOn(bool on);
608 
625  static void flipVertical(bool flipped);
626 
643  static void flipHorizontal(bool flipped);
644 
659  static void sendLCDCommand(uint8_t command);
660 
693  static void setRGBled(uint8_t red, uint8_t green, uint8_t blue);
694 
715  static void setRGBled(uint8_t color, uint8_t val);
716 
717 
728  static void freeRGBled();
729 
773  static void digitalWriteRGB(uint8_t red, uint8_t green, uint8_t blue);
774 
791  static void digitalWriteRGB(uint8_t color, uint8_t val);
792 
815  static void boot();
816 
836  static void safeMode();
837 
855  static unsigned long generateRandomSeed();
856 
868  static void delayShort(uint16_t ms) __attribute__ ((noinline));
869 
886  static void exitToBootloader();
887 
888  protected:
889  // internals
890  static void setCPUSpeed8MHz();
891  static void bootSPI();
892  static void bootOLED();
893  static void bootPins();
894  static void bootPowerSaving();
895 
896  static const PROGMEM uint8_t lcdBootProgram[];
897 };
898 
899 #endif
Arduboy2Core::boot
static void boot()
Initialize the Arduboy's hardware.
Definition: Arduboy2Core.cpp:81
Arduboy2Core::sendLCDCommand
static void sendLCDCommand(uint8_t command)
Send a single command byte to the display.
Definition: Arduboy2Core.cpp:406
Arduboy2Core
Lower level functions generally dealing directly with the hardware.
Definition: Arduboy2Core.h:336
Arduboy2Core::displayOff
static void displayOff()
Turn the display off.
Definition: Arduboy2Core.cpp:295
Arduboy2Core::idle
static void idle()
Idle the CPU to save power.
Definition: Arduboy2Core.cpp:278
Arduboy2Core::flipVertical
static void flipVertical(bool flipped)
Flip the display vertically or set it back to normal.
Definition: Arduboy2Core.cpp:428
Arduboy2Core::generateRandomSeed
static unsigned long generateRandomSeed()
Create a seed suitable for use with a pseudorandom number generator.
Definition: Arduboy2Core.cpp:564
Arduboy2Core::paintScreen
static void paintScreen(const uint8_t *image)
Paints an entire image directly to the display from program memory.
Definition: Arduboy2Core.cpp:319
Arduboy2Core::height
static constexpr uint8_t height()
Get the height of the display in pixels.
Definition: Arduboy2Core.h:471
Arduboy2Core::width
static constexpr uint8_t width()
Get the width of the display in pixels.
Definition: Arduboy2Core.h:464
Arduboy2Core::blank
static void blank()
Blank the display screen by setting all pixels off.
Definition: Arduboy2Core.cpp:400
Arduboy2Core::safeMode
static void safeMode()
Allow upload when the bootloader "magic number" could be corrupted.
Definition: Arduboy2Core.cpp:259
WIDTH
#define WIDTH
Definition: Arduboy2Core.h:242
Arduboy2Core::SPItransferAndRead
static uint8_t SPItransferAndRead(uint8_t data)
Transfer a byte to, and read a byte from, the SPI bus.
Definition: Arduboy2Core.cpp:253
Arduboy2Core::invert
static void invert(bool inverse)
Invert the entire display or set it back to normal.
Definition: Arduboy2Core.cpp:415
Arduboy2Core::displayOn
static void displayOn()
Turn the display on.
Definition: Arduboy2Core.cpp:306
Arduboy2Core::buttonsState
static uint8_t buttonsState()
Get the current state of all buttons as a bitmask.
Definition: Arduboy2Core.cpp:536
Arduboy2Core::flipHorizontal
static void flipHorizontal(bool flipped)
Flip the display horizontally or set it back to normal.
Definition: Arduboy2Core.cpp:434
Arduboy2Core::exitToBootloader
static void exitToBootloader()
Exit the sketch and start the bootloader.
Definition: Arduboy2Core.cpp:587
Arduboy2Core::setRGBled
static void setRGBled(uint8_t red, uint8_t green, uint8_t blue)
Set the light output of the RGB LED.
Definition: Arduboy2Core.cpp:441
Arduboy2Core::LCDDataMode
static void LCDDataMode()
Put the display into data mode.
Definition: Arduboy2Core.cpp:220
Arduboy2Core::digitalWriteRGB
static void digitalWriteRGB(uint8_t red, uint8_t green, uint8_t blue)
Set the RGB LEDs digitally, to either fully on or fully off.
Definition: Arduboy2Core.cpp:496
Arduboy2Core::delayShort
static void delayShort(uint16_t ms) __attribute__((noinline))
Delay for the number of milliseconds, specified as a 16 bit value.
Definition: Arduboy2Core.cpp:582
Arduboy2Core::paint8Pixels
static void paint8Pixels(uint8_t pixels)
Paint 8 pixels vertically to the display.
Definition: Arduboy2Core.cpp:314
Arduboy2Core::allPixelsOn
static void allPixelsOn(bool on)
Turn all display pixels on or display the buffer contents.
Definition: Arduboy2Core.cpp:422
Arduboy2Core::freeRGBled
static void freeRGBled()
Relinquish analog control of the RGB LED.
Definition: Arduboy2Core.cpp:487
HEIGHT
#define HEIGHT
Definition: Arduboy2Core.h:243
Arduboy2Core::LCDCommandMode
static void LCDCommandMode()
Put the display into command mode.
Definition: Arduboy2Core.cpp:225
Arduboy2Core::SPItransfer
static void SPItransfer(uint8_t data)
Transfer a byte to the display.
Definition: Arduboy2Core.cpp:239