Further cleanup and code improvements

Cleanup up commented unused code
Improved ballast setup function(now uses an array of ballast pointers)
This commit is contained in:
Rohan Sircar 2020-08-02 12:34:46 +05:30
parent 6dbc50875c
commit 2df921c435

View File

@ -6,7 +6,8 @@
#include <Streaming.h> #include <Streaming.h>
#define RELAY_ON 0 #define RELAY_ON 0
#define RELAY_OFF 1 #define RELAY_OFF 1
// #define ESP_TO_ARDUINO_PIN 2 #define BALLAST_SIZE 3
SoftwareSerial s(5, 6); //RX,TX SoftwareSerial s(5, 6); //RX,TX
RTC_DS1307 rtc; RTC_DS1307 rtc;
@ -25,11 +26,12 @@ struct ballast
int relayPin; int relayPin;
}; };
struct ballast ballast1, ballast2, ballast3; struct ballast ballast1, ballast2, ballast3;
// struct ballast ballasts[3]; struct ballast *ballasts[BALLAST_SIZE] = {
&ballast1, &ballast2, &ballast3};
void setupBallast(ballast *b); void setupBallast(ballast *b);
void setupBallast(ballast *b, JsonObject &root, int i); int setupBallasts(ballast *b_arr[], size_t arr_size, JsonObject &root);
void doBallast(ballast *b); void doBallast(ballast *b, int hr, int mn);
void updateBallast(ballast *b, JsonObject &root); void updateBallast(ballast *b, JsonObject &root);
void manualMode(ballast *b); void manualMode(ballast *b);
int getFreeRam(); int getFreeRam();
@ -37,46 +39,26 @@ const size_t bufferSize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + 3 * JSON_OB
unsigned long currentMillis; unsigned long currentMillis;
unsigned long loopMillis; unsigned long loopMillis;
unsigned long loopPeriod = 1000; const unsigned long loopPeriod = 1000;
unsigned long data_send_millis; unsigned long data_send_millis;
unsigned long data_send_period = 2000; const unsigned long data_send_period = 2000;
static bool manualFlag; static bool manualFlag;
// char roul[] = "is a fag";
// char data[60];
void setup() void setup()
{ {
// put your setup code here, to run once:
Serial.begin(115200); Serial.begin(115200);
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//Tell me esp that arduino has turned on //Tell the esp that arduino has turned on
s.begin(9600); s.begin(9600);
// analogWrite(13, 255); // analogWrite(13, 255);
digitalWrite(13, 1); digitalWrite(13, 1);
// strcpy(roul,"");
//while (!Serial) continue;
// DateTime now = rtc.now();
//StaticJsonBuffer<400> jsonBuffer1;
// DynamicJsonBuffer jsonBuffer(bufferSize);
StaticJsonBuffer<bufferSize> jsonBuffer; StaticJsonBuffer<bufferSize> jsonBuffer;
JsonObject &root = jsonBuffer.parseObject(s); JsonObject &root = jsonBuffer.parseObject(s);
// while (!root.success())
// { setupBallasts(ballasts, BALLAST_SIZE, root);
// Serial << "Invalid or no JSON received at setup." << endl; // setupBallast(&ballast2, root, 2);
// root = jsonBuffer.parseObject(s); // setupBallast(&ballast3, root, 3);
// }
// root.printTo(data);
// while(root == JsonObject::invalid())
// {
// Serial.println(F("Error code1");
// }
setupBallast(&ballast1, root, 1);
setupBallast(&ballast2, root, 2);
setupBallast(&ballast3, root, 3);
root.prettyPrintTo(Serial); root.prettyPrintTo(Serial);
Serial.println(F("Settings initialized via json")); Serial.println(F("Settings initialized via json"));
if (root["manual"] == F("True")) if (root["manual"] == F("True"))
@ -88,15 +70,12 @@ void setup()
data_send_millis = millis(); data_send_millis = millis();
} }
// // static bool newData = false;
void loop() void loop()
{ {
while (!rtc.begin()) while (!rtc.begin())
{ {
Serial.println(F("Couldn't find RTC")); Serial.println(F("Couldn't find RTC"));
//while (1);
delay(100); delay(100);
} }
@ -105,29 +84,19 @@ void loop()
Serial.println(F("RTC is NOT running!")); Serial.println(F("RTC is NOT running!"));
} }
const DateTime now = rtc.now();
const int hour = now.hour();
const int min = now.minute();
if (s.available() > 0) if (s.available() > 0)
{ {
// delay(100);
// DynamicJsonBuffer jsonBuffer(bufferSize);
// const size_t bufsize = JSON_ARRAY_SIZE(3) + JSON_OBJECT_SIZE(2) + 3*JSON_OBJECT_SIZE(5);
// StaticJsonBuffer<bufsize> jsonBuffer;
StaticJsonBuffer<bufferSize> jsonBuffer; StaticJsonBuffer<bufferSize> jsonBuffer;
JsonObject &root = jsonBuffer.parseObject(s); JsonObject &root = jsonBuffer.parseObject(s);
if (root.success()) if (root.success())
{ {
Serial << F("Received JSON") << endl; Serial << F("Received JSON") << endl;
root.prettyPrintTo(Serial); root.prettyPrintTo(Serial);
// // data = "";
// root.printTo(data);
// // newData = true;
// }
// if (newData)
// {
// Serial.println(F("Parsing data"));
// DynamicJsonBuffer jsonBuffer(bufferSize);
// JsonObject &root = jsonBuffer.parseObject(data);
// root.prettyPrintTo(Serial);
updateBallast(&ballast1, root); updateBallast(&ballast1, root);
updateBallast(&ballast2, root); updateBallast(&ballast2, root);
updateBallast(&ballast3, root); updateBallast(&ballast3, root);
@ -142,7 +111,6 @@ void loop()
Serial.println(F("Manual set to false")); Serial.println(F("Manual set to false"));
} }
} }
// newData = false;
} }
currentMillis = millis(); currentMillis = millis();
@ -158,9 +126,9 @@ void loop()
} }
else else
{ {
doBallast(&ballast1); doBallast(&ballast1, hour, min);
doBallast(&ballast2); doBallast(&ballast2, hour, min);
doBallast(&ballast3); doBallast(&ballast3, hour, min);
} }
// Serial << F("Free RAM: ") << getFreeRam() << endl; // Serial << F("Free RAM: ") << getFreeRam() << endl;
@ -174,8 +142,7 @@ void loop()
JsonObject &root = jsonBuffer.createObject(); JsonObject &root = jsonBuffer.createObject();
JsonArray &lcd_data = root.createNestedArray("lcdData"); JsonArray &lcd_data = root.createNestedArray("lcdData");
char time_buffer[10]; char time_buffer[10];
DateTime now = rtc.now(); sprintf(time_buffer, "%02d:%02d", hour, min);
sprintf(time_buffer, "%02d:%02d", now.hour(), now.minute());
// root["hello"] = ("world"); // root["hello"] = ("world");
lcd_data.add("90%%"); lcd_data.add("90%%");
lcd_data.add(time_buffer); lcd_data.add(time_buffer);
@ -193,20 +160,23 @@ void setupBallast(ballast *b)
b->shour = 14; b->shour = 14;
b->smin = 0; b->smin = 0;
b->fadePeriod = 60 * 60000; b->fadePeriod = 60 * 60000;
//b->stage = 0; b->stage = 0;
b->brightness = 0; b->brightness = 0;
b->pin = 11; b->pin = 11;
} }
void setupBallast(ballast *b, JsonObject &root, int i) int setupBallasts(ballast *b_arr[], size_t arr_size, JsonObject &root)
{ {
pinMode(b->pin, OUTPUT); if (arr_size < 3)
pinMode(b->relayPin, OUTPUT); {
Serial << "Error: array size less than 3" << endl;
return -1;
}
JsonArray &settings = root["settings"]; JsonArray &settings = root["settings"];
JsonObject &settings0 = settings[0]; JsonObject &settings0 = settings[0];
JsonObject &settings1 = settings[1]; JsonObject &settings1 = settings[1];
JsonObject &settings2 = settings[2]; JsonObject &settings2 = settings[2];
if (i == 1)
{ {
ballast *b = b_arr[0];
b->id = 1; b->id = 1;
b->shour = settings0["shour"]; b->shour = settings0["shour"];
unsigned long fP = settings0["fadePeriod"]; unsigned long fP = settings0["fadePeriod"];
@ -218,9 +188,11 @@ void setupBallast(ballast *b, JsonObject &root, int i)
b->pin = 11; b->pin = 11;
b->relayPin = 2; b->relayPin = 2;
b->fadeStartMillis = millis(); b->fadeStartMillis = millis();
pinMode(b->pin, OUTPUT);
pinMode(b->relayPin, OUTPUT);
} }
else if (i == 2)
{ {
ballast *b = b_arr[1];
b->id = 2; b->id = 2;
b->shour = settings1["shour"]; b->shour = settings1["shour"];
unsigned long fP = settings1["fadePeriod"]; unsigned long fP = settings1["fadePeriod"];
@ -232,9 +204,11 @@ void setupBallast(ballast *b, JsonObject &root, int i)
b->pin = 10; b->pin = 10;
b->relayPin = 4; b->relayPin = 4;
b->fadeStartMillis = millis(); b->fadeStartMillis = millis();
pinMode(b->pin, OUTPUT);
pinMode(b->relayPin, OUTPUT);
} }
else if (i == 3)
{ {
ballast *b = b_arr[2];
b->id = 3; b->id = 3;
b->shour = settings2["shour"]; b->shour = settings2["shour"];
unsigned long fP = settings2["fadePeriod"]; unsigned long fP = settings2["fadePeriod"];
@ -246,42 +220,15 @@ void setupBallast(ballast *b, JsonObject &root, int i)
b->pin = 9; b->pin = 9;
b->relayPin = 7; b->relayPin = 7;
b->fadeStartMillis = millis(); b->fadeStartMillis = millis();
pinMode(b->pin, OUTPUT);
pinMode(b->relayPin, OUTPUT);
} }
return 0;
} }
void updateBallast(ballast *b, JsonObject &root) void updateBallast(ballast *b, JsonObject &root)
{ {
JsonArray &settings = root["settings"]; JsonArray &settings = root["settings"];
// JsonObject &settings0 = settings[0];
// JsonObject &settings1 = settings[1];
// JsonObject &settings2 = settings[2];
// if (b->id == 1)
// {
// b->shour = settings0["shour"];
// unsigned long fP = settings0["fadePeriod"];
// b->fadePeriod = fP * 60000;
// b->smin = settings0["smin"];
// b->ehour = settings0["ehour"];
// b->emin = settings0["emin"];
// }
// if (b->id == 2)
// {
// b->shour = settings1["shour"];
// unsigned long fP = settings1["fadePeriod"];
// b->fadePeriod = fP * 60000;
// b->smin = settings1["smin"];
// b->ehour = settings1["ehour"];
// b->emin = settings1["emin"];
// }
// if (b->id == 3)
// {
// b->shour = settings2["shour"];
// unsigned long fP = settings2["fadePeriod"];
// b->fadePeriod = fP * 60000;
// b->smin = settings2["smin"];
// b->ehour = settings2["ehour"];
// b->emin = settings2["emin"];
// }
Serial << "id = " << b->id << endl; Serial << "id = " << b->id << endl;
JsonObject &settings_i = settings[b->id - 1]; JsonObject &settings_i = settings[b->id - 1];
b->shour = settings_i["shour"]; b->shour = settings_i["shour"];
@ -291,11 +238,8 @@ void updateBallast(ballast *b, JsonObject &root)
b->ehour = settings_i["ehour"]; b->ehour = settings_i["ehour"];
b->emin = settings_i["emin"]; b->emin = settings_i["emin"];
} }
int getStage(ballast *b) int getStage(ballast *b, int hr, int mn)
{ {
DateTime now = rtc.now();
int hr = now.hour();
int mn = now.minute();
Serial.print(F("Current hour is: ")); Serial.print(F("Current hour is: "));
Serial.println(hr); Serial.println(hr);
Serial.print(F("Current minute is: ")); Serial.print(F("Current minute is: "));
@ -348,17 +292,14 @@ int getStage(ballast *b)
return 1; return 1;
} }
int getStage2(ballast *b) int getStage2(ballast *b, int hr, int mn)
{ {
const DateTime now = rtc.now();
int hr = now.hour();
int mn = now.minute();
Serial.print(F("Current hour is: ")); Serial.print(F("Current hour is: "));
Serial.println(hr); Serial.println(hr);
Serial.print(F("Current minute is: ")); Serial.print(F("Current minute is: "));
Serial.println(mn); Serial.println(mn);
const unsigned int current_time = (now.hour() * 60) + now.minute(); const unsigned int current_time = (hr * 60) + mn;
const unsigned int start_time = ((b->shour * 60) + b->smin); const unsigned int start_time = ((b->shour * 60) + b->smin);
const unsigned int end_time = ((b->ehour * 60) + b->emin); const unsigned int end_time = ((b->ehour * 60) + b->emin);
const unsigned int fade_period = (b->fadePeriod / 60000); const unsigned int fade_period = (b->fadePeriod / 60000);
@ -381,7 +322,7 @@ int getStage2(ballast *b)
// const unsigned long time_offset = settings_time - current_time; // const unsigned long time_offset = settings_time - current_time;
} }
void doBallast(ballast *b) void doBallast(ballast *b, int hr, int mn)
{ {
// Log.notice(F("-------------------Ballast%d-------------------")); // Log.notice(F("-------------------Ballast%d-------------------"));
Serial << "-------------------Ballast" << b->id << "-------------------" << endl; Serial << "-------------------Ballast" << b->id << "-------------------" << endl;
@ -398,8 +339,8 @@ void doBallast(ballast *b)
Serial.println(b->emin); Serial.println(b->emin);
Serial.print(F("Brightness: ")); Serial.print(F("Brightness: "));
Serial.println(b->brightness); Serial.println(b->brightness);
const int stage = getStage2(b); const int stage = getStage2(b, hr, mn);
const int stage2 = getStage(b); const int stage2 = getStage(b, hr, mn);
Serial.print(F("Stage: ")); Serial.print(F("Stage: "));
Serial.println(stage); Serial.println(stage);
Serial << "Stage2 = " << stage2 << endl; Serial << "Stage2 = " << stage2 << endl;
@ -475,9 +416,8 @@ void doBallast(ballast *b)
// Serial.println(b->brightness); // Serial.println(b->brightness);
// b->fadeStartMillis = currentMillis; // b->fadeStartMillis = currentMillis;
// } // }
DateTime now = rtc.now();
const unsigned int fade_period = (b->fadePeriod / 60000); const unsigned int fade_period = (b->fadePeriod / 60000);
const unsigned long current_time = now.hour() * 60 + now.minute(); const unsigned long current_time = hr * 60 + mn;
const unsigned long settings_time = (b->ehour * 60 + b->emin + fade_period); const unsigned long settings_time = (b->ehour * 60 + b->emin + fade_period);
const unsigned long time_offset = fade_period - (settings_time - current_time); const unsigned long time_offset = fade_period - (settings_time - current_time);
const unsigned int brightness2 = map(time_offset, 0, fade_period, 255, 0); const unsigned int brightness2 = map(time_offset, 0, fade_period, 255, 0);