#define EB_FAST_TIME 120
#include <AccelStepper.h>
#include <GyverOLED.h>
#include <EncButton.h>
#include "GyverOLEDMenu.h"
boolean Scanner = false ;
EncButton eb(25, 33, 32, INPUT_PULLUP);
GyverOLED<SSD1306_128x64> oled;
OledMenu<9, GyverOLED<SSD1306_128x64>> menu(&oled);
int FotoStep[8][2] = {
{100, 2},
{50, 4},
{40, 5},
{25, 8},
{20, 10},
{10, 20},
{ 5, 40},
{ 4, 50},
};
int NFT;
int idx=6;
int d_p = 10;
int d_i = 1000;
int d_d = 50;
byte tt11 = 10;
float tt1 = 0.5;
boolean lgh = false;
int tt3 = 5;
int tt4 = 1000;
const int DIR = 4;
const int STEP = 16;
#define motorInterfaceType 1
AccelStepper motor(motorInterfaceType, STEP, DIR);
const static uint8_t icons_8x8[][8] PROGMEM = {
{0x00, 0x42, 0x24, 0x18, 0xff, 0x99, 0x66, 0x00}, //0 bluetooth
{0x7c, 0x44, 0xe7, 0xa5, 0xa5, 0xe7, 0x44, 0x7c}, //1 printer
{0x7e, 0x81, 0x91, 0xa1, 0x91, 0x89, 0x84, 0x72}, //2 checked
{0x7e, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x7e}, //3 unchecked
{0x1c, 0x3e, 0x73, 0xe1, 0xe1, 0x73, 0x3e, 0x1c}, //4 map
};
void drawIcon8x8(byte index) {
size_t s = sizeof icons_8x8[index];//можна так, а можна просто 8
for(unsigned int i = 0; i < s; i++) {
oled.drawByte(pgm_read_byte(&(icons_8x8[index][i])));
}
}
void PrintValue( int v) {
char str[6];
oled.setCursorXY(30, 30); // курсор в (пиксель X, строка Y)
oled.setScale(4);
if ( v ) sprintf(str, "%3u", v);
else sprintf(str, " ", v);
oled.print(str);
oled.setScale(1);
oled.update();
}
void setup() {
Serial.begin(115200);
oled.init();
Wire.setClock(400000L);
oled.clear();
//oled.update();
oled.setCursorXY(118, 0);
drawIcon8x8(0);
PrintValue(0);
oled.update();
menu.onChange(onItemChange, true);
menu.onPrintOverride(onItemPrintOverride); // если нужно сделать своё форматирование значений
menu.addItem(PSTR("<- Return")); // 0
menu.addItem(PSTR("STOP")); // 1
menu.addItem(PSTR("Num. Foto"), GM_N_INT(2), &d_p, GM_N_INT(10), GM_N_INT(50));
// menu.addItem(PSTR("PID. I"), GM_N_INT(1), &d_i, GM_N_INT(-5), GM_N_INT(20));
// menu.addItem(PSTR("PID. D"), GM_N_INT(1), &d_d, GM_N_INT(0), GM_N_INT(7000)); // 3
// menu.addItem(PSTR("ODA TIME."), GM_N_FLOAT(0.01), &tt1, GM_N_FLOAT(1), GM_N_FLOAT(20));
// menu.addItem(PSTR("TIMER 1"), GM_N_BYTE(1), &tt11, GM_N_BYTE(1), GM_N_BYTE(255)); // 5
// menu.addItem(PSTR("LIGHTING"), &lgh); // page 2
// menu.addItem(PSTR("TIMER 3"), GM_N_INT(1), &tt3, GM_N_INT(1), GM_N_INT(5));
// menu.addItem(PSTR("TIMER 4"), GM_N_INT(5), &tt4, GM_N_INT(0), GM_N_INT(1000)); // 8
menu.showMenu(false);
eb.attach(cb);
motor.setMaxSpeed(1000);
motor.setAcceleration(60);
motor.setSpeed(200);
// motor.moveTo(200);
}
void onItemChange(const int index, const void* val, const byte valType) {
if (valType == VAL_ACTION) {
if (index == 0) {
menu.showMenu(false);
}
}
if (valType == VAL_ACTION) {
if (index == 1) {
menu.showMenu(false);
Scanner = false;
}
}
}
boolean onItemPrintOverride(const int index, const void* val, const byte valType) {
// Допустим, что `TIMER 4`(index 8) - это минуты, которые мы можем менять. Отформатируем минуты по формату - `hh:mm`
if (index == 8) {
unsigned int hours = tt4 / 60; // [hh]
byte minutes = tt4 - (hours * 60); // [mm]
// отображаем нужном нам формате:
if (hours < 10) {
oled.print(0);
}
oled.print(hours);
oled.print(":");
if (minutes < 10) {
oled.print(0);
}
oled.print(minutes);
return true; // сигнализируем, что мы сами вызываем метод oled.print(...) с нужным нам форматированием
}
// возвращаем всегда `false`, если мы не собираемся для других пунктов меню принтить значение
return false;
}
void cb() {
switch (eb.action()) {
case EB_TURN:
if (eb.dir() == 1) {
menu.selectPrev(eb.fast());
} else {
menu.selectNext(eb.fast());
}
break;
case EB_HOLD:
menu.showMenu(true);
break;
case EB_CLICK:
if ( menu.isMenuShowing ) {
menu.toggleChangeSelected();
} else {
Scanner = true ;
motor.setCurrentPosition(0);
NFT=FotoStep[idx][0] ;
motor.moveTo(FotoStep[idx][1]);
}
break;
}
}
void loop() {
eb.tick();
// if (motor.distanceToGo() == 0) {
// motor.moveTo(-motor.currentPosition());
// Serial.println("Rotating Motor in opposite direction...");
// }
if ( Scanner ) {
// Serial.print("Motor Pos: ");
if ( motor.distanceToGo() == 0 && NFT!=0 ) {
Serial.print(NFT);
Serial.println(" - Ok..faccio la foto");
PrintValue(NFT);
delay(500);
NFT-- ;
if ( NFT != 0 ) {
motor.setCurrentPosition(0);
motor.moveTo(FotoStep[idx][1]);
} else {
PrintValue(0);
}
}
motor.run();
}
}