Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2019 OBONO
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22.  
  23. #include <Arduboy.h>
  24. #include <SwitchControlLibrary.h>
  25.  
  26. #define DPAD_BUTTONS (UP_BUTTON|DOWN_BUTTON|LEFT_BUTTON|RIGHT_BUTTON)
  27.  
  28. #define IMG_BUTTON_W 16
  29.  
  30. PROGMEM static const uint8_t imgButtonReleased[][32] = { // 16x16 x6
  31. {
  32. 0xE0, 0x18, 0x04, 0x02, 0x82, 0x41, 0x41, 0x21, 0x21, 0x11, 0xF1, 0x02, 0x02, 0x04, 0x18, 0xE0,
  33. 0x07, 0x18, 0x20, 0x40, 0x41, 0x82, 0x82, 0x84, 0x84, 0x88, 0x8F, 0x40, 0x40, 0x20, 0x18, 0x07
  34. },{
  35. 0xE0, 0x18, 0x04, 0x02, 0x02, 0xF1, 0x11, 0x21, 0x21, 0x41, 0x41, 0x82, 0x02, 0x04, 0x18, 0xE0,
  36. 0x07, 0x18, 0x20, 0x40, 0x40, 0x8F, 0x88, 0x84, 0x84, 0x82, 0x82, 0x41, 0x40, 0x20, 0x18, 0x07
  37. },{
  38. 0xE0, 0x18, 0x04, 0x02, 0x02, 0x81, 0x61, 0x11, 0x11, 0x61, 0x81, 0x02, 0x02, 0x04, 0x18, 0xE0,
  39. 0x07, 0x18, 0x20, 0x40, 0x46, 0x85, 0x84, 0x84, 0x84, 0x84, 0x85, 0x46, 0x40, 0x20, 0x18, 0x07
  40. },{
  41. 0xE0, 0x18, 0x04, 0x02, 0x62, 0xA1, 0x21, 0x21, 0x21, 0x21, 0xA1, 0x62, 0x02, 0x04, 0x18, 0xE0,
  42. 0x07, 0x18, 0x20, 0x40, 0x40, 0x81, 0x86, 0x88, 0x88, 0x86, 0x81, 0x40, 0x40, 0x20, 0x18, 0x07
  43. },{
  44. 0xE0, 0x18, 0x04, 0x02, 0xFA, 0x89, 0x89, 0x89, 0x89, 0x89, 0x51, 0x22, 0x02, 0x04, 0x18, 0xE0,
  45. 0x07, 0x18, 0x20, 0x40, 0x5F, 0x90, 0x90, 0x90, 0x90, 0x90, 0x89, 0x46, 0x40, 0x20, 0x18, 0x07
  46. },{
  47. 0xE0, 0x18, 0x04, 0x02, 0x02, 0x01, 0xE1, 0x19, 0x19, 0xE1, 0x01, 0x02, 0x02, 0x04, 0x18, 0xE0,
  48. 0x07, 0x18, 0x20, 0x40, 0x58, 0x87, 0x82, 0x82, 0x82, 0x82, 0x87, 0x58, 0x40, 0x20, 0x18, 0x07
  49. }
  50. };
  51.  
  52. PROGMEM static const uint8_t imgButtonPressed[][32] = { // 16x16 x6
  53. {
  54. 0xE0, 0xF8, 0xFC, 0xFE, 0x7E, 0xBF, 0xBF, 0xDF, 0xDF, 0xEF, 0x0F, 0xFE, 0xFE, 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70. 0x07, 0x1F, 0x3F, 0x7F, 0x67, 0xF8, 0xFD, 0xFD, 0xFD, 0xFD, 0xF8, 0x67, 0x7F, 0x3F, 0x1F, 0x07
  71. }
  72. };
  73.  
  74. static Arduboy arduboy;
  75.  
  76. void handleDpad(uint8_t state)
  77. {
  78. Hat hat;
  79. switch (state) {
  80. case UP_BUTTON: hat = Hat::TOP; break;
  81. case UP_BUTTON|RIGHT_BUTTON: hat = Hat::TOP_RIGHT; break;
  82. case RIGHT_BUTTON: hat = Hat::RIGHT; break;
  83. case DOWN_BUTTON|RIGHT_BUTTON: hat = Hat::BOTTOM_RIGHT; break;
  84. case DOWN_BUTTON: hat = Hat::BOTTOM; break;
  85. case DOWN_BUTTON|LEFT_BUTTON: hat = Hat::BOTTOM_LEFT; break;
  86. case LEFT_BUTTON: hat = Hat::LEFT; break;
  87. case UP_BUTTON|LEFT_BUTTON: hat = Hat::TOP_LEFT; break;
  88. default: hat = Hat::CENTER; break;
  89. }
  90. SwitchControlLibrary().MoveHat((uint8_t)hat);
  91. }
  92.  
  93. void handleAButton(bool isPress)
  94. {
  95. if (isPress) {
  96. SwitchControlLibrary().PressButtonA();
  97. } else {
  98. SwitchControlLibrary().ReleaseButtonA();
  99. }
  100. }
  101.  
  102. void handleBButton(bool isPress)
  103. {
  104. if (isPress) {
  105. SwitchControlLibrary().PressButtonB();
  106. } else {
  107. SwitchControlLibrary().ReleaseButtonB();
  108. }
  109. }
  110.  
  111. void drawButton(uint8_t x, uint8_t y, uint8_t idx, bool isPressed)
  112. {
  113. const uint8_t *s = (isPressed) ? imgButtonPressed[idx] : imgButtonReleased[idx];
  114. uint8_t *d = arduboy.getBuffer() + x + y / 8 * WIDTH;
  115. memcpy_P(d, s, IMG_BUTTON_W);
  116. memcpy_P(d + WIDTH, s + IMG_BUTTON_W, IMG_BUTTON_W);
  117. }
  118.  
  119. /*-------------------------------------------------------------------------*/
  120.  
  121. void setup()
  122. {
  123. arduboy.beginNoLogo();
  124. arduboy.setFrameRate(60);
  125. arduboy.clear();
  126. arduboy.setCursor(22, 0);
  127. arduboy.print(F("Arduboy4Switch"));
  128. }
  129.  
  130. void loop()
  131. {
  132. static uint8_t lastState = 0;
  133. uint8_t state = arduboy.buttonsState();
  134. uint8_t diff = state ^ lastState;
  135.  
  136. if (diff & DPAD_BUTTONS) {
  137. handleDpad(state & DPAD_BUTTONS);
  138. }
  139. if (diff & A_BUTTON) {
  140. handleBButton(state & A_BUTTON);
  141. }
  142. if (diff & B_BUTTON) {
  143. handleAButton(state & B_BUTTON);
  144. }
  145. lastState = state;
  146.  
  147. if (arduboy.nextFrame()) {
  148. drawButton(16, 32, 0, state & LEFT_BUTTON);
  149. drawButton(48, 32, 1, state & RIGHT_BUTTON);
  150. drawButton(32, 16, 2, state & UP_BUTTON);
  151. drawButton(32, 48, 3, state & DOWN_BUTTON);
  152. drawButton(80, 48, 4, state & A_BUTTON);
  153. drawButton(96, 32, 5, state & B_BUTTON);
  154. arduboy.display();
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement