// 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...
// POZOR jiné řazení PCF a PCA na kanálech
//#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 PCF.h
//#define NTP // obsluha NTP.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"
uint8_t j;
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: 12");
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..
Serial.setTimeout(1000); // Set timeout to 1 seconds
#ifdef NEX
Serial2.begin(9600, SERIAL_8N1, 16, 17); // hwUART2 e.g. pins RX=GPI16 TX=GPI17 for Nextion serial
Serial2.setTimeout(50); // Set timeout to 0,1 seconds
#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");
#ifdef NTP
configNTP(); /********************************************************************************************/
lcd.setCursor(0, 1);
lcd.println("..local time set");
vTaskDelay(pdMS_TO_TICKS(500));
#endif
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Online H0_12");
#ifdef PWM
PCA_setup(); /********************************************************************************************/
#endif
vTaskDelay(pdMS_TO_TICKS(500));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////// main loop //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
delay(10); // this speeds up the simulation
// nex_read_data();
#ifdef NTP
// aktualizace času na LCD každoých 500 msec
if (millis()-period >= 500)
{
printLocalTime(); /********************************************************************************************/
period=millis();
//Serial2.print("j0.val=15");
//sendTermination();
}
#endif
#ifdef PCF
// 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(0); // přepínání kanálů funguje, TCA na sběrnici 1 (Wire1())
pcf_inputs = ~pcf8575_read(i2c_bus, 0x22);
pcf8575_write(0, 0x22, pcf_inputs); // testování write
pcf_inputs = ~pcf8575_read(i2c_bus, 0x23);
pcf8575_write(0, 0x23, pcf_inputs); // testování write
#endif PCF
#ifdef NEX
nex_read_data();
/* if (dataReady) {
nex_parse_data();
} */
dataReady = false;
//Serial2.print("j0.val=25");
//sendTermination();
#endif NEX
#ifdef PWM
// Serial.println(millis());
write_pwm(); // trvá cca
// Serial.println(millis());
#endif
vTaskDelay(pdMS_TO_TICKS(1000));
A_track_control[8].direction_s = 63;
Serial.println(A_track_control[8].direction_s);
}
/*******************************************************************************************************************/
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
0x21 001
0x22 010
0x42 00010
0x43 00011
pwm1 0x41 - 00001
0x23 011
0x22 010
0x23 011
0x42 00010
0x43 00011
pwm1 0x41 - 00001