Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2019 OBONO
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
- #include <Arduboy.h>
- #include <SwitchControlLibrary.h>
- #define DPAD_BUTTONS (UP_BUTTON|DOWN_BUTTON|LEFT_BUTTON|RIGHT_BUTTON)
- #define IMG_BUTTON_W 16
- PROGMEM static const uint8_t imgButtonReleased[][32] = { // 16x16 x6
- {
- 0xE0, 0x18, 0x04, 0x02, 0x82, 0x41, 0x41, 0x21, 0x21, 0x11, 0xF1, 0x02, 0x02, 0x04, 0x18, 0xE0,
- 0x07, 0x18, 0x20, 0x40, 0x41, 0x82, 0x82, 0x84, 0x84, 0x88, 0x8F, 0x40, 0x40, 0x20, 0x18, 0x07
- },{
- 0xE0, 0x18, 0x04, 0x02, 0x02, 0xF1, 0x11, 0x21, 0x21, 0x41, 0x41, 0x82, 0x02, 0x04, 0x18, 0xE0,
- 0x07, 0x18, 0x20, 0x40, 0x40, 0x8F, 0x88, 0x84, 0x84, 0x82, 0x82, 0x41, 0x40, 0x20, 0x18, 0x07
- },{
- 0xE0, 0x18, 0x04, 0x02, 0x02, 0x81, 0x61, 0x11, 0x11, 0x61, 0x81, 0x02, 0x02, 0x04, 0x18, 0xE0,
- 0x07, 0x18, 0x20, 0x40, 0x46, 0x85, 0x84, 0x84, 0x84, 0x84, 0x85, 0x46, 0x40, 0x20, 0x18, 0x07
- },{
- 0xE0, 0x18, 0x04, 0x02, 0x62, 0xA1, 0x21, 0x21, 0x21, 0x21, 0xA1, 0x62, 0x02, 0x04, 0x18, 0xE0,
- 0x07, 0x18, 0x20, 0x40, 0x40, 0x81, 0x86, 0x88, 0x88, 0x86, 0x81, 0x40, 0x40, 0x20, 0x18, 0x07
- },{
- 0xE0, 0x18, 0x04, 0x02, 0xFA, 0x89, 0x89, 0x89, 0x89, 0x89, 0x51, 0x22, 0x02, 0x04, 0x18, 0xE0,
- 0x07, 0x18, 0x20, 0x40, 0x5F, 0x90, 0x90, 0x90, 0x90, 0x90, 0x89, 0x46, 0x40, 0x20, 0x18, 0x07
- },{
- 0xE0, 0x18, 0x04, 0x02, 0x02, 0x01, 0xE1, 0x19, 0x19, 0xE1, 0x01, 0x02, 0x02, 0x04, 0x18, 0xE0,
- 0x07, 0x18, 0x20, 0x40, 0x58, 0x87, 0x82, 0x82, 0x82, 0x82, 0x87, 0x58, 0x40, 0x20, 0x18, 0x07
- }
- };
- PROGMEM static const uint8_t imgButtonPressed[][32] = { // 16x16 x6
- {
- 0xE0, 0xF8, 0xFC, 0xFE, 0x7E, 0xBF, 0xBF, 0xDF, 0xDF, 0xEF, 0x0F, 0xFE, 0xFE, 0xFC, 0xF8, 0xE0,
- 0x07, 0x1F, 0x3F, 0x7F, 0x7E, 0xFD, 0xFD, 0xFB, 0xFB, 0xF7, 0xF0, 0x7F, 0x7F, 0x3F, 0x1F, 0x07
- },{
- 0xE0, 0xF8, 0xFC, 0xFE, 0xFE, 0x0F, 0xEF, 0xDF, 0xDF, 0xBF, 0xBF, 0x7E, 0xFE, 0xFC, 0xF8, 0xE0,
- 0x07, 0x1F, 0x3F, 0x7F, 0x7F, 0xF0, 0xF7, 0xFB, 0xFB, 0xFD, 0xFD, 0x7E, 0x7F, 0x3F, 0x1F, 0x07
- },{
- 0xE0, 0xF8, 0xFC, 0xFE, 0xFE, 0x7F, 0x9F, 0xEF, 0xEF, 0x9F, 0x7F, 0xFE, 0xFE, 0xFC, 0xF8, 0xE0,
- 0x07, 0x1F, 0x3F, 0x7F, 0x79, 0xFA, 0xFB, 0xFB, 0xFB, 0xFB, 0xFA, 0x79, 0x7F, 0x3F, 0x1F, 0x07
- },{
- 0xE0, 0xF8, 0xFC, 0xFE, 0x9E, 0x5F, 0xDF, 0xDF, 0xDF, 0xDF, 0x5F, 0x9E, 0xFE, 0xFC, 0xF8, 0xE0,
- 0x07, 0x1F, 0x3F, 0x7F, 0x7F, 0xFE, 0xF9, 0xF7, 0xF7, 0xF9, 0xFE, 0x7F, 0x7F, 0x3F, 0x1F, 0x07
- },{
- 0xE0, 0xF8, 0xFC, 0xFE, 0x06, 0x77, 0x77, 0x77, 0x77, 0x77, 0xAF, 0xDE, 0xFE, 0xFC, 0xF8, 0xE0,
- 0x07, 0x1F, 0x3F, 0x7F, 0x60, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xF6, 0x79, 0x7F, 0x3F, 0x1F, 0x07
- },{
- 0xE0, 0xF8, 0xFC, 0xFE, 0xFE, 0xFF, 0x1F, 0xE7, 0xE7, 0x1F, 0xFF, 0xFE, 0xFE, 0xFC, 0xF8, 0xE0,
- 0x07, 0x1F, 0x3F, 0x7F, 0x67, 0xF8, 0xFD, 0xFD, 0xFD, 0xFD, 0xF8, 0x67, 0x7F, 0x3F, 0x1F, 0x07
- }
- };
- static Arduboy arduboy;
- void handleDpad(uint8_t state)
- {
- Hat hat;
- switch (state) {
- case UP_BUTTON: hat = Hat::TOP; break;
- case UP_BUTTON|RIGHT_BUTTON: hat = Hat::TOP_RIGHT; break;
- case RIGHT_BUTTON: hat = Hat::RIGHT; break;
- case DOWN_BUTTON|RIGHT_BUTTON: hat = Hat::BOTTOM_RIGHT; break;
- case DOWN_BUTTON: hat = Hat::BOTTOM; break;
- case DOWN_BUTTON|LEFT_BUTTON: hat = Hat::BOTTOM_LEFT; break;
- case LEFT_BUTTON: hat = Hat::LEFT; break;
- case UP_BUTTON|LEFT_BUTTON: hat = Hat::TOP_LEFT; break;
- default: hat = Hat::CENTER; break;
- }
- SwitchControlLibrary().MoveHat((uint8_t)hat);
- }
- void handleAButton(bool isPress)
- {
- if (isPress) {
- SwitchControlLibrary().PressButtonA();
- } else {
- SwitchControlLibrary().ReleaseButtonA();
- }
- }
- void handleBButton(bool isPress)
- {
- if (isPress) {
- SwitchControlLibrary().PressButtonB();
- } else {
- SwitchControlLibrary().ReleaseButtonB();
- }
- }
- void drawButton(uint8_t x, uint8_t y, uint8_t idx, bool isPressed)
- {
- const uint8_t *s = (isPressed) ? imgButtonPressed[idx] : imgButtonReleased[idx];
- uint8_t *d = arduboy.getBuffer() + x + y / 8 * WIDTH;
- memcpy_P(d, s, IMG_BUTTON_W);
- memcpy_P(d + WIDTH, s + IMG_BUTTON_W, IMG_BUTTON_W);
- }
- /*-------------------------------------------------------------------------*/
- void setup()
- {
- arduboy.beginNoLogo();
- arduboy.setFrameRate(60);
- arduboy.clear();
- arduboy.setCursor(22, 0);
- arduboy.print(F("Arduboy4Switch"));
- }
- void loop()
- {
- static uint8_t lastState = 0;
- uint8_t state = arduboy.buttonsState();
- uint8_t diff = state ^ lastState;
- if (diff & DPAD_BUTTONS) {
- handleDpad(state & DPAD_BUTTONS);
- }
- if (diff & A_BUTTON) {
- handleBButton(state & A_BUTTON);
- }
- if (diff & B_BUTTON) {
- handleAButton(state & B_BUTTON);
- }
- lastState = state;
- if (arduboy.nextFrame()) {
- drawButton(16, 32, 0, state & LEFT_BUTTON);
- drawButton(48, 32, 1, state & RIGHT_BUTTON);
- drawButton(32, 16, 2, state & UP_BUTTON);
- drawButton(32, 48, 3, state & DOWN_BUTTON);
- drawButton(80, 48, 4, state & A_BUTTON);
- drawButton(96, 32, 5, state & B_BUTTON);
- arduboy.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement