#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//#include <Fonts/FreeSerifItalic9pt7b.h> librerias para fuentes italicas
#define ANCHO 128 // reemplaza ocurrencia de ANCHO por 128
#define ALTO 64 // reemplaza ocurrencia de ALTO por 64
#define OLED_RESET -1 // necesario por la libreria pero no usado
Adafruit_SSD1306 display(ANCHO, ALTO, &Wire, OLED_RESET); // crea objeto
#if (SSD1306_LCDHEIGHT != 64)
//#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
const char menu1[] PROGMEM = "MENU 1 ";
const char menu2[] PROGMEM = "MENU 2 ";
const char menu3[] PROGMEM = "MENU 3 ";
const char menu4[] PROGMEM = "MENU 4 ";
const char sub11[] PROGMEM = " < BACK ";
const char sub12[] PROGMEM = "T.C.Aut";
const char sub13[] PROGMEM = "T.C.Parc";
const char sub14[] PROGMEM = "T.Freno";
const char sub21[] PROGMEM = " < BACK ";
const char sub22[] PROGMEM = "T.Tarbj";
const char sub23[] PROGMEM = "PGM-Ctrl";
const char sub24[] PROGMEM = " ----";
const char sub31[] PROGMEM = " < BACK ";
const char sub32[] PROGMEM = "SUB 32";
const char sub33[] PROGMEM = "SUB 33";
const char sub34[] PROGMEM = "SUB 34";
const char sub41[] PROGMEM = " < BACK ";
const char sub42[] PROGMEM = "SUB 42";
const char sub43[] PROGMEM = "SUB 43";
const char sub44[] PROGMEM = "SUB 44";
//---------------------------------------------
const char*const menus[] PROGMEM ={
menu1, menu2, menu3, menu4, // menus principales
sub11, sub12, sub13, sub14, // sub menu for menu1
sub21, sub22, sub23, sub24, // sub menu for menu2
sub31, sub32, sub33, sub34, // sub menu for menu3
sub41, sub42, sub43, sub44, // sub menu for menu4
};
//----------------------------------------------
int boton = 7;
int estado_boton = 0;
int Ult_est_boton = 1;
int sub_Menu = 0;
int NUM_MENU = 4;
int val = -1;
int PinA = 3;
int PinB = 4;
int PinALast = LOW;
int n = LOW;
volatile int Posic_Ant = 0; // almacena valor anterior de la variable POSICION
volatile int Posic = 0; // variable POSICION con valor inicial de 50 y definida
//====================================================
void setup() {
pinMode( boton , INPUT_PULLUP);
pinMode (PinA, INPUT_PULLUP);
pinMode (PinB, INPUT_PULLUP);
Wire.begin(); // inicializa bus I2C
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
attachInterrupt(digitalPinToInterrupt(PinA), encoder, LOW);// interrupcion sobre pin A con
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// Serial.begin (9600);
display.clearDisplay();
}
//=================================================
void loop() {
estado_boton = digitalRead( boton );
buttonPush();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(103, 50); // ubica cursor en coordenas 10,30
display.setTextSize(2); // establece tamano de texto en 2
display.print(Posic); // escribe texto
display.display(); // muestra en pantalla todo lo establecido anteriormente
if (Posic != Posic_Ant) { // si el valor de POSICION es distinto de ANTERIOR
Posic_Ant = Posic ; // asigna a ANTERIOR el valor actualizado de POSICION
}
if( val != Posic ){
val = Posic;
actualizar_Menu();
}
}
//=====================================================
void encoder(){
static unsigned long ult_Interrup = 0; // variable static con ultimo valor de
unsigned long t_Interrup = millis(); // variable almacena valor de func. millis
if (t_Interrup - ult_Interrup > 5) { // rutina antirebote desestima
// pulsos menores a 5 mseg.
if (digitalRead(PinB) == HIGH) { // si B es HIGH, sentido horario
Posic++ ; // incrementa POSICION en 1
}
else { // si B es LOW, senti anti horario
Posic-- ; // decrementa POSICION en 1
}
Posic = min(3, max(0, Posic)); // establece limite inferior de 0 y
// superior de 100 para POSICION
ult_Interrup = t_Interrup; // guarda valor actualizado del tiempo
} // de la interrupcion en variable static
}
//=======================================================
void actualizar_Menu(){
display.clearDisplay();
//display.setFont(&FreeSerifItalic9pt7b); acitva las fuentes italicas
display.setTextSize(2);
if( sub_Menu == 0 ){
Dibujar_Menu(0); //MUESTRA EL sub_Menu 0
}
if( sub_Menu == 1){
Dibujar_Menu(4); //MUESTRA EL sub_Menu 1
}
else if( sub_Menu == 2){
Dibujar_Menu(8); //MUESTRA EL sub_Menu 2
}
else if( sub_Menu == 3){
Dibujar_Menu(12); //MUESTRA EL sub_Menu 3
}
else if( sub_Menu == 4){
Dibujar_Menu(16); // MUESTRA EL sub_Menu 4
}
//display.setFont();// detiene la futuras fuentes italica
display.display();
}
//===============================================
void buttonPush(){
if( (estado_boton == 0) && ( Ult_est_boton != 0) ){
Ult_est_boton = 0;
if( (sub_Menu == 0) && ( Posic == 0)){ // sub menu 1
sub_Menu = 1; // va del Sub_11
NUM_MENU = 4; // al Sub_13
val = -1;
}
else if( (sub_Menu == 0) && ( Posic == 1)){ // sub menu 2
sub_Menu = 2; // va del Sub_21
NUM_MENU = 4; // al Sub_23
val = -1;
}
else if( (sub_Menu == 0) && ( Posic == 2)){ // sub menu 2///OJO
sub_Menu = 3; // va del Sub_31
NUM_MENU = 4; // al Sub_33
val = -1;
}
else if( (sub_Menu == 0) && ( Posic == 3)){ // sub menu 2///OJO
sub_Menu = 4; // va del Sub_31
NUM_MENU = 4; // al Sub_33
val = -1;
} //sub_Menu != 0) && ( Posic == 0
else if( ((sub_Menu != 0) && ( Posic == 0)) ){ // Back to main menu
sub_Menu = 0;
NUM_MENU = 4;
val = -1;
}
}
Ult_est_boton = estado_boton;
}
//=============================================
void Dibujar_Menu(int start){
char tBuffer[16];//22
for(int i=0; i < NUM_MENU;i++){
display.setCursor(3,(i*17)); // posicion del menu (para fuente italica(3,(i*16)+14))
display.setTextColor(WHITE);
if( (Posic) == i ){
display.setTextColor(BLACK, WHITE);
}
display.println(strcpy_P(tBuffer, (char*)pgm_read_word(&(menus[i+start]))));
}
}