// funguje i2c scan
// !!! nekde na i2c to vidi nejake 0x27 i kdyz neni, proto testuju isr vektor na 0x26
// funguje PCF read vcetne ISR
// funguje PCA pwm serva - na zkousku jen 2 pwm vystupy
// funguje WS2812
// L298 simulace motoru funguje
// zkouska PWM LED vz pwm3 kod od luisllamas - simulace zamrzne a nejede
// Arduino kompilace ESP32 Dev Module:
// Projekt zabírá 1029544 bytů (78%) úložného místa pro program. Maximum je 1310720 bytů.
// Globální proměnné zabírají 49668 bytů (15%) dynamické paměti, 278012 bytů zůstává pro lokální proměnné. Maximum je 327680 bytů.
// za ten objem asi muze wifi
#include <WiFi.h>
#include <Wire.h>
#include <I2cDiscreteIoExpander.h> // PCF85xx library
#include <Adafruit_PWMServoDriver.h> // PWMservo library
#include <FastLED.h>
#define NUM_LEDS 16
#define DATA_PIN 15
CRGB leds[NUM_LEDS];
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
#include <LiquidCrystal_I2C.h> // Importa a biblioteca para o LCD I2C.
LiquidCrystal_I2C lcd(0x27, 16, 2); // Define o endereço I2C do LCD e seu tamanho.
#define Add0 32 // PCF chip1 INToutput read
#define LEDPIN 25
#define DEBOUNCE_DELAY 100 // puvodni 200
/*
bool interrupt = false; // true on interrupt
bool ledState = false;
uint32_t lastInterruptTime = 0;
uint16_t ISRVector;
*/
volatile bool interrupt = false; // true on interrupt
volatile bool ledState = false;
volatile uint32_t lastInterruptTime = 0;
volatile uint16_t ISRVector;
uint16_t ISR_Xvector;
uint8_t ISRdev = 6; // ISR vector device
/*volatile */ uint8_t error, address, reg, data;
uint8_t dev_addr = 0x40; //0x40 - 12C Address
uint8_t mode1 = 0; //0x00 location for Mode1 register address
uint8_t mode2 = 1; //0x01 location for Mode2 register address
uint16_t PRE_SCALE =30;
uint8_t led0 = 6; //0x06 location for start LED0 registers
uint8_t rst = 1; //0x01 reset device
uint8_t rval = 0; //read register value
uint16_t led_on;
uint16_t led_off;
uint8_t ledN;
uint8_t led_stat[16];
/**********************************************************************/
#define PCA9685_ADDRESS_1 0x40 // I2C PCA9685
#define PCA9685_ADDRESS_2 0x41 // I2C PCA9685
#define PCA9685_ADDRESS_3 0x42 // I2C PCA9685
#define PCA9685_ADDRESS_4 0x43 // I2C PCA9685
// Adafruit_PWMServoDriver pwm1 = Adafruit_PWMServoDriver(PCA9685_ADDRESS_1);
Adafruit_PWMServoDriver pwm2 = Adafruit_PWMServoDriver(PCA9685_ADDRESS_2);
Adafruit_PWMServoDriver pwm3 = Adafruit_PWMServoDriver(PCA9685_ADDRESS_3);
// Adafruit_PWMServoDriver pwm4 = Adafruit_PWMServoDriver(PCA9685_ADDRESS_4);
#define SERVOMIN 150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // This is the 'maximum' pulse length count (out of 4096)
#define SERVO_FREQ 50 // Analog servos run at ~50 Hz updates
// Adjust these angles according to Servo360_PCA_AngleFInd code that I have provided
int leftAngle = 0; // angel for motor to move left (If the motor move in right you can change the name to rightAngle)
int rightAngle = 180; // angel for motor to move right (If the motor move in left you can change the name to leftAngle)
int stopAngle = 90; // Angle when motor stops
// our servo # counter
uint8_t servonum = 0;
byte totalServos = 2; // Define how many servos we are using here
// Define the time (in milliseconds 1 second = 1000 millisecond) for each servo to move in right direction
int rightMoveTime[13] = { 200, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; // Wait for this millisecond when motor move right
int stopWaitTime[13] = { 200, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; // Wait for this millisecond after motor has reached it's point
int leftMoveTime[13] = { 200, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; // Wait for this millisecond when motor move left
/*************************************************************************/
// create I2cDiscreteIoExpander objects
I2cDiscreteIoExpander device[8] = {0,1,2,3,4,5,6,7}; // [8=total number]
uint8_t PCF_addr[8] = {0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27};
uint8_t PCA_addr[8] = {0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47};
/********************************** ISR *********************************/
void IRAM_ATTR doISR(){
//void ARDUINO_ISR_ATTR doISR() {
uint32_t currentTime = millis();
if (currentTime - lastInterruptTime > DEBOUNCE_DELAY) { // <<<---- tady je to kouzlo
interrupt = true;
lastInterruptTime = currentTime;
/*
if (interrupt) {
Serial.println("ON"); // to jeste zvlada bez havarie
} else {
Serial.println("OFF"); // to jeste zvlada bez havarie
}
*/
// ledState = !ledState; // jen střídavě přepíná
// digitalWrite(LEDPIN, ledState); // POZOR - dlouhá loop - dlouhá odezva - ošetřit jinak
digitalWrite(LEDPIN, interrupt); // POZOR - dlouhá loop - dlouhá odezva - ošetřit jinak
}
}
void setup() {
pinMode(Add0, INPUT_PULLUP);
pinMode(34, INPUT_PULLUP);
pinMode(LEDPIN, OUTPUT);
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, 16, 17); // Nextion serial
// Serial2.begin(9600); // Nextion serial2
Serial.println("\nI2C Scanner");
Serial2.println("Serial 2 NEX");
Wire.begin();
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" I2C scan ");
lcd.setCursor(0, 1);
lcd.print(" > scanning > ");
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
// LCD_displ(" I2C scan ", " > finished > ");
// lcd.setCursor(0, 0);
// lcd.print(" I2C scan ");
lcd.setCursor(0, 1);
lcd.print(" finished ");
}
lcd.clear();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Connecting to ");
lcd.setCursor(0, 1);
lcd.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("Online");
lcd.setCursor(0, 1);
lcd.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
setTimezone("CET-1CEST,M3.5.0,M10.5.0/3");
// initTime("CET-1CEST,M3.5.0,M10.5.0/3"); // TZ-String for Europe/Berlin
/************************************************************************/
// prime ovladani PCA 0x40 pro LED
//set for auto-increnent
///reg = mode1;
data = 160; // 0b10100000 viz Mode register 1 PCA9685 str.13 RESTART a AUTOINCREMENT
//data = 176; // 0b11100000 viz Mode register 1 PCA9685 str.13 RESTART, SLEEP a AUTOINCREMENT
//data = 32; // 0b00100000 viz bit 5 Mode register 1 PCA9685 str.13
data = 160; // 0b10100000 viz Mode register 1 PCA9685 str.13 RESTART a AUTOINCREMENT
//data = 176; // 0b11100000 viz Mode register 1 PCA9685 str.13 RESTART, SLEEP a AUTOINCREMENT
//data = 32; // 0b00100000 viz bit 5 Mode register 1 PCA9685 str.13
WRITE_REG(0x40, mode1, data); //mode1 register
delay(50);
// Choose below the desired output mode
//Direct LED connection
// -- set to outpud mode INVRT= 1 OUTDRV =0
///reg = mode2;
data = 16;
WRITE_REG(0x40, mode2, data); //mode2 register
delay(50);
// The PRE_SCALE register can only be set when the SLEEP bit of MODE1 register is set to logic 1.
// mode1 | 0b00001000
WRITE_REG(0x40, mode1, 176); //mode1 SLEEP bit
delay(50);
uint16_t prescale = 121; // => 50 Hz
WRITE_REG(0xFE, PRE_SCALE, prescale); //prescale factor
delay(50);
// mode1 & 0b11110111
WRITE_REG(0x40, mode1, data); //mode1 register
delay(50);
/************************************************************************/
// PCF init preset
// bez nasled disable to nehraje
for(uint8_t i = 0; i<8; i++){
device[i].disableBitwiseInversion();
}
/***===================================================================***/
// pwm1.begin(); // Initialize PWM instance
// pwm1.setOscillatorFrequency(27000000);
// pwm1.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
pwm2.begin(); // Initialize PWM instance
pwm2.setOscillatorFrequency(27000000);
pwm2.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
// pwm3.begin(); // Initialize PWM instance
// pwm3.setOscillatorFrequency(27000000);
// pwm3.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
// zkouska ale simulace pak zamrzne viz v loop luisllamas kod
pwm3.begin();
// Set the frequency to the maximum (1600Hz)
pwm3.setPWMFreq(1600);
// pwm4.begin(); // Initialize PWM instance
// pwm4.setOscillatorFrequency(27000000);
// pwm4.setPWMFreq(SERVO_FREQ); // Analog servos run at ~50 Hz updates
// Stop all servos
for (int servoNum = 0; servoNum < totalServos; servoNum++) { // initially stop all servo motors
pwm2.setPWM(servoNum, 0, getAnglePulse(stopAngle));
}
/***===================================================================***/
/******************** Nextion init *************/
// Serial2.begin(9600, SERIAL_8N1, 16, 17); // Nextion serial
// sendNumberData("n0", 255);
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS); // GRB ordering is typical
attachInterrupt(Add0, doISR, CHANGE);
printLocalTime();
delay(500);
//ALL_LED
ledN = 61; //odkaz na registr 250 ALL_LED
led_on = 4096; // zhasne vsechny LED - stav H - LED jsou proti +Vcc
led_off = 4095; // k tomu musi byt led_on = 4095 jinak nestiha simulace
WRITE_LED(0x40, ledN, led_on, led_off);
}
/**************************************************************/
/********** MAIN LOOP - MAIN LOOP - MAIN LOOP ***************/
/**************************************************************/
void loop() {
/*
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Main loop ");
lcd.setCursor(0, 1);
lcd.print(" > in progress >");
*/
/* zpracovat flag if(interrupt=true) a pak ho vynulovat */
interrupt = false;
digitalWrite(LEDPIN, interrupt); // POZOR - dlouhá loop - dlouhá odezva - ošetřit jinak
/*
digitalWrite(LEDPIN, ledState); // POZOR - dlouhá loop - dlouhá odezva - ošetřit jinak
Serial.print("var ledState : ");
Serial.println(ledState);
digitalWrite(LEDPIN, ledState); // POZOR - dlouhá loop - dlouhá odezva - ošetřit jinak
*/
device[ISRdev].digitalRead();
ISRVector = device[ISRdev].getPorts(); // az precteni PCF s aktivnim INT schodi vystup int PCF
Serial.print("ISRVector getPorts : ");
Serial.println(ISRVector, HEX);
delay(200);
ISR_Xvector = XreadInputs(ISRdev); // az precteni PCF s aktivnim INT schodi vystup int PCF // tohle je asi pomalejsi - cca 2 millis
Serial.print("ISRX_Vector from readInputs : ");
Serial.println(ISR_Xvector, HEX);
Serial.print("var interrupt : ");
Serial.println(interrupt);
// nasledujici zpracovani = precteni portu PCF schodi vystupy INT do LOW ( HIGH je aktivni)
for(uint8_t idx = 0; idx<7; idx++){
Serial.print("inputs on device : ");
Serial.print(idx, HEX);
Serial.print(" bin : ");
Serial.println(getChangedInputValue(idx), BIN);
}
// detachInterrupt(Add0);
Serial.println("===== mirror 1 =====");
//delay(20);
mirrorInputOntoOutputs(0); // pozn: zrcadli se LowByte do HighByte, ISR funguje
delay(20);
// Serial.println("===== mirror 2 =====");
//delay(20);
/// mirrorInputOntoOutputs(1);
// Serial.println("===== mirror 3 =====");
//delay(20);
/// mirrorInputOntoOutputs(2);
// Serial.println("===== mirror 4 =====");
//delay(20);
/// mirrorInputOntoOutputs(3);
// Serial.println("===== mirror 5 =====");
//delay(20);
/// mirrorInputOntoOutputs(4);
// Serial.println("===== mirror 6 =====");
//delay(20);
/// mirrorInputOntoOutputs(5);
/*
Serial.println("ISR vectors on device(7)");
//delay(20);
readInputs(7);
/// mirrorInputOntoOutputs(7);
*/
/************************** PCA LED *********************************************/
/**/
//Serial.println("loop 0");
ledN = 0;
led_on = 4096;
led_off = 4095;
WRITE_LED(0x40, ledN, led_on, led_off);
//Serial.println("loop 1");
ledN = 1;
led_on = 4095; // prioritu ma LED_full_OFF - !!!!!!!!!
led_off = 4096; // k tomu musi byt led_on = 4095 nebo 4096(LED_ON) jinak nestiha simulace
// tyhle parametry nefungujou - ale nevim proc
WRITE_LED(0x40, ledN, led_on, led_off);
//Serial.println("loop 2");
ledN = 2;
led_on = 4096; // jas LED cim vyssi hodnota tim vic sviti / LED proti Vcc
led_off = 4095;
WRITE_LED(0x40, ledN, led_on, led_off);
//Serial.println("loop 3");
ledN = 3;
led_on = 4095;
led_off = 4096;
WRITE_LED(0x40, ledN, led_on, led_off);
/*/
/* //ALL_LED
ledN = 61; //odkaz na registr 250 ALL_LED
led_on = 4095;
led_off = 4095;
WRITE_LED(0x40, ledN, led_on, led_off);
*/
///WRITE_LED(0x40, 0, 4096, 4095);
/****************************************************************************/
leds[0] = CRGB::Orange;
leds[1] = CRGB::Green;
leds[2] = CRGB::Red;
leds[3] = CRGB::Black;
//...
leds[6] = CRGB::Blue;
leds[7] = CRGB::White;
FastLED.show();
pwm3.setPWM(0, 0, 4096);
pwm3.setPWM(1, 4096,0);
pwm3.setPWM(2, 0, 4096);
pwm3.setPWM(3, 4096,0);
delay(500);
/***====================================================================***/
// Run one servo at a time
// test pwm nr 1 only:
/*
for (int currentServo = 0; currentServo < totalServos; currentServo++) {
pwm1.setPWM(currentServo, 0, getAnglePulse(rightAngle)); // First move servo to right side
delay(rightMoveTime[currentServo]); // move motor in right for this time defined in start
pwm1.setPWM(currentServo, 0, getAnglePulse(stopAngle)); // Stop servo at this point
delay(stopWaitTime[currentServo]); // Keep motor stopped at this position defined in start
pwm1.setPWM(currentServo, 0, getAnglePulse(leftAngle)); // Now move servo to left side back
delay(leftMoveTime[currentServo]); // move motor in left for this time defined in start
pwm1.setPWM(currentServo, 0, getAnglePulse(stopAngle)); // Stop This servo
delay(50); // wait for 50 milliseconds before moving next servo
}
*/
// test pwm nr 2 only:
for (int currentServo = 0; currentServo < totalServos; currentServo++) {
pwm2.setPWM(currentServo, 0, getAnglePulse(rightAngle)); // First move servo to right side
delay(rightMoveTime[currentServo]); // move motor in right for this time defined in start
pwm2.setPWM(currentServo, 0, getAnglePulse(stopAngle)); // Stop servo at this point
delay(stopWaitTime[currentServo]); // Keep motor stopped at this position defined in start
pwm2.setPWM(currentServo, 0, getAnglePulse(leftAngle)); // Now move servo to left side back
delay(leftMoveTime[currentServo]); // move motor in left for this time defined in start
pwm2.setPWM(currentServo, 0, getAnglePulse(stopAngle)); // Stop This servo
delay(50); // wait for 50 milliseconds before moving next servo
}
/*
// test pwm nr 3 only:
for (int currentServo = 0; currentServo < totalServos; currentServo++) {
pwm3.setPWM(currentServo, 0, getAnglePulse(rightAngle)); // First move servo to right side
delay(rightMoveTime[currentServo]); // move motor in right for this time defined in start
pwm3.setPWM(currentServo, 0, getAnglePulse(stopAngle)); // Stop servo at this point
delay(stopWaitTime[currentServo]); // Keep motor stopped at this position defined in start
pwm3.setPWM(currentServo, 0, getAnglePulse(leftAngle)); // Now move servo to left side back
delay(leftMoveTime[currentServo]); // move motor in left for this time defined in start
pwm3.setPWM(currentServo, 0, getAnglePulse(stopAngle)); // Stop This servo
delay(50); // wait for 50 milliseconds before moving next servo
}
*/
/*
// https://www.luisllamas.es/en/control-16-servos-16-pwm-outputs-arduino-pca9685/
for (uint16_t duty = 0; duty < 4096; duty += 8)
{
for (uint8_t pwmNum = 0; pwmNum < 16; pwmNum++)
{
// Adjust PWM with ON at tick=0 and OFF at tick=duty
pwm3.setPWM(pwmNum, 0, duty); // to simulace nezvlada ani s knihownou PWM a zamrzne...
}
}
*/
/*
// test pwm nr 4 only:
for (int currentServo = 0; currentServo < totalServos; currentServo++) {
pwm4.setPWM(currentServo, 0, getAnglePulse(rightAngle)); // First move servo to right side
delay(rightMoveTime[currentServo]); // move motor in right for this time defined in start
pwm4.setPWM(currentServo, 0, getAnglePulse(stopAngle)); // Stop servo at this point
delay(stopWaitTime[currentServo]); // Keep motor stopped at this position defined in start
pwm4.setPWM(currentServo, 0, getAnglePulse(leftAngle)); // Now move servo to left side back
delay(leftMoveTime[currentServo]); // move motor in left for this time defined in start
pwm4.setPWM(currentServo, 0, getAnglePulse(stopAngle)); // Stop This servo
delay(50); // wait for 50 milliseconds before moving next servo
}
*/
//
/***====================================================================***/
/**/
//Serial.println("loop 0e");
ledN = 0;
led_on = 4095;
led_off = 4096;
WRITE_LED(0x40, ledN, led_on, led_off);
//Serial.println("loop 1e");
ledN = 1;
led_on = 2048; // jine nez 4096 nebo 4095 ignoruje
led_off = 4095;
WRITE_LED(0x40, ledN, led_on, led_off);
//Serial.println("loop 2e");
ledN = 2;
led_on = 4095;
led_off = 4096;
WRITE_LED(0x40, ledN, led_on, led_off);
//Serial.println("loop 3e");
ledN = 3;
led_on = 4096;
led_off = 4095;
WRITE_LED(0x40, ledN, led_on, led_off);
/*/
/* //ALL_LED
ledN = 61; //odkaz na registr 250 ALL_LED
led_on = 4095;
led_off = 4096; // k tomu musi byt led_on = 4095 jinak nestiha simulace
WRITE_LED(0x40, ledN);
*/
/**************************************************************************/
leds[0] = CRGB::Orange;
leds[1] = CRGB::Black;
leds[2] = CRGB::Black;
leds[3] = CRGB::Orange;
//...
leds[6] = CRGB::White;
leds[7] = CRGB::Blue;
FastLED.show();
pwm3.setPWM(0, 4096, 0);
pwm3.setPWM(1, 0, 4096);
pwm3.setPWM(2, 4096, 0);
pwm3.setPWM(3, 0, 4096);
//delay(200);
delay(1000);
}
/*************** END OF MAIN LOOP - END OF MAIN LOOP ***************/
void LED_SW(uint8_t PCA_addr, uint16_t ledN, uint16_t led_on, uint16_t led_off){
if (led_stat[ledN] == 0) {
led_stat[ledN] = 1;
led_on = 4096;
led_off = 0;
WRITE_LED(PCA_addr, ledN, led_on, led_off);
}
else{
led_stat[ledN] = 0;
led_on = 0;
led_off = 4096;
WRITE_LED(PCA_addr, ledN, led_on, led_off);
}
}
void SetOFF() {
led_on = 0;
led_off = 4096;
WRITE_LED;
}
void READREG(uint8_t dev_addr) {
Wire.beginTransmission( dev_addr );
Wire.write(reg);
error = Wire.endTransmission();
Wire.beginTransmission( dev_addr );
Wire.requestFrom(dev_addr,1); //start a transaction to read 1 byte
rval = Wire.read(); //read the byte
error = Wire.endTransmission();
}
void WRITE_LED(uint8_t dev_addr, uint8_t ledN, uint16_t led_on, uint16_t led_off){
byte ah,al;
Wire.beginTransmission( dev_addr );
Wire.write(led0 + 4*ledN);
ah = led_on >> 8;
al = led_on && 255;
Wire.write(al);
Wire.write(ah);
ah = led_off >> 8;
al = led_off and 255;
Wire.write(al);
Wire.write(ah);
error = Wire.endTransmission();
//Serial.print("error > ");
/*
Serial.print(error);
Serial.print(" on addr 0x");
Serial.print(dev_addr,HEX);
Serial.print(" LED:");
Serial.print(ledN);
Serial.print(" led_on :");
Serial.print(led_on);
Serial.print(" led_off :");
Serial.println(led_off);
*/
}
void WRITE_REG(uint8_t dev_addr, uint8_t reg, uint8_t data ){
Wire.beginTransmission( dev_addr );
Wire.write(reg);
Wire.write(data);
error = Wire.endTransmission();
}
/**************************************************************************/
int getAnglePulse(int inpAngle) {
int retPulse = map(inpAngle, leftAngle, rightAngle, SERVOMIN, SERVOMAX);
return retPulse;
}
uint16_t getChangedInputValue(uint8_t idx) {
// Serial.println("Get Input");
device[idx].digitalRead();
return device[idx].getPorts();
}
void mirrorInputOntoOutputs(uint8_t idx) {
uint16_t bothports = getChangedInputValue(idx);
uint16_t inputs = lowByte(bothports);
// Mirror the inputs to the outputs
// leave the inputs, as inputs (i.e. writing high)
uint16_t newValue = word(inputs, 0xff) ; // (inputs << 8) | 0xff;
/* Serial.print("Inputs read as 0x");
Serial.print(inputs, HEX);
*/ //Serial.println(" being mirrored to out");
device[idx].digitalWrite(newValue);
/* Serial.print("Wrote 2 bytes: port0 -> 0x");
Serial.print(lowByte(newValue), HEX);
Serial.print(" port1 -> 0x");
Serial.print(highByte(newValue), HEX);
Serial.print(" to device ");
Serial.println((int)idx);
*/ }
void readInputs(uint8_t idx) { // pouze posle na ser.port
uint16_t newValue = getChangedInputValue(idx);
Serial.print("Inputs read 0x");
Serial.print(newValue, HEX);
Serial.print(" on device ");
Serial.println((int)idx);
// if(newValue < 0xFFFE)
// device[idx].digitalWrite (0xFFFE);
// else device[idx].digitalWrite (0xFFFF);
}
uint16_t XreadInputs(uint8_t idx) { // funkce, vraci stav vstupu
uint16_t newValue = getChangedInputValue(idx);
Serial.print("Inputs Xread 0x");
Serial.print(newValue, HEX);
Serial.print(" on device ");
Serial.println((int)idx);
// if(newValue < 0xFFFE)
// device[idx].digitalWrite (0xFFFE);
// else device[idx].digitalWrite (0xFFFF);
// delay(50);
return newValue;
}
/********************* NEX ***** NEX ******* NEX ******************/
void sendTermination() {
Serial2.write(0xFF);
Serial2.write(0xFF);
Serial2.write(0xFF);
}
void sendWaveformData(int waveformId, int channel, int value) {
// Serial.println(value);
Serial2.print("add ");
Serial2.print(waveformId);
Serial2.print(",");
Serial2.print(channel);
Serial2.print(",");
Serial2.print(value);
sendTermination();
}
void sendTextData(const char* componentName, int value) {
Serial2.print(componentName);
Serial2.print(".txt=\"");
Serial2.print(value);
Serial2.print("\"");
sendTermination();
}
void sendNumberData(const char* componentName, int value) {
Serial2.print(componentName);
Serial2.print(".val=");
Serial2.print(value);
sendTermination();
}
void sendGaugeData(const char* componentName, int value) {
Serial2.print(componentName);
Serial2.print(".val=");
Serial2.print(value);
sendTermination();
}
void sendProgressBarData(const char* componentName, int value) {
Serial2.print(componentName);
Serial2.print(".val=");
Serial2.print(value);
sendTermination();
}
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;
}
}
/************************** NTP *********************************/
/*
void printLocalTime(){ // puvodni od https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.print("Day of week: ");
Serial.println(&timeinfo, "%A");
Serial.print("Month: ");
Serial.println(&timeinfo, "%B");
Serial.print("Day of Month: ");
Serial.println(&timeinfo, "%d");
Serial.print("Year: ");
Serial.println(&timeinfo, "%Y");
Serial.print("Hour: ");
Serial.println(&timeinfo, "%H");
Serial.print("Hour (12 hour format): ");
Serial.println(&timeinfo, "%I");
Serial.print("Minute: ");
Serial.println(&timeinfo, "%M");
Serial.print("Second: ");
Serial.println(&timeinfo, "%S");
Serial.println("Time variables");
char timeHour[3];
strftime(timeHour,3, "%H", &timeinfo);
Serial.println(timeHour);
char timeWeekDay[10];
strftime(timeWeekDay,10, "%A", &timeinfo);
Serial.println(timeWeekDay);
Serial.println();
}
*/
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
lcd.setCursor(0, 1);
lcd.println("Connection Err ");
Serial.println("Connection Err");
return;
}
lcd.setCursor(8, 0);
lcd.println(&timeinfo, "%H:%M:%S");
Serial.println(&timeinfo, "%H:%M:%S");
lcd.setCursor(0, 1);
lcd.println(&timeinfo, "%Y-%m-%d %Z");
Serial.println(&timeinfo, "%Y-%m-%d %Z");
}
void setTimezone(String timezone){
Serial.printf(" Setting Timezone to %s\n",timezone.c_str());
setenv("TZ",timezone.c_str(),1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
tzset();
}
void setTime(int yr, int month, int mday, int hr, int minute, int sec, int isDst){
struct tm tm;
tm.tm_year = yr - 1900; // Set date
tm.tm_mon = month-1;
tm.tm_mday = mday;
tm.tm_hour = hr; // Set time
tm.tm_min = minute;
tm.tm_sec = sec;
tm.tm_isdst = isDst; // 1 or 0
time_t t = mktime(&tm);
Serial.printf("Setting time: %s", asctime(&tm));
struct timeval now = { .tv_sec = t };
settimeofday(&now, NULL);
}
pwm1 - 00000
Sample time 20ms - diagram.json
0x27 ISR vect
pwm1 - 00000