Skip to content

Commit

Permalink
Opaygo step by step librairies and functions integration (#100)
Browse files Browse the repository at this point in the history
* git changes

* Changes in loop

* change on token_management

* main.cpp

* include opaygo_decoder

* token_management

* add function GetDataFromToken from opaygo_decoder

* this commit is only to update the branch

* update branch

* add opaygo_functions

* add LoadActivationVariables() function

* complete main.cpp with functions of opaygo

* add UpdateDeviceStatusFromTokenValue()

* add functions in opaygo_functions

* update code

* add functions in opaygo_functions

* solving problem of time

* editorConfig problem solving

* editorConfig problem solving

* editorConfig problem solving

* clang-format problem solving

* EditConfig problem solving

* EditConfig problem solving

* commit before changing branch

* solving conflict for PR

* format document

* format document

---------

Co-authored-by: Peguy WANDA <peguy@debian-BULLSEYE-live-builder-AMD64>
Co-authored-by: Daniel Mohns <daniel.mohns@posteo.de>
  • Loading branch information
3 people authored Aug 21, 2023
1 parent f91f7ee commit cd2b49d
Show file tree
Hide file tree
Showing 8 changed files with 461 additions and 42 deletions.
Empty file added firmware/.gitignore
Empty file.
1 change: 0 additions & 1 deletion firmware/src/credit.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once

// defines

// Arduino base libraries
Expand Down
45 changes: 45 additions & 0 deletions firmware/src/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,48 @@ void urgeent() {
}
}
}

uint32_t convertArrayToUint32(int arrayToConvert[9]) {
int i = 0;
uint32_t uint32 = 0;
for (i = 0; i < 9; i++) {
uint32 = 10 * uint32 + arrayToConvert[i];
}
return (uint32);
}

uint32_t convertByteArrayToUint32(uint8_t arrayToConvert[4]) {
uint32_t low1 = arrayToConvert[0];
uint32_t low2 = arrayToConvert[1];
low2 = low2 << 8;
uint32_t high1 = arrayToConvert[2];
high1 = high1 << 16;
uint32_t high2 = arrayToConvert[3];
high2 = high2 << 24;
uint32_t result = low1 + low2 + high1 + high2;
return (result);
}

void convertUint32ToUint8Array(uint32_t uint32ToConvert,
uint8_t arrayBytes[4]) {
byte low1 = uint32ToConvert;
byte low2 = uint32ToConvert >> 8;
byte high1 = uint32ToConvert >> 16;
byte high2 = uint32ToConvert >> 24;
arrayBytes[0] = low1;
arrayBytes[1] = low2;
arrayBytes[2] = high1;
arrayBytes[3] = high2;
}

void storeUint32InNvram(uint32_t toStore, int address) {
uint8_t arrayBytes[4];
convertUint32ToUint8Array(toStore, arrayBytes);
rtc.writenvram(address, arrayBytes, 4);
}

uint32_t readUint32FromNvram(int address) {
uint8_t readData[4] = {0};
rtc.readnvram(readData, 4, address);
return (convertByteArrayToUint32(readData));
}
82 changes: 66 additions & 16 deletions firmware/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// defines

// Arduino base libraries
#include <Wire.h>
#include "Arduino.h"
// #include <Arduino.h>

// third party libraries
#include <ArduinoHttpClient.h>
// #include <ArduinoHttpClient.h>
// #include <Wire.h>

// OpenSmartMeter libraries
#include "credit.hpp"
Expand All @@ -14,6 +14,7 @@
#include "lcd_display.hpp"
#include "lcd_init.hpp"
#include "mem_init.hpp"
#include "opaygo_functions.hpp"
#include "power.hpp"
#include "relay.hpp"
#include "remote.hpp"
Expand All @@ -22,6 +23,17 @@
#include "time_management.hpp"
#include "token_management.hpp"

extern "C" {
#include "opaygo_decoder.h"
}

uint64_t InputToken;
TokenData Output;
uint32_t StartingCode = 123456789;
unsigned char SECRET_KEY[16] = {0xa2, 0x9a, 0xb8, 0x2e, 0xdc, 0x5f, 0xbb, 0xc4,
0x1e, 0xc9, 0x53, 0xf, 0x6d, 0xac, 0x86, 0xb1};
// char SECRET_KEY[16] = {...};

HardwareSerial Serial2(PA3, PA2);

byte fe1[8] = {0b00011, 0b00011, 0b00011, 0b00011,
Expand Down Expand Up @@ -110,7 +122,7 @@ void setup() {
lcd.print("RTC is NOT running!");
delay(2000);
}

initializeTime();
lcd.setCursor(0, 0);
lcd.print("CSOne : ");
lcd.setCursor(8, 0);
Expand All @@ -134,23 +146,44 @@ void setup() {
}
delay(10);
relay_on();
if (is_STSmode) {
#if defined(TIM1)
TIM_TypeDef* Instance = TIM1;
TIM_TypeDef* Instance = TIM1;
#else
TIM_TypeDef* Instance = TIM2;
TIM_TypeDef* Instance = TIM2;
#endif
HardwareTimer* MyTim = new HardwareTimer(Instance);
MyTim->setOverflow(20, HERTZ_FORMAT);
MyTim->attachInterrupt(urgeent);
MyTim->resume();
} else {
printf("OpenPAYGO code written here");
HardwareTimer* MyTim = new HardwareTimer(Instance);
MyTim->setOverflow(20, HERTZ_FORMAT);
switch (Mode_select) {
case 0:
lcd.clear();
lcd.setCursor(0, 0);
lcd.println("No Configuration! ");
while (Mode_select == 0) // wait for mode configuration
{
STS_keypad();
delay(20);
}
break;

case 1:
MyTim->attachInterrupt(urgeent);
MyTim->resume();
break;

case 2:
/*OpenPayGo Token initializing code; */
printf("Welcome to the OPAYGO Device\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
break;
}
}

void loop() {
if (is_STSmode) {
if (Mode_select == 1) {
mesure();
if ((mains_input_value > 50)) {
credit_reminder();
Expand All @@ -163,7 +196,24 @@ void loop() {
if ((sts_mode == 0) && (mains_input_value > 50)) {
gsm_func();
}
} else {
printf("OpenPAYGO code written here");
}

if (Mode_select == 2) {
// 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);
}
}
Loading

0 comments on commit cd2b49d

Please sign in to comment.