Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore state of Choice-between-energy-based-and-time-based #128

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions firmware/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ board = bluepill_f103c8_128k

upload_protocol = serial
monitor_speed = 115200
#upload_port = COM4 # To upload the code uncomment this line and put the port corresponding to your module on you PC

# Disable some warning flags for libraries
build_flags =
Expand Down
4 changes: 3 additions & 1 deletion firmware/src/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ void urgeent() {
credit_formular();
}

STS_keypad();
if (Mode_select != 2) {
STS_keypad();
}
warn_now = millis();
if (c_chek == 1) {
if (warn_now - warntime > 1500) {
Expand Down
38 changes: 32 additions & 6 deletions firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,31 @@ void setup() {
}
break;

case 1:
case 1: // Module in STS Mode
printf("Welcome to the STS Energy-based\n");
MyTim->attachInterrupt(urgeent);
MyTim->resume();
break;

case 2:
case 2: // Module in OpenPaygo Energy-based
/*OpenPayGo Token initializing code; */
printf("Welcome to the OPAYGO Device\n");
printf("Welcome to the OpenPaygo Energy-based\n");
printf(
"We're waiting for the * character to start recording the key "
"presses.\n(Press the '#' key to see the device activation "
"status)\n\n");
LoadActivationVariables(); // We load the activation variableS
LoadActivationVariables(); // We load the activation variables
break;

case 3: // Module in OpenPaygo Time-based
/*OpenPaygo Time-based initializing code; */

break;
}
}

