forked from hallard/remora_soft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibLibTeleinfo.h
158 lines (137 loc) · 4.91 KB
/
LibLibTeleinfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// **********************************************************************************
// Driver definition for French Teleinfo
// **********************************************************************************
// Creative Commons Attrib Share-Alike License
// You are free to use/extend this library but please abide with the CC-BY-SA license:
// http://creativecommons.org/licenses/by-sa/4.0/
//
// For any explanation about teleinfo ou use , see my blog
// http://hallard.me/category/tinfo
//
// Code based on following datasheet
// http://www.erdf.fr/sites/default/files/ERDF-NOI-CPT_02E.pdf
//
// Written by Charles-Henri Hallard (http://hallard.me)
//
// History : V1.00 2015-06-14 - First release
//
// All text above must be included in any redistribution.
//
// Edit : Tab size set to 2 but I converted tab to sapces
//
// **********************************************************************************
#ifndef LibTeleinfo_h
#define LibTeleinfo_h
#ifdef __arm__
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define boolean bool
#endif
#ifdef ARDUINO
#include <Arduino.h>
#endif
// Using ESP8266 ?
#ifdef ESP8266
#include <ESP8266WiFi.h>
#endif
// Define this if you want library to be verbose
//#define TI_DEBUG
// I prefix debug macro to be sure to use specific for THIS library
// debugging, this should not interfere with main sketch or other
// libraries
#ifdef TI_DEBUG
#ifdef ESP8266
#define TI_Debug(x) Serial1.print(x)
#define TI_Debugln(x) Serial1.println(x)
#define TI_Debugf(...) Serial1.printf(__VA_ARGS__)
#define TI_Debugflush Serial1.flush
#else
#define TI_Debug(x) Serial.print(x)
#define TI_Debugln(x) Serial.println(x)
#define TI_Debugf(...) Serial.printf(__VA_ARGS__)
#define TI_Debugflush Serial.flush
#endif
#else
#define TI_Debug(x)
#define TI_Debugln(x)
#define TI_Debugf(...)
#define TI_Debugflush
#endif
#ifdef ESP8266
// For 4 bytes Aligment boundaries
#define ESP8266_allocAlign(size) ((size + 3) & ~((size_t) 3))
#endif
#pragma pack(push) // push current alignment to stack
#pragma pack(1) // set alignment to 1 byte boundary
// Linked list structure containing all values received
typedef struct _ValueList ValueList;
struct _ValueList
{
ValueList *next; // next element
uint8_t checksum;// checksum
uint8_t flags; // specific flags
char * name; // LABEL of value name
char * value; // value
};
#pragma pack(pop)
// Library state machine
enum _State_e {
TINFO_INIT, // We're in init
TINFO_WAIT_STX, // We're waiting for STX
TINFO_WAIT_ETX, // We had STX, We're waiting for ETX
TINFO_READY // We had STX AND ETX, So we're OK
};
// what we done with received value (also for callback flags)
#define TINFO_FLAGS_NONE 0x00
#define TINFO_FLAGS_NOTHING 0x01
#define TINFO_FLAGS_ADDED 0x02
#define TINFO_FLAGS_EXIST 0x04
#define TINFO_FLAGS_UPDATED 0x08
#define TINFO_FLAGS_ALERT 0x80 /* This will generate an alert */
// Local buffer for one line of teleinfo
// maximum size, I think it should be enought
#define TINFO_BUFSIZE 64
// Teleinfo start and end of frame characters
#define TINFO_STX 0x02
#define TINFO_ETX 0x03
#define TINFO_SGR '\n' // start of group
#define TINFO_EGR '\r' // End of group
class TInfo
{
public:
TInfo();
void init();
_State_e process (char c);
void attachADPS(void (*_fn_ADPS)(uint8_t phase));
void attachData(void (*_fn_data)(ValueList * valueslist, uint8_t state));
void attachNewFrame(void (*_fn_new_frame)(ValueList * valueslist));
void attachUpdatedFrame(void (*_fn_updated_frame)(ValueList * valueslist));
ValueList * addCustomValue(char * name, char * value, uint8_t * flags);
ValueList * getList(void);
uint8_t valuesDump(void);
char * valueGet(char * name, char * value);
boolean listDelete();
unsigned char calcChecksum(char *etiquette, char *valeur);
private:
uint8_t clearBuffer();
ValueList * valueAdd (char * name, char * value, uint8_t checksum, uint8_t * flags);
boolean valueRemove (char * name);
boolean valueRemoveFlagged(uint8_t flags);
int labelCount();
void customLabel( char * plabel, char * pvalue, uint8_t * pflags) ;
ValueList * checkLine(char * pline) ;
_State_e _state; // Teleinfo machine state
ValueList _valueslist; // Linked list of teleinfo values
char _recv_buff[TINFO_BUFSIZE]; // line receive buffer
uint8_t _recv_idx; // index in receive buffer
boolean _frame_updated; // Data on the frame has been updated
void (*_fn_ADPS)(uint8_t phase);
void (*_fn_data)(ValueList * valueslist, uint8_t state);
void (*_fn_new_frame)(ValueList * valueslist);
void (*_fn_updated_frame)(ValueList * valueslist);
//volatile uint8_t *dcport;
//uint8_t dcpinmask;
};
#endif