// ТЕСТ кнопки
#define OLED_SOFT_64
#define BTN_OK 2
#define BTN_UP 3
#define BTN_DOWN 4
#define BTN_LEFT 8
#define BTN_RIGHT 9
#define OLED_PWR1 5
#define OLED_PWR2 6
#define OLED_PWR3 7
#include <directADC.h>
#include <EEPROM.h>
#include <GyverPower.h>
#include <GyverTimer.h>
#include <GyverButton.h>
#include <GyverOLED.h>
#define APPS_NUM 3
GyverOLED<SSD1306_128x64, OLED_BUFFER> oled;
GButton ok(BTN_OK, HIGH_PULL);
GButton up(BTN_UP, HIGH_PULL);
GButton down(BTN_DOWN, HIGH_PULL);
GButton left(BTN_LEFT, HIGH_PULL);
GButton right(BTN_RIGHT, HIGH_PULL);
int iUP, iDOWN, iLEFT, iRIGHT;
void setup() {
oledEnable();
Serial.begin(9600);
Wire.setClock(800000L); // макс. 800'000
}
void loop() {
static uint8_t pointer = 1;
buttonsTick();
if(up.isClick() or up.isStep()) {
pointer = constrain(pointer - 1, 1, APPS_NUM);
}
if(down.isClick() or down.isStep()) {
pointer = constrain(pointer + 1, 1, APPS_NUM);
}
if(ok.isClick() or ok.isStep()) {
switch (pointer) {
case 1: PlayGame1();
break;
case 2: PlayGame2();
break;
case 3: PlayGame3();
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
}
}
oled.clear();
oled.home();
oled.setCursor(0, 1); oled.print(" Game 1\n");
oled.setCursor(0, 2); oled.print(" Game 2\n");
oled.setCursor(0, 3); oled.print(" Game 3\n");
oled.setCursor(0, pointer);
oled.print(F(" >"));
oled.update();
}
void oledEnable() {
oled.init();
Wire.setClock(800000L); // макс. 800'000
}
void buttonSetting() {
ok.setDebounce(30);
up.setDebounce(30);
down.setDebounce(30);
left.setDebounce(30);
right.setDebounce(30);
ok.setTimeout(140);
up.setTimeout(140);
down.setTimeout(140);
left.setTimeout(140);
right.setTimeout(140);
ok.setClickTimeout(140);
up.setClickTimeout(140);
down.setClickTimeout(140);
left.setClickTimeout(140);
right.setClickTimeout(140);
}
void buttonsTick() {
ok.tick();
up.tick();
down.tick();
left.tick();
right.tick();
}
void PlayGame1() {
while(true) {
buttonsTick();
oled.clear();
oled.setCursor(0, 0);
oled.print("Test");
oled.setCursor(60, 2); oled.print(iUP);
oled.setCursor(30, 4); oled.print(iLEFT);
oled.setCursor(90, 4); oled.print(iRIGHT);
oled.setCursor(60, 6); oled.print(iDOWN);
if(ok.isClick()){
break;
}
if(up.isClick() or up.isHold()){
iUP++;
}
if(down.isClick() or down.isHold()){
iDOWN++;
}
if(left.isClick() or left.isHold()){
iLEFT++;
}
if(right.isClick() or right.isHold()){
iRIGHT++;
}
oled.update();
}
}
void PlayGame2() {
static int i;
static byte x = 1;
static byte y = 15;
static byte xSize = 4;
static byte ySize = 4;
static bool moveRight;
static bool moveDown;
while(true) {
buttonsTick();
if(ok.isClick() or ok.isStep()){
break;
}
oled.clear();
oled.rect(0, 0, 127, 53, OLED_STROKE);
oled.rect(x, y, x+xSize, y+ySize, OLED_FILL);
oled.setCursor(28, 7);
oled.print("rizha mavpa?");
oled.setCursor(2, 7);
oled.print(i);
/* ------- Движение по оси х ------- */
if(moveRight == true) {
if (x < 127 - xSize)
x++;
else if(x > xSize) {
moveRight = false;
x--;
i++;
}
}
else {
if (x > 0)
x--;
else {
moveRight = true;
x++;
i++;
}
}
/* ------- Движение по оси у ------- */
if(moveDown == true) {
if (y < 53 - ySize)
y++;
else if(y > ySize) {
moveDown = false;
y--;
i++;
}
}
else {
if (y > 0)
y--;
else {
moveDown = true;
y++;
i++;
}
}
oled.update();
}
}
void PlayGame3() {
while(true) {
if(ok.isClick() or ok.isStep()){
break;
}
buttonsTick();
oled.clear();
oled.setCursor(5, 2); oled.print("BACK TO MENU!");
oled.update();
}
}