void loop() {
if (Mode_select == 1) {
if (Mode_select == 1) { // Module in STS Mode
mesure();
if ((mains_input_value > 50)) {
credit_reminder();
Expand All @@ -196,7 +202,7 @@ void loop() {
}
}

if (Mode_select == 2) {
if (Mode_select == 2) { // Module in OpenPaygo Energy-based
// We wait for a token to be entered
InputToken = WaitForTokenEntry();
// We get the activation value from the token
Expand All @@ -214,4 +220,24 @@ void loop() {

UpdateDeviceStatusFromTokenValue(Output.Value, Output.Count);
}

if (Mode_select == 3) { // Module in OpenPaygo Time-based
/*Code for OpenPaygo Time-based*/
/* // We wait for a token to be entered
InputToken = WaitForTokenEntry();
// We get the activation value from the token

Output = GetDataFromToken(
InputToken, &TokenCount, &UsedTokens, StartingCode,
SECRET_KEY); // We get the activation value from the token

printf("\n(Token entered: %llu)", InputToken);
printf("\n(Activation Value from Token: %d)",
Output.Value); // Activation Value found in the token
printf("\n(Count: %d)", Output.Count); // Count found in the token
printf("\n(Max Count: %d)", TokenCount); // Count found in the token
printf("\n(Used Tokens: %d)\n", UsedTokens); // Count found in the token

UpdateDeviceStatusFromTokenValue(Output.Value, Output.Count);*/
}
}
112 changes: 95 additions & 17 deletions firmware/src/opaygo_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,50 @@ void LoadActivationVariables() {
// UsedTokens if needed)
UsedTokens =
mem.readInt(UsedTokens_eeprom_location); // We load UsedTokens if needed
PAYGEnabled = mem.read(
switch (mem.read(PAYGEnabled_eeprom_location)) {
case 0:
PAYGEnabled = false;
break;
case 1:
PAYGEnabled = true;
break;
}

mem.read(
PAYGEnabled_eeprom_location); // We load PAYGEnabled //Verify this syntax
ActiveUntil =
mem.readLong(ActiveUntil_eeprom_location); // We load ActiveUntil
TokenEntryLockedUntil = mem.readLong(
TokenEntryLockedUntil_eeprom_location); // We load TokenEntryLockedUntil
if (Mode_select == 2) {
creditt =
mem.readLong(credit_eeprom_location); // We load creditt in case of
// OpenPaygo Energy-based
} else {
ActiveUntil =
mem.readLong(ActiveUntil_eeprom_location); // We load ActiveUntil
}
}

void StoreActivationVariables() {
mem.writeInt(TokenCount_eeprom_location,
TokenCount); // We store TokenCount (& UsedTokens if needed)
mem.writeInt(UsedTokens_eeprom_location,
UsedTokens); // We store UsedTokens if needed
mem.write(PAYGEnabled_eeprom_location,
PAYGEnabled); // We store PAYGEnabled //Verify this syntax
mem.writeLong(ActiveUntil_eeprom_location,
ActiveUntil); // We store ActiveUntil
if (PAYGEnabled) {
mem.writeInt(PAYGEnabled_eeprom_location, 1); // We store PAYGEnabled
} else {
mem.writeInt(PAYGEnabled_eeprom_location, 0); // We store PAYGEnabled
}

mem.writeLong(TokenEntryLockedUntil_eeprom_location,
TokenEntryLockedUntil); // We store TokenEntryLockedUntil
if (Mode_select == 2) {
mem.writeLong(
credit_eeprom_location,
creditt); // We Store creditt in case of OpenPaygo Energy-based
} else {
mem.writeLong(ActiveUntil_eeprom_location,
ActiveUntil); // We store ActiveUntil
}
}

void storeTimeStampEEPROM(uint32_t timeStampInSeconds) {
Expand Down Expand Up @@ -202,7 +227,29 @@ void SetTime(int ActivationValue) {
// seconds for to compare to our RTC time)
}

void AddCreditt(int ActivationValue) {
mem.writeLong(credit_eeprom_location, creditt); // write present credit
creditt = mem.readLong(credit_eeprom_location); // fetch previous credit //
// add new if any to old credit //
creditt += ActivationValue;
mem.writeLong(credit_eeprom_location, creditt);
get_credit = 1;
}

void SetCreditt(int ActivationValue) {
creditt = ActivationValue;
mem.writeLong(credit_eeprom_location, creditt); // write present credit
creditt = mem.readLong(credit_eeprom_location); // fetch previous credit //
// add new if any to old credit //
creditt = ActivationValue;
mem.writeLong(credit_eeprom_location, creditt);
get_credit = 1;
}

void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) {
// TokenValue in case of OpenPaygo Energy-based is consider directly as a
// credit to add or set and in case of OpenPaygo Time-based it is consider as
// a number of day to add or to set
if (TokenValue == -1) {
InvalidTokenCount++;
UpdateInvalidTokenWaitingPeriod();
Expand All @@ -223,9 +270,17 @@ void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) {
} else {
if (ActivationCount % 2) {
PAYGEnabled = true;
SetTime(TokenValue);
if (Mode_select == 2) {
SetCreditt(TokenValue);
} else {
SetTime(TokenValue);
}
} else {
AddTime(TokenValue);
if (Mode_select == 2) {
AddCreditt(TokenValue);
} else {
AddTime(TokenValue);
}
}
BlinkGreenLED(
2,
Expand All @@ -237,14 +292,23 @@ void UpdateDeviceStatusFromTokenValue(int TokenValue, int ActivationCount) {

bool IsActive() {
if (PAYGEnabled) {
if (ActiveUntil > GetTimeInSeconds()) {
return true;
} else {
return false;
if (Mode_select == 2) {
if (creditt < 1 || fault == 1) {
return false;
} else {
return true;
}
}

if (Mode_select == 3) {
if (ActiveUntil > GetTimeInSeconds()) {
return true;
} else {
return false;
}
}
} else {
return true;
}
return true;
}

uint64_t WaitForTokenEntry() {
Expand All @@ -268,8 +332,22 @@ uint64_t WaitForTokenEntry() {
if (IsActive()) {
BlinkGreenLED(1, BLINK_PERIOD);
#ifdef DEBUG
printf("\nTime Left: %" PRIu32 "seconds",
ActiveUntil - GetTimeInSeconds());
if (Mode_select == 2) { //
mesure();
if ((mains_input_value > 50)) {
credit_reminder();
}
if ((mains_input_value < 50)) {
digitalWrite(red_led, LOW);
digitalWrite(green_led, LOW);
}
urgeent();
printf("\nEnergy Left: %f KWH", ENERGY);
} else {
printf("\nTime Left: %" PRIu32 "seconds",
ActiveUntil - GetTimeInSeconds());
}

#endif
} else {
BlinkRedLED(1, BLINK_PERIOD);
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/sts_token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void STStoken_decode() {
mem.writeLong(credit_eeprom_location, creditt); // write present credit
creditt =
mem.readLong(credit_eeprom_location); // fetch previous credit //
creditt = creditt + topup; // add new if any to old credit //
creditt = creditt + topup; // add new if any to old credit //
mem.writeLong(credit_eeprom_location, creditt);
get_credit = 1;
}
Expand Down
8 changes: 6 additions & 2 deletions firmware/src/token_management.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,15 @@ void STS_keypad() {
dt = 0;
}
if (sts_data == "112") {
Mode_select = 1;
Mode_select = 1; // For STS Mode (Energy-based)
}

if (sts_data == "122") {
Mode_select = 2;
Mode_select = 2; // For OpenPaygo Energy-based
}

if (sts_data == "132") {
Mode_select = 3; // For OpenPaygo Time-based
}
}
}
Expand Down