// předefinování a přečíslování i2c sběrnic - Wire() , Wire1()
// upraveno přepínání Wire() a Wire1() ternárním operátorem
// tcaselect funguje o.k.
// ještě propracovat PCA_routines ...
// 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: 08");
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(); // běží s default parametry
Wire.setClock(Wire_FREQ);
// Wire.begin(SDA, SCL, Wire_FREQ); // možnost změny default parametrů
// !!! Wire1.begin(); // nefunguje bez zadání pinů
Wire1.begin(SDA_1, SCL_1, Wire_1_FREQ); // druhá sběrnice, deklarace parametrů v I2C.h //
Serial.println("Hello, H0 by ESP32!");
vTaskDelay(pdMS_TO_TICKS(500));
I2C_scan(0); // scan systemové sběrice Wire()
I2C_scan(1); // scan sběrnice Wire1()
lcd.setCursor(0, 0);
lcd.print("Connecting to ");
lcd.setCursor(0, 1);
lcd.print("WiFi ......... ");
vTaskDelay(pdMS_TO_TICKS(500));
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(500));
// lcd.clear();
// lcd.setCursor(0, 0);
// lcd.println("Online");
configNTP(); /********************************************************************************************/
lcd.setCursor(0, 1);
lcd.println("..local time set");
vTaskDelay(pdMS_TO_TICKS(500));
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Online H0_08");
#ifdef PWM
PCA_setup(); /********************************************************************************************/
#endif
vTaskDelay(pdMS_TO_TICKS(500));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////// 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; // select system i2c bus (wire())
pcf_inputs = ~pcf8575_read(i2c_bus,0x21); // testování read
Serial.println("PCF8575_21 wire : ");
Serial.println(pcf_inputs,BIN);
i2c_bus = 1; // select (wire1) i2c bus
tcaselect(1); // přepínání kanálů funguje, TCA na sběrnici 1 (Wire1())
pcf_inputs = ~pcf8575_read(i2c_bus, 0x24);
pcf8575_write(0, 0x22, pcf_inputs); // testování write
Serial.println("PCF8575_24 wire2 : ");
Serial.println(pcf_inputs,BIN);
pcf_inputs = ~pcf8575_read(i2c_bus, 0x25);
pcf8575_write(0, 0x23, pcf_inputs); // testování write
#ifdef NEX
nex_read_data(); /********************************************************************************************/
if (dataReady) {
nex_parse_data(); /********************************************************************************************/
dataReady = false;
}
#endif NEX
#ifdef PWM
// Serial.println(millis());
write_pwm(); // trvá cca
// Serial.println(millis());
#endif
vTaskDelay(pdMS_TO_TICKS(1000));
}
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