-
-
Save Pharap/5716c72cbf053287d3181ce69bf3ae48 to your computer and use it in GitHub Desktop.
Example Arduboy drawing program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduboy2.h> | |
Arduboy2 arduboy; | |
void setup() | |
{ | |
arduboy.begin(); | |
arduboy.clear(); | |
} | |
constexpr uint8_t cursorXMin = 0; | |
constexpr uint8_t cursorXMax = (WIDTH - 1); | |
constexpr uint8_t cursorYMin = 0; | |
constexpr uint8_t cursorYMax = (HEIGHT - 1); | |
uint8_t cursorX = 0; | |
uint8_t cursorY = 0; | |
bool isValidPixelX(int x) | |
{ | |
return ((x >= cursorXMin) && (x < cursorXMax)); | |
} | |
bool isValidPixelY(int y) | |
{ | |
return ((y >= cursorYMin) && (y < cursorYMax)); | |
} | |
uint8_t invertPixel(uint8_t pixel) | |
{ | |
return ((pixel == BLACK) ? WHITE : BLACK); | |
} | |
constexpr uint8_t cursorWidth = 3; | |
constexpr uint8_t cursorHeight = 3; | |
bool cursorActive = false; | |
uint8_t cursorBuffer[cursorHeight][cursorWidth] = {}; | |
void saveCursorArea(uint8_t cursorX, uint8_t cursorY) | |
{ | |
uint8_t yi = 0; | |
for(int y = -1; y <= 1; ++y) | |
{ | |
const int pixelY = (cursorY + y); | |
if((pixelY < cursorYMin) || (pixelY > cursorYMax)) | |
continue; | |
uint8_t xi = 0; | |
for(int x = -1; x <= 1; ++x) | |
{ | |
const int pixelX = (cursorX + x); | |
if((pixelX < cursorXMin) || (pixelX > cursorXMax)) | |
continue; | |
const uint8_t pixel = arduboy.getPixel(pixelX, pixelY); | |
cursorBuffer[yi][xi] = pixel; | |
++xi; | |
} | |
++yi; | |
} | |
} | |
void restoreCursorArea(uint8_t cursorX, uint8_t cursorY) | |
{ | |
uint8_t yi = 0; | |
for(int y = -1; y <= 1; ++y) | |
{ | |
const int pixelY = (cursorY + y); | |
if((pixelY < cursorYMin) || (pixelY > cursorYMax)) | |
continue; | |
uint8_t xi = 0; | |
for(int x = -1; x <= 1; ++x) | |
{ | |
const int pixelX = (cursorX + x); | |
if((pixelX < cursorXMin) || (pixelX > cursorXMax)) | |
continue; | |
const uint8_t pixel = cursorBuffer[yi][xi]; | |
arduboy.drawPixel(pixelX, pixelY, pixel); | |
++xi; | |
} | |
++yi; | |
} | |
} | |
void drawCursor(uint8_t cursorX, uint8_t cursorY) | |
{ | |
for(int y = -1; y <= 1; ++y) | |
{ | |
const int pixelY = (cursorY + y); | |
if((pixelY < cursorYMin) || (pixelY > cursorYMax)) | |
continue; | |
for(int x = -1; x <= 1; ++x) | |
{ | |
const int pixelX = (cursorX + x); | |
if((pixelX < cursorXMin) || (pixelX > cursorXMax)) | |
continue; | |
if((x == 0) && (y == 0)) | |
continue; | |
const uint8_t pixel = arduboy.getPixel(pixelX, pixelY); | |
const uint8_t newPixel = invertPixel(pixel); | |
arduboy.drawPixel(pixelX, pixelY, newPixel); | |
} | |
} | |
} | |
//void drawCursor(uint8_t cursorX, uint8_t cursorY) | |
//{ | |
// int startX = (cursorX - 1); | |
// int startY = (cursorY - 1); | |
// int endX = (cursorX + 1); | |
// int endY = (cursorY + 1); | |
// | |
// if(!isValidPixelX(startX)) | |
// startX = cursorX; | |
// | |
// if(!isValidPixelY(startY)) | |
// startY = cursorY; | |
// | |
// if(!isValidPixelX(endX)) | |
// endX = cursorX; | |
// | |
// if(!isValidPixelY(endY)) | |
// endY = cursorY; | |
// | |
// const uint8_t pixel = arduboy.getPixel(cursorX, cursorY); | |
// const uint8_t newPixel = invertPixel(pixel); | |
// | |
// const uint8_t width = static_cast<uint8_t>((endX - startX) + 1); | |
// const uint8_t height = static_cast<uint8_t>((endY - startY) + 1); | |
// | |
// arduboy.drawRect(startX, startY, width, height, newPixel); | |
//} | |
void loop() | |
{ | |
if(!arduboy.nextFrame()) | |
return; | |
arduboy.pollButtons(); | |
const uint8_t previousCursorX = cursorX; | |
const uint8_t previousCursorY = cursorY; | |
if(arduboy.justPressed(UP_BUTTON)) | |
{ | |
if(cursorY > cursorYMin) | |
--cursorY; | |
} | |
if(arduboy.justPressed(DOWN_BUTTON)) | |
{ | |
if(cursorY < cursorYMax) | |
++cursorY; | |
} | |
if(arduboy.justPressed(LEFT_BUTTON)) | |
{ | |
if(cursorX > cursorXMin) | |
--cursorX; | |
} | |
if(arduboy.justPressed(RIGHT_BUTTON)) | |
{ | |
if(cursorX < cursorXMax) | |
++cursorX; | |
} | |
if(arduboy.justPressed(A_BUTTON)) | |
{ | |
const uint8_t pixel = cursorBuffer[1][1]; | |
const uint8_t newPixel = invertPixel(pixel); | |
cursorBuffer[1][1] = newPixel; | |
if(!cursorActive) | |
restoreCursorArea(previousCursorX, previousCursorY); | |
} | |
const bool cursorMoved = ((cursorX != previousCursorX) || (cursorY != previousCursorY)); | |
if(cursorMoved) | |
{ | |
restoreCursorArea(previousCursorX, previousCursorY); | |
saveCursorArea(cursorX, cursorY); | |
cursorActive = false; | |
} | |
if(arduboy.everyXFrames(15)) | |
{ | |
if(cursorActive) | |
{ | |
cursorActive = false; | |
restoreCursorArea(cursorX, cursorY); | |
} | |
else | |
{ | |
cursorActive = true; | |
saveCursorArea(cursorX, cursorY); | |
drawCursor(cursorX, cursorY); | |
} | |
} | |
arduboy.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment