Go to the documentation of this file.
7 #ifndef ARDUBOY2_CORE_H
8 #define ARDUBOY2_CORE_H
11 #include <avr/power.h>
12 #include <avr/sleep.h>
17 #if !defined(ARDUBOY_10) && !defined(AB_DEVKIT)
30 #define ARDUBOY_10 //< compile for the production Arduboy v1.0
37 // ----- Arduboy pins -----
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
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
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
56 #define RED_LED_PORT PORTB
57 #define RED_LED_BIT PORTB6
59 #define GREEN_LED_PORT PORTB
60 #define GREEN_LED_BIT PORTB7
62 #define BLUE_LED_PORT PORTB
63 #define BLUE_LED_BIT PORTB5
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
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
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
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
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
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
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
117 #define SPEAKER_2_PORT PORTC
118 #define SPEAKER_2_DDR DDRC
119 #define SPEAKER_2_BIT PORTC7
123 #elif defined(AB_DEVKIT)
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
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
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
142 #define BLUE_LED_PORT PORTB
143 #define BLUE_LED_BIT PORTB0
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)
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
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
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
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
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
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
191 #define PIN_SPEAKER_1 A2
192 #define SPEAKER_1_PORT PORTF
193 #define SPEAKER_1_DDR DDRF
194 #define SPEAKER_1_BIT PORTF5
206 #define RAND_SEED_IN A4
207 #define RAND_SEED_IN_PORT PORTF
208 #define RAND_SEED_IN_BIT PORTF1
210 #define RAND_SEED_IN_ADMUX (_BV(REFS0) | _BV(REFS1) | _BV(MUX0))
213 #define SPI_MISO_PORT PORTB
214 #define SPI_MISO_BIT PORTB3
216 #define SPI_MOSI_PORT PORTB
217 #define SPI_MOSI_BIT PORTB2
219 #define SPI_SCK_PORT PORTB
220 #define SPI_SCK_BIT PORTB1
222 #define SPI_SS_PORT PORTB
223 #define SPI_SS_BIT PORTB0
228 #define OLED_PIXELS_INVERTED 0xA7 // All pixels inverted
229 #define OLED_PIXELS_NORMAL 0xA6 // All pixels normal
231 #define OLED_ALL_PIXELS_ON 0xA5 // all pixels on
232 #define OLED_PIXELS_FROM_RAM 0xA4 // pixels mapped to display RAM contents
234 #define OLED_VERTICAL_FLIPPED 0xC0 // reversed COM scan direction
235 #define OLED_VERTICAL_NORMAL 0xC8 // normal COM scan direction
237 #define OLED_HORIZ_FLIPPED 0xA0 // reversed segment re-map
238 #define OLED_HORIZ_NORMAL 0xA1 // normal segment re-map
245 #define COLUMN_ADDRESS_END (WIDTH - 1) & 127 // 128 pixels wide
246 #define PAGE_ADDRESS_END ((HEIGHT/8)-1) & 7 // 8 pages high
304 #define ARDUBOY_NO_USB int main() __attribute__ ((OS_main)); \
306 Arduboy2NoUSB::mainNoUSB(); \
317 static void mainNoUSB();
337 friend class Arduboy2Ex;
560 static void paintScreen(uint8_t image[],
bool clear =
false);
586 static void invert(
bool inverse);
693 static void setRGBled(uint8_t red, uint8_t green, uint8_t blue);
715 static void setRGBled(uint8_t color, uint8_t val);
868 static void delayShort(uint16_t ms) __attribute__ ((noinline));
890 static void setCPUSpeed8MHz();
891 static void bootSPI();
892 static void bootOLED();
893 static void bootPins();
894 static void bootPowerSaving();
896 static const PROGMEM uint8_t lcdBootProgram[];
static void boot()
Initialize the Arduboy's hardware.
static void sendLCDCommand(uint8_t command)
Send a single command byte to the display.
Lower level functions generally dealing directly with the hardware.
static void displayOff()
Turn the display off.
static void idle()
Idle the CPU to save power.
static void flipVertical(bool flipped)
Flip the display vertically or set it back to normal.
static unsigned long generateRandomSeed()
Create a seed suitable for use with a pseudorandom number generator.
static void paintScreen(const uint8_t *image)
Paints an entire image directly to the display from program memory.
static constexpr uint8_t height()
Get the height of the display in pixels.
static constexpr uint8_t width()
Get the width of the display in pixels.
static void blank()
Blank the display screen by setting all pixels off.
static void safeMode()
Allow upload when the bootloader "magic number" could be corrupted.
static uint8_t SPItransferAndRead(uint8_t data)
Transfer a byte to, and read a byte from, the SPI bus.
static void invert(bool inverse)
Invert the entire display or set it back to normal.
static void displayOn()
Turn the display on.
static uint8_t buttonsState()
Get the current state of all buttons as a bitmask.
static void flipHorizontal(bool flipped)
Flip the display horizontally or set it back to normal.
static void exitToBootloader()
Exit the sketch and start the bootloader.
static void setRGBled(uint8_t red, uint8_t green, uint8_t blue)
Set the light output of the RGB LED.
static void LCDDataMode()
Put the display into data mode.
static void digitalWriteRGB(uint8_t red, uint8_t green, uint8_t blue)
Set the RGB LEDs digitally, to either fully on or fully off.
static void delayShort(uint16_t ms) __attribute__((noinline))
Delay for the number of milliseconds, specified as a 16 bit value.
static void paint8Pixels(uint8_t pixels)
Paint 8 pixels vertically to the display.
static void allPixelsOn(bool on)
Turn all display pixels on or display the buffer contents.
static void freeRGBled()
Relinquish analog control of the RGB LED.
static void LCDCommandMode()
Put the display into command mode.
static void SPItransfer(uint8_t data)
Transfer a byte to the display.