#include <Arduino.h>
#include "U8g2lib.h"
#include <Wire.h>
//U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=/ 12, / data=/ 14, / reset=*/ U8X8_PIN_NONE);
//U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0);
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0);
#include <ezButton.h>
#include "ErriezDS1307.h"
// Create RTC object
ErriezDS1307 rtc;
#include "Menu.h"
#include "hello.h"
Menu* menu = NULL;
#define JOYX_PIN A0
#define JOYY_PIN A1
int joyX ;
int joyY ;
int joyX_initial_value;
int joyY_initial_value;
int joyX_value;
int joyY_value;
bool joy_up=0;
bool joy_down =0;
bool joy_right=0;
bool joy_left =0;
bool joy_right_prev = 0;
bool joy_left_prev = 0;
bool joy_up_prev = 0;
bool joy_down_prev = 0;
bool joy_right_prev_millis = 0;
bool joy_left_prev_millis = 0;
bool sel_right = false;
bool sel_left = false;
bool sel_up = false;
bool sel_down = false;
int menu_selected = 0;
#define servo_pin_bas 9 //SERVO_PIN_A //9
#define servo_pin_millieu 10 // SERVO_PIN_B //10
#define BUTON_PIN 6
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16 // Popular NeoPixel ring size
ezButton button1(BUTON_PIN); // Connect your button between pin 2 and GND
bool flipFlop = false;
int leds_brightness = 16;
int servo_moved = 1;
long led_step_prev_millis;
uint8_t hour;
uint8_t min;
uint8_t sec;
void setup(void) {
u8g2.begin();
u8g2.setDrawColor(1);
u8g2.setFontPosTop();
u8g2.setFontDirection(0);
menu = new Menu(MENU_PRINCIPAL);
pinMode(JOYX_PIN,INPUT);
pinMode(JOYY_PIN,INPUT);
joyX_initial_value = analogRead(JOYX_PIN);
joyY_initial_value = analogRead(JOYY_PIN);
//button1.begin();
button1.setDebounceTime(100); // set debounce time to 100 milliseconds
button1.setCountMode(COUNT_FALLING);
Serial.begin(57600);
Serial.println("Hello");
u8g2.drawBitmap( 0, 0, 16,64,Hello);
do {
}
while (millis()<2000);
// Initialize RTC
while (!rtc.begin()) {
Serial.println(F("RTC not found"));
delay(3000);
}
// Set square wave out pin
// SquareWaveDisable, SquareWave1Hz, SquareWave4096Hz, SquareWave8192Hz, SquareWave32768Hz
rtc.setSquareWave(SquareWaveDisable);
// Set new time 12:00:00
if (!rtc.setTime(12, 0, 0)) {
Serial.println(F("Set time failed"));
}
}
void draw(void) {
uint8_t i, h;
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_logisoso34_tf);
u8g2.drawStr( 5, 12, "20");
u8g2.drawStr( 54, 12, ":");
u8g2.drawStr( 79, 12, "40");
// here mass the display
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(10,10,"T");
u8g2.sendBuffer();
//}
}
void joy_step(void) {
joyX_value = analogRead(JOYX_PIN);
joyY_value = analogRead(JOYY_PIN);
if (joyX_value> 550) joy_right = 1 ;
else joy_right = 0;
if (joyX_value< 400) joy_left = 1 ;
else joy_left = 0;
if (joyY_value> 550) joy_up = 1;
else joy_up = 0;
if (joyY_value< 400) joy_down = 1;
else joy_down = 0;
if ((joy_right==1) && ((joy_right_prev!= joy_right)||(joy_right_prev_millis+100<millis()))) {
sel_right = true;
}
if ((joy_left==1) && ((joy_left_prev!= joy_left)||(joy_left_prev_millis+100<millis()))) {
sel_left = true;
}
if ((joy_up==1) && (joy_up_prev!= joy_up)) {
Serial.println("UP");
sel_up = true;
}
if ((joy_down==1) && (joy_down_prev!= joy_down)) {
Serial.println("DOWN");
sel_down = true;
}
joy_right_prev = joy_right;
joy_left_prev = joy_left;
joy_up_prev = joy_up;
joy_down_prev = joy_down;
if (sel_up==true) {
menu->buttonUp();
sel_up=false;}
if (sel_down==true) {
menu->buttonDown();
sel_down=false;}
if (sel_right==true) {
menu->buttonRight();
joy_right_prev_millis = millis();
sel_right=false;}
if (sel_left==true) {
menu->buttonLeft();
joy_left_prev_millis = millis();
sel_left=false;}
}
void loop(void) {
joy_step();
// if ( menu_redraw_required != 0 ) {
u8g2.sendBuffer();
// menu_redraw_required = 0;
// }
static uint8_t aLastState, aState,counter;
button1.loop();
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
//isPressed()
if (button1.getCount() == 1) {
menu->buttonClick();
button1.resetCount();
}
// Get time from RTC
if (!rtc.getTime(&hour, &min, &sec)) {
Serial.println(F("Get time failed"));
} else {
// Print time
Serial.print(hour);
Serial.print(F(":"));
if (min < 10) {
Serial.print(F("0"));
}
Serial.print(min);
Serial.print(F(":"));
if (sec < 10) {
Serial.print(F("0"));
}
Serial.println(sec);
}
}