// zapracovat NEX :A different approach ... example pro spolecne cteni/posilani 8bit Go, Halt, dir...
//#define DEBUG // Pokud zakomentuju nebo smažu, blok se nepřeloží
#define PWM // obsluha pwm_PCA.h
#define NEX // obsluha next.h
#define PCF // obsluha next.h
uint16_t pcf_inputs;
#include "defs.h" // deklarace společných proměnných
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(LCD1_ADDRESS, 16, 2); // je v I2C.h
#include "I2C.h"
#include "NTP.h"
//uint8_t error;
unsigned long duration, period, start, finished, elapsed; // pomocne pro time
#include "PCA_routines.h"
#include "PCF_routines.h"
#include "NEX_routines.h"
void setup() {
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("H0 Railway ESP32");
lcd.setCursor(0, 1);
lcd.println("structs v: 05");
Serial.begin(115200); // hwUART0 default pins RX=GPIO3 TX=GPIO1, can be remapped
//Serial1.begin(9600); // hwUART1 e.g. pins RX=GPI09 TX=GPI10, flash memory conflict..
#ifdef NEX
Serial2.begin(9600, SERIAL_8N1, 16, 17); // hwUART2 e.g. pins RX=GPI16 TX=GPI17 for Nextion serial
#endif NEX
// best safe use hwUART2: pins RX=GPI16/TX=GPI17 independent off usb programming,boot/loop,internal memory, wifi etc
Wire.begin();
Wire2.begin(SDA_2, SCL_2, Wire_2_FREQ); // druhá sběrnice, deklarováno v I2C.h
Serial.println("Hello, H0 by ESP32!");
vTaskDelay(pdMS_TO_TICKS(1000));
I2C_1_scan(); /********************************************************************************************/
I2C_2_scan(); /********************************************************************************************/
lcd.setCursor(0, 0);
lcd.print("Connecting to ");
lcd.setCursor(0, 1);
lcd.print("WiFi ......... ");
vTaskDelay(pdMS_TO_TICKS(1000));
WiFi.begin("Wokwi-GUEST", "", 6);
uint64_t startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < timeout) {
delay(250);
spinner();
}
if (WiFi.status() == WL_CONNECTED) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("WiFi connected");
lcd.setCursor(0, 1);
lcd.print("local IP : ");
lcd.setCursor(0, 1);
lcd.print(WiFi.localIP());
Serial.println("\nConnected to WiFi!");
Serial.println(WiFi.localIP());
} else {
lcd.setCursor(0, 1);
lcd.print("ConnectionFailed");
Serial.println("\nConnection Timeout or Failed. Continuing offline...");
}
vTaskDelay(pdMS_TO_TICKS(2000));
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.println("Online");
configNTP(); /********************************************************************************************/
lcd.setCursor(0, 1);
lcd.println("..local time set");
vTaskDelay(pdMS_TO_TICKS(1000));
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Online H0_00");
#ifdef PWM
PCA_setup(); /********************************************************************************************/
#endif
vTaskDelay(pdMS_TO_TICKS(1000));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////// main loop //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
delay(10); // this speeds up the simulation
// aktualizace času na LCD každoých 500 msec
if (millis()-period >= 500)
{
printLocalTime(); /********************************************************************************************/
period=millis();
}
// PCF readings
i2c_bus = 0; // selec system i2c bus (wire)
pcf_inputs = ~pcf8575_read(i2c_bus,0x21);
Serial.println("PCF8575_21 wire : ");
Serial.println(pcf_inputs,BIN);
i2c_bus = 2;
tcaselect(1); // přepínání kanálů funguje
pcf_inputs = ~pcf8575_read(i2c_bus, 0x24);
pcf8575_write(0, 0x22, pcf_inputs);
Serial.println("PCF8575_24 wire2 : ");
Serial.println(pcf_inputs,BIN);
#ifdef NEX
nex_read_data(); /********************************************************************************************/
if (dataReady) {
nex_parse_data(); /********************************************************************************************/
dataReady = false;
}
#endif NEX
#ifdef PWM
Serial.println(millis());
write_pwm(); /* trvá cca 44 milis *************************************************************/
Serial.println(millis());
#endif
vTaskDelay(pdMS_TO_TICKS(2000));
}
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
lcd.setCursor(15, 1);
lcd.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
70 addr 000
0 addr 000
LCD 0x27
5 addr 101
4 addr 100
pwm3 00010
pwm4 00011