1
1
# PiCode Library
2
2
3
- C++ class to manage OOK protocols supported by [ ** "pilight"** ] ( https://github.com/pilight/pilight ) project.
3
+ C/C++ library to manage OOK protocols supported by [ ** "pilight"** ] ( https://github.com/pilight/pilight ) project.
4
+
5
+ Works on any libc/libc++ compatible system, like macOS, FreeBSD, Linux, even Windows.
4
6
5
7
[ ![ License: LGPL v3] ( https://img.shields.io/badge/License-LGPL%20v3-blue.svg )] ( https://www.gnu.org/licenses/lgpl-3.0 )
6
8
[ ![ Build tests] ( https://github.com/latchdevel/PiCode/actions/workflows/BuildTest.yml/badge.svg )] ( https://github.com/latchdevel/PiCode/actions/workflows/BuildTest.yml )
@@ -65,7 +67,7 @@ Example: `{"arctech_switch":{"id":92,"unit":0,"on":1}}`
65
67
66
68
67
69
## BUILD
68
- No external depends, can run on any libc/libc++ compatible system, like MacOS , FreeBSD, Linux, even Windows.
70
+ No external depends, can run on any libc/libc++ compatible system, like macOS , FreeBSD, Linux, even Windows.
69
71
```
70
72
$ git clone https://github.com/latchdevel/PiCode (or download .zip)
71
73
$ cd PiCode
@@ -74,11 +76,12 @@ No external depends, can run on any libc/libc++ compatible system, like MacOS, F
74
76
$ cmake .. (or "cmake -DCMAKE_BUILD_TYPE=debug .." for debug)
75
77
$ make
76
78
$ make install (optional)
77
- # make picode_example (optional)
79
+ # make picode_example (optional C++ example)
80
+ # make cpicode_example (optional C example)
78
81
$ make uninstall (to uninstall)
79
82
```
80
83
81
- ## Example
84
+ ## C++ example
82
85
``` cpp
83
86
/*
84
87
Example of using the PiCode Library
@@ -90,17 +93,44 @@ No external depends, can run on any libc/libc++ compatible system, like MacOS, F
90
93
91
94
*/
92
95
93
- #include " src/PiCode.h"
94
- #include < stdio.h>
96
+ #include < cstdio> /* printf() */
97
+ #include < cstdlib> /* free() */
98
+ #include < cinttypes> /* uint8_t */
99
+
100
+ #include " src/PiCode.h" /* PiCode object class */
95
101
96
102
int main (){
97
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
+
98
126
/* Decode from pilight string */
99
127
100
128
char* pilight_string = (char*) "c:011010100101011010100110101001100110010101100110101010101010101012;p:1400,600,6800@";
101
129
102
130
char* decoded_string = PiCode.decodeString(pilight_string);
103
131
132
+ printf ("String to decode: \" %s\" \n",pilight_string);
133
+
104
134
if (decoded_string){
105
135
106
136
printf ("Decode string successful:\n");
@@ -109,7 +139,8 @@ int main(){
109
139
free (decoded_string);
110
140
111
141
}else{
112
- printf("Unable to decode string\n");
142
+ printf("ERROR: Unable to decode string.\n");
143
+ result--;
113
144
}
114
145
115
146
/* Encode to pilight string from json */
@@ -119,6 +150,8 @@ int main(){
119
150
120
151
char* encoded_json_string = PiCode.encodeJson(json,repeats);
121
152
153
+ printf ("\nJSON to encode: \" %s\" \n",json);
154
+
122
155
if (encoded_json_string){
123
156
124
157
printf ("Encode successful:\n");
@@ -127,15 +160,100 @@ int main(){
127
160
free (encoded_json_string);
128
161
129
162
}else{
130
- printf("Unable to encode\n");
163
+ printf("ERROR: Unable to encode JSON.\n");
164
+ result--;
165
+ }
166
+ printf("\n");
167
+ return result;
168
+ }
169
+ ```
170
+
171
+ ## C example
172
+ ``` c
173
+ /*
174
+ Example of using the pure C PiCode Library
175
+
176
+ https://github.com/latchdevel/PiCode
177
+
178
+ Copyright (c) 2021 Jorge Rivera. All right reserved.
179
+ License GNU Lesser General Public License v3.0.
180
+
181
+ */
182
+
183
+ #include < stdio.h> /* printf() */
184
+ #include < stdlib.h> /* free(), uint8_t */
185
+
186
+ #include " src/cPiCode.h" /* Pure C PiCode library */
187
+
188
+ int main (){
189
+
190
+ int result = 0;
191
+
192
+ printf ("cpicode_example (%s)\n", STRINGIFY(BUILD_VERSION));
193
+ printf("Compiled at " __ DATE__ " " __ TIME__ " %s (%s)\n",STRINGIFY(BUILD_COMPILER), BUILD_TYPE );
194
+
195
+ /* Get PiCode library version */
196
+
197
+ char* library_version = getPiCodeVersion();
198
+
199
+ if (library_version){
200
+ printf ("PiCode library version: %s\n", library_version);
201
+ free(library_version);
202
+ }else{
203
+ printf("ERROR: Unable to get PiCode library version.\n");
204
+ result--;
205
+ }
206
+
207
+ printf ("\n");
208
+
209
+ /* Decode from pilight string */
210
+
211
+ char* pilight_string = (char*) "c:011010100101011010100110101001100110010101100110101010101010101012;p:1400,600,6800@";
212
+
213
+ char* decoded_string = decodeString(pilight_string);
214
+
215
+ printf ("String to decode: \" %s\" \n",pilight_string);
216
+
217
+ if (decoded_string){
218
+
219
+ printf ("Decode string successful:\n");
220
+ printf("%s\n",decoded_string);
221
+
222
+ free (decoded_string);
223
+
224
+ }else{
225
+ printf("ERROR: Unable to decode string.\n");
226
+ result--;
131
227
}
132
228
133
- return 0;
229
+ /* Encode to pilight string from json */
230
+
231
+ char* json = (char*) "{ 'arctech_switch' : { 'id': 92, 'unit': 0, 'on': 1 }}";
232
+ uint8_t repeats = 5;
233
+
234
+ char* encoded_json_string = encodeJson(json,repeats);
235
+
236
+ printf ("\nJSON to encode: \" %s\" \n",json);
237
+
238
+ if (encoded_json_string){
239
+
240
+ printf ("Encode successful:\n");
241
+ printf("%s\n",encoded_json_string);
242
+
243
+ free (encoded_json_string);
244
+
245
+ }else{
246
+ printf("ERROR: Unable to encode JSON.\n");
247
+ result--;
248
+ }
249
+ printf("\n");
250
+ return result;
134
251
}
135
252
```
136
253
137
254
## Output
138
255
```
256
+ String to decode: "c:011010100101011010100110101001100110010101100110101010101010101012;p:1400,600,6800@"
139
257
Decode string successful:
140
258
[{
141
259
"conrad_rsl_switch": {
@@ -144,12 +262,14 @@ Decode string successful:
144
262
"state": "on"
145
263
}
146
264
}]
265
+
266
+ JSON to encode: "{ 'arctech_switch' : { 'id': 92, 'unit': 0, 'on': 1 }}"
147
267
Encode successful:
148
- c:010002000200020002000200020002000200020002000200020002000200020002000200020002020000020200020002000002000200020200000200020002000203;p:315,2835,1260,10710;r:5
268
+ c:010002000200020002000200020002000200020002000200020002000200020002000200020002020000020200020002000002000200020200000200020002000203;p:315,2835,1260,10710;r:5@
149
269
```
150
270
151
271
# License
152
- Copyright (c) 2021 Jorge Rivera. All right reserved.
272
+ Copyright (c) 2021-2022 Jorge Rivera. All right reserved.
153
273
154
274
License GNU Lesser General Public License v3.0.
155
275
0 commit comments