#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <Keypad.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//DISPLAY
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
//DISPLAY
//KEYPAD
// define numero de filas
const uint8_t ROWS = 4;
// define numero de columnas
const uint8_t COLS = 4;
// define la distribucion de teclas
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
// pines correspondientes a las filas
uint8_t colPins[COLS] = { 16, 4, 2, 15 };
// pines correspondientes a las columnas
uint8_t rowPins[ROWS] = { 19, 18, 5, 17 };
// crea objeto con los prametros creados previamente
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
//KEYPAD
//Variables
bool conectado_wifi=false;
bool conectado_dcc=false;
String key_string = "";
String ip_server;
String ip_port;
String loco_addr;
void setup() {
Serial.begin(115200);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
// Draw a single pixel in white
//display.drawPixel(10, 10, WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
main_menu();
//conectar_screen();
/*
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP
// it is a good practice to make sure your code sets wifi mode how you want it.
// put your setup code here, to run once:
//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wm;
// reset settings - wipe stored credentials for testing
// these are stored by the esp library
//wm.resetSettings();
// Automatically connect using saved credentials,
// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),
// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())
// then goes into a blocking loop awaiting configuration and will return success result
bool res;
// res = wm.autoConnect(); // auto generated AP name from chipid
// res = wm.autoConnect("AutoConnectAP"); // anonymous ap
res = wm.autoConnect("AutoConnectAP","password"); // password protected ap
if(!res) {
Serial.println("Failed to connect");
// ESP.restart();
}
else {
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
}
*/
}
void main_menu(){
display.clearDisplay(); // Clear display buffer
display.display();
}
String readKeypadInput() {
String inputString = ""; // Almacena la cadena de números ingresados
while (true) {
char key = keypad.getKey();
if (key) {
if (key == '#') {
// Si se presiona '#', regresa la cadena y sale de la función
//display.println(inputString);
//display.display();
return inputString;
}else if(key == 'B'){ //Borra ultimo caracter.
inputString = inputString.substring(0, inputString.length() - 1);
Serial.println(inputString);
display.print(inputString);
display.display();
} else {
// Si se presiona un número, agrégalo a la cadena
if (key=='*'){
key='.';
}
inputString += key;
Serial.println(key);
display.print(key);
display.display();
}
}
}
}
void conectar_screen(){
display.clearDisplay(); // Clear display buffer
display.setCursor(0, 5);
display.setTextSize(1);
display.setTextColor(WHITE);
// Display static text
if (ip_server.length() == 0){
display.print("IP Server:");
display.display();
ip_server = readKeypadInput();
}else{
display.print("IP Server:");
display.println(ip_server);
}
if (ip_port.length() == 0){
display.println("");
display.print("IP Port:");
display.display();
ip_port = readKeypadInput();
}else{
display.println("");
display.print("IP Port:");
display.println(ip_port);
}
// display.display();
if (ip_server.length() > 0 && ip_port.length() > 0){
display.println("");
display.print("conectando....");
}
// si ya tengo las variables llenas. hacer la coneccion
//cient.connect (ip_server,ip_port);
display.display();
//ponele que conecto
conectado_dcc=true;
}
void select_loco(){
display.clearDisplay(); // Clear display buffer
display.setCursor(0, 10);
display.setTextSize(1);
display.setTextColor(WHITE);
// Display static text
if (loco_addr.length() == 0){
display.print("Loco:");
display.display();
loco_addr = readKeypadInput();
}else{
display.print("Loco:");
display.println(loco_addr);
}
display.display();
}
void loop() {
if (conectado_dcc==false){
conectar_screen();
}else{
select_loco();
}
}