@@ -24,41 +24,45 @@ pilight string: `c:0100020002000200020002000200020002000200020002000200020002000
24
24
25
25
decode as:
26
26
``` json
27
- [{
28
- "smartwares_switch" : {
29
- "id" : 92 ,
30
- "unit" : 0 ,
31
- "state" : " on"
32
- }
33
- },
34
27
{
35
- "arctech_switch" : {
36
- "id" : 92 ,
37
- "unit" : 0 ,
38
- "state" : " on"
39
- }
40
- },
41
- {
42
- "arctech_screen" : {
43
- "id" : 92 ,
44
- "unit" : 0 ,
45
- "state" : " up"
46
- }
47
- },
48
- {
49
- "arctech_dimmer" : {
50
- "id" : 92 ,
51
- "unit" : 0 ,
52
- "state" : " on"
53
- }
54
- },
55
- {
56
- "arctech_contact" : {
57
- "id" : 92 ,
58
- "unit" : 0 ,
59
- "state" : " opened"
60
- }
61
- }]
28
+ "protocols" : [
29
+ {
30
+ "arctech_contact" : {
31
+ "id" : 92 ,
32
+ "unit" : 0 ,
33
+ "state" : " opened"
34
+ }
35
+ },
36
+ {
37
+ "arctech_dimmer" : {
38
+ "id" : 92 ,
39
+ "unit" : 0 ,
40
+ "state" : " on"
41
+ }
42
+ },
43
+ {
44
+ "arctech_screen" : {
45
+ "id" : 92 ,
46
+ "unit" : 0 ,
47
+ "state" : " up"
48
+ }
49
+ },
50
+ {
51
+ "arctech_switch" : {
52
+ "id" : 92 ,
53
+ "unit" : 0 ,
54
+ "state" : " on"
55
+ }
56
+ },
57
+ {
58
+ "smartwares_switch" : {
59
+ "id" : 92 ,
60
+ "unit" : 0 ,
61
+ "state" : " on"
62
+ }
63
+ }
64
+ ]
65
+ }
62
66
```
63
67
64
68
To encode to pilight string, you must provide the protocol and custom parameters of that protocol in json format.
@@ -69,7 +73,7 @@ Example: `{"arctech_switch":{"id":92,"unit":0,"on":1}}`
69
73
## BUILD
70
74
No external depends, can run on any libc/libc++ compatible system, like macOS, FreeBSD, Linux, even Windows.
71
75
```
72
- $ git clone https://github.com/latchdevel/PiCode (or download .zip)
76
+ $ git clone https://github.com/latchdevel/PiCode (or download .zip)
73
77
$ cd PiCode
74
78
$ mkdir build
75
79
$ cd build
@@ -81,92 +85,6 @@ No external depends, can run on any libc/libc++ compatible system, like macOS, F
81
85
$ make uninstall (to uninstall)
82
86
```
83
87
84
- ## C++ example
85
- ``` cpp
86
- /*
87
- Example of using the PiCode Library
88
-
89
- https://github.com/latchdevel/PiCode
90
-
91
- Copyright (c) 2021 Jorge Rivera. All right reserved.
92
- License GNU Lesser General Public License v3.0.
93
-
94
- */
95
-
96
- #include < cstdio> /* printf() */
97
- #include < cstdlib> /* free() */
98
- #include < cinttypes> /* uint8_t */
99
-
100
- #include " src/PiCode.h" /* PiCode object class */
101
-
102
- int main (){
103
-
104
- int result = 0;
105
-
106
- printf ("picode_example (%s)\n", STRINGIFY(BUILD_VERSION));
107
- printf("Compiled at " __ DATE__ " " __ TIME__ " %s (%s)\n",STRINGIFY(BUILD_COMPILER), BUILD_TYPE );
108
-
109
- /* Get PiCode library version */
110
-
111
- char* library_version = PiCode.getPiCodeVersion();
112
-
113
- if (library_version){
114
-
115
- printf ("PiCode library version: %s\n", library_version);
116
-
117
- free (library_version);
118
-
119
- }else{
120
- printf ("ERROR: Unable to get PiCode library version.\n");
121
- result--;
122
- }
123
-
124
- printf ("\n");
125
-
126
- /* Decode from pilight string */
127
-
128
- char* pilight_string = (char*) "c:011010100101011010100110101001100110010101100110101010101010101012;p:1400,600,6800@";
129
-
130
- char* decoded_string = PiCode.decodeString(pilight_string);
131
-
132
- printf ("String to decode: \" %s\" \n",pilight_string);
133
-
134
- if (decoded_string){
135
-
136
- printf ("Decode string successful:\n");
137
- printf("%s\n",decoded_string);
138
-
139
- free (decoded_string);
140
-
141
- }else{
142
- printf("ERROR: Unable to decode string.\n");
143
- result--;
144
- }
145
-
146
- /* Encode to pilight string from json */
147
-
148
- char* json = (char*) "{ 'arctech_switch' : { 'id': 92, 'unit': 0, 'on': 1 }}";
149
- uint8_t repeats = 5;
150
-
151
- char* encoded_json_string = PiCode.encodeJson(json,repeats);
152
-
153
- printf ("\nJSON to encode: \" %s\" \n",json);
154
-
155
- if (encoded_json_string){
156
-
157
- printf ("Encode successful:\n");
158
- printf("%s\n",encoded_json_string);
159
-
160
- free (encoded_json_string);
161
-
162
- }else{
163
- printf("ERROR: Unable to encode JSON.\n");
164
- result--;
165
- }
166
- printf("\n");
167
- return result;
168
- }
169
- ```
170
88
171
89
## C example
172
90
``` c
@@ -246,26 +164,75 @@ int main(){
246
164
printf("ERROR: Unable to encode JSON.\n");
247
165
result--;
248
166
}
167
+
168
+ /* Encode from protocol name and json data to array of pulses if success */
169
+
170
+ char* protocol_name = (char*) "arctech_switch";
171
+ char* json_data = (char*) "{'id': 92, 'unit': 0, 'on': 1}";
172
+
173
+ uint32_t* pulses = NULL;
174
+ uint16_t n_pulses_max = 0;
175
+ int n_pulses = 0;
176
+
177
+ n_pulses_max = protocol_maxrawlen();
178
+
179
+ pulses = (uint32_t*)malloc(sizeof *pulses * (n_pulses_max + 1));
180
+
181
+ if (pulses != NULL){
182
+
183
+ printf ("\nEncode protocol: \" %s\" JSON data: \" %s\" \n",protocol_name,json_data);
184
+
185
+ n_pulses = encodeToPulseTrainByName(pulses, n_pulses_max, protocol_name, json_data);
186
+
187
+ if (n_pulses>0){
188
+ printf ("Encode successful:\n");
189
+ printf("pulses[ %d] ={",n_pulses);
190
+ for (int i = 0; i<n_pulses; i++){
191
+ printf("%d",pulses[ i] );
192
+ if (i<n_pulses-1){
193
+ printf(",");
194
+ }else{
195
+ printf("};\n");
196
+ }
197
+ }
198
+ }else{
199
+ printf("ERROR: Unable to encode (%i)\n",n_pulses);
200
+ }
201
+ free(pulses);
202
+ }else{
203
+ printf("ERROR: out of memory (%d)) \n",n_pulses_max);
204
+ result--;
205
+ }
206
+
249
207
printf ("\n");
250
208
return result;
251
209
}
252
210
```
253
211
254
212
## Output
255
213
```
214
+ Compiled at Oct 6 2022 23:10:26 AppleClang 13.0.0.13000029 (Release)
215
+ PiCode library version: v1.4
216
+
256
217
String to decode: "c:011010100101011010100110101001100110010101100110101010101010101012;p:1400,600,6800@"
257
218
Decode string successful:
258
- [{
259
- "conrad_rsl_switch": {
260
- "id": 1,
261
- "unit": 2,
262
- "state": "on"
263
- }
264
- }]
219
+ {
220
+ "protocols": [{
221
+ "conrad_rsl_switch": {
222
+ "id": 1,
223
+ "unit": 2,
224
+ "state": "on"
225
+ }
226
+ }]
227
+ }
265
228
266
229
JSON to encode: "{ 'arctech_switch' : { 'id': 92, 'unit': 0, 'on': 1 }}"
267
230
Encode successful:
268
231
c:010002000200020002000200020002000200020002000200020002000200020002000200020002020000020200020002000002000200020200000200020002000203;p:315,2835,1260,10710;r:5@
232
+
233
+ Encode protocol: "arctech_switch" JSON data: "{'id': 92, 'unit': 0, 'on': 1}"
234
+ Encode successful:
235
+ pulses[132]={315,2835,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,1260,315,315,315,315,315,1260,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,1260,315,315,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,315,315,1260,315,10710};
269
236
```
270
237
271
238
# License
0 commit comments