forked from joan2937/pigpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pigpiod_if2.c
1659 lines (1258 loc) · 33.8 KB
/
pigpiod_if2.c
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
/* PIGPIOD_IF2_VERSION 1 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <netdb.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include "pigpio.h"
#include "command.h"
#include "pigpiod_if2.h"
#define PI_MAX_REPORTS_PER_READ 4096
#define STACK_SIZE (256*1024)
#define MAX_PI 32
typedef void (*CBF_t) ();
struct callback_s
{
int id;
int pi;
int gpio;
int edge;
CBF_t f;
void * user;
int ex;
callback_t *prev;
callback_t *next;
};
/* GLOBALS ---------------------------------------------------------------- */
static int gPiInUse [MAX_PI];
static int gPigCommand [MAX_PI];
static int gPigHandle [MAX_PI];
static int gPigNotify [MAX_PI];
static uint32_t gNotifyBits [MAX_PI];
static uint32_t gLastLevel [MAX_PI];
static pthread_t *gPthNotify [MAX_PI];
static pthread_mutex_t gCmdMutex [MAX_PI];
static int gCancelState [MAX_PI];
static callback_t *gCallBackFirst = 0;
static callback_t *gCallBackLast = 0;
/* PRIVATE ---------------------------------------------------------------- */
static void _pml(int pi)
{
int cancelState;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancelState);
pthread_mutex_lock(&gCmdMutex[pi]);
gCancelState[pi] = cancelState;
}
static void _pmu(int pi)
{
int cancelState;
cancelState = gCancelState[pi];
pthread_mutex_unlock(&gCmdMutex[pi]);
pthread_setcancelstate(cancelState, NULL);
}
static int pigpio_command(int pi, int command, int p1, int p2, int rl)
{
cmdCmd_t cmd;
if ((pi < 0) || (pi >= MAX_PI) || !gPiInUse[pi])
return pigif_unconnected_pi;
cmd.cmd = command;
cmd.p1 = p1;
cmd.p2 = p2;
cmd.res = 0;
_pml(pi);
if (send(gPigCommand[pi], &cmd, sizeof(cmd), 0) != sizeof(cmd))
{
_pmu(pi);
return pigif_bad_send;
}
if (recv(gPigCommand[pi], &cmd, sizeof(cmd), MSG_WAITALL) != sizeof(cmd))
{
_pmu(pi);
return pigif_bad_recv;
}
if (rl) _pmu(pi);
return cmd.res;
}
static int pigpio_notify(int pi)
{
cmdCmd_t cmd;
if ((pi < 0) || (pi >= MAX_PI) || !gPiInUse[pi])
return pigif_unconnected_pi;
cmd.cmd = PI_CMD_NOIB;
cmd.p1 = 0;
cmd.p2 = 0;
cmd.res = 0;
_pml(pi);
if (send(gPigNotify[pi], &cmd, sizeof(cmd), 0) != sizeof(cmd))
{
_pmu(pi);
return pigif_bad_send;
}
if (recv(gPigNotify[pi], &cmd, sizeof(cmd), MSG_WAITALL) != sizeof(cmd))
{
_pmu(pi);
return pigif_bad_recv;
}
_pmu(pi);
return cmd.res;
}
static int pigpio_command_ext
(int pi, int command, int p1, int p2, int p3,
int extents, gpioExtent_t *ext, int rl)
{
int i;
cmdCmd_t cmd;
if ((pi < 0) || (pi >= MAX_PI) || !gPiInUse[pi])
return pigif_unconnected_pi;
cmd.cmd = command;
cmd.p1 = p1;
cmd.p2 = p2;
cmd.p3 = p3;
_pml(pi);
if (send(gPigCommand[pi], &cmd, sizeof(cmd), 0) != sizeof(cmd))
{
_pmu(pi);
return pigif_bad_send;
}
for (i=0; i<extents; i++)
{
if (send(gPigCommand[pi], ext[i].ptr, ext[i].size, 0) != ext[i].size)
{
_pmu(pi);
return pigif_bad_send;
}
}
if (recv(gPigCommand[pi], &cmd, sizeof(cmd), MSG_WAITALL) != sizeof(cmd))
{
_pmu(pi);
return pigif_bad_recv;
}
if (rl) _pmu(pi);
return cmd.res;
}
static int pigpioOpenSocket(char *addr, char *port)
{
int sock, err, opt;
struct addrinfo hints, *res, *rp;
const char *addrStr, *portStr;
if (!addr)
{
addrStr = getenv(PI_ENVADDR);
if ((!addrStr) || (!strlen(addrStr)))
{
addrStr = PI_DEFAULT_SOCKET_ADDR_STR;
}
}
else addrStr = addr;
if (!port)
{
portStr = getenv(PI_ENVPORT);
if ((!portStr) || (!strlen(portStr)))
{
portStr = PI_DEFAULT_SOCKET_PORT_STR;
}
}
else portStr = port;
memset (&hints, 0, sizeof (hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags |= AI_CANONNAME;
err = getaddrinfo (addrStr, portStr, &hints, &res);
if (err) return pigif_bad_getaddrinfo;
for (rp=res; rp!=NULL; rp=rp->ai_next)
{
sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sock == -1) continue;
/* Disable the Nagle algorithm. */
opt = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&opt, sizeof(int));
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) break;
}
freeaddrinfo(res);
if (rp == NULL) return pigif_bad_connect;
return sock;
}
static void dispatch_notification(int pi, gpioReport_t *r)
{
callback_t *p;
uint32_t changed;
int l, g;
/*
printf("s=%4x f=%4x t=%10u l=%8x\n",
r->seqno, r->flags, r->tick, r->level);
*/
if (r->flags == 0)
{
changed = (r->level ^ gLastLevel[pi]) & gNotifyBits[pi];
gLastLevel[pi] = r->level;
p = gCallBackFirst;
while (p)
{
if (((p->pi) == pi) && (changed & (1<<(p->gpio))))
{
if ((r->level) & (1<<(p->gpio))) l = 1; else l = 0;
if ((p->edge) ^ l)
{
if (p->ex) (p->f)(pi, p->gpio, l, r->tick, p->user);
else (p->f)(pi, p->gpio, l, r->tick);
}
}
p = p->next;
}
}
else
{
g = (r->flags) & 31;
p = gCallBackFirst;
while (p)
{
if (((p->pi) == pi) && ((p->gpio) == g))
{
if (p->ex) (p->f)(pi, g, PI_TIMEOUT, r->tick, p->user);
else (p->f)(pi, g, PI_TIMEOUT, r->tick);
}
p = p->next;
}
}
}
static void *pthNotifyThread(void *x)
{
static int got = 0;
int pi;
int bytes, r;
gpioReport_t report[PI_MAX_REPORTS_PER_READ];
pi = *((int*)x);
free(x); /* memory allocated in pigpio_start */
while (1)
{
bytes = read(gPigNotify[pi], (char*)&report+got, sizeof(report)-got);
if (bytes > 0) got += bytes;
else break;
r = 0;
while (got >= sizeof(gpioReport_t))
{
dispatch_notification(pi, &report[r]);
r++;
got -= sizeof(gpioReport_t);
}
/* copy any partial report to start of array */
if (got && r) report[0] = report[r];
}
fprintf(stderr, "notify thread for pi %d broke with read error %d\n",
pi, bytes);
while (1) sleep(1);
return NULL;
}
static void findNotifyBits(int pi)
{
callback_t *p;
uint32_t bits = 0;
p = gCallBackFirst;
while (p)
{
if (p->pi == pi) bits |= (1<<(p->gpio));
p = p->next;
}
if (bits != gNotifyBits[pi])
{
gNotifyBits[pi] = bits;
pigpio_command(pi, PI_CMD_NB, gPigHandle[pi], gNotifyBits[pi], 1);
}
}
static void _wfe(
int pi, unsigned user_gpio, unsigned level, uint32_t tick, void *user)
{
*(int *)user = 1;
}
static int intCallback(
int pi, unsigned user_gpio, unsigned edge, void *f, void *user, int ex)
{
static int id = 0;
callback_t *p;
if ((user_gpio >=0) && (user_gpio < 32) && (edge >=0) && (edge <= 2) && f)
{
/* prevent duplicates */
p = gCallBackFirst;
while (p)
{
if ((p->pi == pi) &&
(p->gpio == user_gpio) &&
(p->edge == edge) &&
(p->f == f))
{
return pigif_duplicate_callback;
}
p = p->next;
}
p = malloc(sizeof(callback_t));
if (p)
{
if (!gCallBackFirst) gCallBackFirst = p;
p->id = id++;
p->pi = pi;
p->gpio = user_gpio;
p->edge = edge;
p->f = f;
p->user = user;
p->ex = ex;
p->next = 0;
p->prev = gCallBackLast;
if (p->prev) (p->prev)->next = p;
gCallBackLast = p;
findNotifyBits(pi);
return p->id;
}
return pigif_bad_malloc;
}
return pigif_bad_callback;
}
static int recvMax(int pi, void *buf, int bufsize, int sent)
{
uint8_t scratch[4096];
int remaining, fetch, count;
if (sent < bufsize) count = sent; else count = bufsize;
if (count) recv(gPigCommand[pi], buf, count, MSG_WAITALL);
remaining = sent - count;
while (remaining)
{
fetch = remaining;
if (fetch > sizeof(scratch)) fetch = sizeof(scratch);
recv(gPigCommand[pi], scratch, fetch, MSG_WAITALL);
remaining -= fetch;
}
return count;
}
/* PUBLIC ----------------------------------------------------------------- */
double time_time(void)
{
struct timeval tv;
double t;
gettimeofday(&tv, 0);
t = (double)tv.tv_sec + ((double)tv.tv_usec / 1E6);
return t;
}
void time_sleep(double seconds)
{
struct timespec ts, rem;
if (seconds > 0.0)
{
ts.tv_sec = seconds;
ts.tv_nsec = (seconds-(double)ts.tv_sec) * 1E9;
while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &rem))
{
/* copy remaining time to ts */
ts.tv_sec = rem.tv_sec;
ts.tv_nsec = rem.tv_nsec;
}
}
}
char *pigpio_error(int errnum)
{
if (errnum > -1000) return cmdErrStr(errnum);
else
{
switch(errnum)
{
case pigif_bad_send:
return "failed to send to pigpiod";
case pigif_bad_recv:
return "failed to receive from pigpiod";
case pigif_bad_getaddrinfo:
return "failed to find address of pigpiod";
case pigif_bad_connect:
return "failed to connect to pigpiod";
case pigif_bad_socket:
return "failed to create socket";
case pigif_bad_noib:
return "failed to open notification in band";
case pigif_duplicate_callback:
return "identical callback exists";
case pigif_bad_malloc:
return "failed to malloc";
case pigif_bad_callback:
return "bad callback parameter";
case pigif_notify_failed:
return "failed to create notification thread";
case pigif_callback_not_found:
return "callback not found";
case pigif_unconnected_pi:
return "not connected to Pi";
case pigif_too_many_pis:
return "too many connected Pis";
default:
return "unknown error";
}
}
}
unsigned pigpiod_if_version(void)
{
return PIGPIOD_IF2_VERSION;
}
pthread_t *start_thread(gpioThreadFunc_t thread_func, void *userdata)
{
pthread_t *pth;
pthread_attr_t pthAttr;
pth = malloc(sizeof(pthread_t));
if (pth)
{
if (pthread_attr_init(&pthAttr))
{
perror("pthread_attr_init failed");
free(pth);
return NULL;
}
if (pthread_attr_setstacksize(&pthAttr, STACK_SIZE))
{
perror("pthread_attr_setstacksize failed");
free(pth);
return NULL;
}
if (pthread_create(pth, &pthAttr, thread_func, userdata))
{
perror("pthread_create socket failed");
free(pth);
return NULL;
}
}
return pth;
}
void stop_thread(pthread_t *pth)
{
if (pth)
{
pthread_cancel(*pth);
pthread_join(*pth, NULL);
free(pth);
}
}
int pigpio_start(char *addrStr, char *portStr)
{
int pi;
int *userdata;
for (pi=0; pi<MAX_PI; pi++)
{
if (!gPiInUse[pi]) break;
}
if (pi >= MAX_PI) return pigif_too_many_pis;
gPiInUse[pi] = 1;
pthread_mutex_init(&gCmdMutex[pi], NULL);
gPigCommand[pi] = pigpioOpenSocket(addrStr, portStr);
if (gPigCommand[pi] >= 0)
{
gPigNotify[pi] = pigpioOpenSocket(addrStr, portStr);
if (gPigNotify[pi] >= 0)
{
gPigHandle[pi] = pigpio_notify(pi);
if (gPigHandle[pi] < 0) return pigif_bad_noib;
else
{
gLastLevel[pi] = read_bank_1(pi);
/* must be freed by pthNotifyThread */
userdata = malloc(sizeof(*userdata));
*userdata = pi;
gPthNotify[pi] = start_thread(pthNotifyThread, userdata);
if (gPthNotify[pi]) return pi;
else return pigif_notify_failed;
}
}
else return gPigNotify[pi];
}
else return gPigCommand[pi];
}
void pigpio_stop(int pi)
{
if ((pi < 0) || (pi >= MAX_PI) || !gPiInUse[pi]) return;
if (gPthNotify[pi])
{
stop_thread(gPthNotify[pi]);
gPthNotify[pi] = 0;
}
if (gPigCommand[pi] >= 0)
{
if (gPigHandle[pi] >= 0)
{
pigpio_command(pi, PI_CMD_NC, gPigHandle[pi], 0, 1);
gPigHandle[pi] = -1;
}
close(gPigCommand[pi]);
gPigCommand[pi] = -1;
}
if (gPigNotify[pi] >= 0)
{
close(gPigNotify[pi]);
gPigNotify[pi] = -1;
}
gPiInUse[pi] = 0;
}
int set_mode(int pi, unsigned gpio, unsigned mode)
{return pigpio_command(pi, PI_CMD_MODES, gpio, mode, 1);}
int get_mode(int pi, unsigned gpio)
{return pigpio_command(pi, PI_CMD_MODEG, gpio, 0, 1);}
int set_pull_up_down(int pi, unsigned gpio, unsigned pud)
{return pigpio_command(pi, PI_CMD_PUD, gpio, pud, 1);}
int gpio_read(int pi, unsigned gpio)
{return pigpio_command(pi, PI_CMD_READ, gpio, 0, 1);}
int gpio_write(int pi, unsigned gpio, unsigned level)
{return pigpio_command(pi, PI_CMD_WRITE, gpio, level, 1);}
int set_PWM_dutycycle(int pi, unsigned user_gpio, unsigned dutycycle)
{return pigpio_command(pi, PI_CMD_PWM, user_gpio, dutycycle, 1);}
int get_PWM_dutycycle(int pi, unsigned user_gpio)
{return pigpio_command(pi, PI_CMD_GDC, user_gpio, 0, 1);}
int set_PWM_range(int pi, unsigned user_gpio, unsigned range)
{return pigpio_command(pi, PI_CMD_PRS, user_gpio, range, 1);}
int get_PWM_range(int pi, unsigned user_gpio)
{return pigpio_command(pi, PI_CMD_PRG, user_gpio, 0, 1);}
int get_PWM_real_range(int pi, unsigned user_gpio)
{return pigpio_command(pi, PI_CMD_PRRG, user_gpio, 0, 1);}
int set_PWM_frequency(int pi, unsigned user_gpio, unsigned frequency)
{return pigpio_command(pi, PI_CMD_PFS, user_gpio, frequency, 1);}
int get_PWM_frequency(int pi, unsigned user_gpio)
{return pigpio_command(pi, PI_CMD_PFG, user_gpio, 0, 1);}
int set_servo_pulsewidth(int pi, unsigned user_gpio, unsigned pulsewidth)
{return pigpio_command(pi, PI_CMD_SERVO, user_gpio, pulsewidth, 1);}
int get_servo_pulsewidth(int pi, unsigned user_gpio)
{return pigpio_command(pi, PI_CMD_GPW, user_gpio, 0, 1);}
int notify_open(int pi)
{return pigpio_command(pi, PI_CMD_NO, 0, 0, 1);}
int notify_begin(int pi, unsigned handle, uint32_t bits)
{return pigpio_command(pi, PI_CMD_NB, handle, bits, 1);}
int notify_pause(int pi, unsigned handle)
{return pigpio_command(pi, PI_CMD_NB, handle, 0, 1);}
int notify_close(int pi, unsigned handle)
{return pigpio_command(pi, PI_CMD_NC, handle, 0, 1);}
int set_watchdog(int pi, unsigned user_gpio, unsigned timeout)
{return pigpio_command(pi, PI_CMD_WDOG, user_gpio, timeout, 1);}
uint32_t read_bank_1(int pi)
{return pigpio_command(pi, PI_CMD_BR1, 0, 0, 1);}
uint32_t read_bank_2(int pi)
{return pigpio_command(pi, PI_CMD_BR2, 0, 0, 1);}
int clear_bank_1(int pi, uint32_t levels)
{return pigpio_command(pi, PI_CMD_BC1, levels, 0, 1);}
int clear_bank_2(int pi, uint32_t levels)
{return pigpio_command(pi, PI_CMD_BC2, levels, 0, 1);}
int set_bank_1(int pi, uint32_t levels)
{return pigpio_command(pi, PI_CMD_BS1, levels, 0, 1);}
int set_bank_2(int pi, uint32_t levels)
{return pigpio_command(pi, PI_CMD_BS2, levels, 0, 1);}
int hardware_clock(int pi, unsigned gpio, unsigned frequency)
{return pigpio_command(pi, PI_CMD_HC, gpio, frequency, 1);}
int hardware_PWM(int pi, unsigned gpio, unsigned frequency, uint32_t dutycycle)
{
gpioExtent_t ext[1];
/*
p1=gpio
p2=frequency
p3=4
## extension ##
uint32_t dutycycle
*/
ext[0].size = sizeof(dutycycle);
ext[0].ptr = &dutycycle;
return pigpio_command_ext(
pi, PI_CMD_HP, gpio, frequency, sizeof(dutycycle), 1, ext, 1);
}
uint32_t get_current_tick(int pi)
{return pigpio_command(pi, PI_CMD_TICK, 0, 0, 1);}
uint32_t get_hardware_revision(int pi)
{return pigpio_command(pi, PI_CMD_HWVER, 0, 0, 1);}
uint32_t get_pigpio_version(int pi)
{return pigpio_command(pi, PI_CMD_PIGPV, 0, 0, 1);}
int wave_clear(int pi)
{return pigpio_command(pi, PI_CMD_WVCLR, 0, 0, 1);}
int wave_add_new(int pi)
{return pigpio_command(pi, PI_CMD_WVNEW, 0, 0, 1);}
int wave_add_generic(int pi, unsigned numPulses, gpioPulse_t *pulses)
{
gpioExtent_t ext[1];
/*
p1=0
p2=0
p3=pulses*sizeof(gpioPulse_t)
## extension ##
gpioPulse_t[] pulses
*/
if (!numPulses) return 0;
ext[0].size = numPulses * sizeof(gpioPulse_t);
ext[0].ptr = pulses;
return pigpio_command_ext(
pi, PI_CMD_WVAG, 0, 0, ext[0].size, 1, ext, 1);
}
int wave_add_serial(
int pi, unsigned user_gpio, unsigned baud, uint32_t databits,
uint32_t stophalfbits, uint32_t offset, unsigned numChar, char *str)
{
uint8_t buf[12];
gpioExtent_t ext[2];
/*
p1=user_gpio
p2=baud
p3=len+12
## extension ##
uint32_t databits
uint32_t stophalfbits
uint32_t offset
char[len] str
*/
if (!numChar) return 0;
memcpy(buf, &databits, 4);
memcpy(buf+4, &stophalfbits, 4);
memcpy(buf+8, &offset, 4);
ext[0].size = sizeof(buf);
ext[0].ptr = buf;
ext[1].size = numChar;
ext[1].ptr = str;
return pigpio_command_ext(pi, PI_CMD_WVAS,
user_gpio, baud, numChar+sizeof(buf), 2, ext, 1);
}
int wave_create(int pi)
{return pigpio_command(pi, PI_CMD_WVCRE, 0, 0, 1);}
int wave_delete(int pi, unsigned wave_id)
{return pigpio_command(pi, PI_CMD_WVDEL, wave_id, 0, 1);}
int wave_tx_start(int pi) /* DEPRECATED */
{return pigpio_command(pi, PI_CMD_WVGO, 0, 0, 1);}
int wave_tx_repeat(int pi) /* DEPRECATED */
{return pigpio_command(pi, PI_CMD_WVGOR, 0, 0, 1);}
int wave_send_once(int pi, unsigned wave_id)
{return pigpio_command(pi, PI_CMD_WVTX, wave_id, 0, 1);}
int wave_send_repeat(int pi, unsigned wave_id)
{return pigpio_command(pi, PI_CMD_WVTXR, wave_id, 0, 1);}
int wave_chain(int pi, char *buf, unsigned bufSize)
{
gpioExtent_t ext[1];
/*
p1=0
p2=0
p3=bufSize
## extension ##
char buf[bufSize]
*/
ext[0].size = bufSize;
ext[0].ptr = buf;
return pigpio_command_ext
(pi, PI_CMD_WVCHA, 0, 0, bufSize, 1, ext, 1);
}
int wave_tx_busy(int pi)
{return pigpio_command(pi, PI_CMD_WVBSY, 0, 0, 1);}
int wave_tx_stop(int pi)
{return pigpio_command(pi, PI_CMD_WVHLT, 0, 0, 1);}
int wave_get_micros(int pi)
{return pigpio_command(pi, PI_CMD_WVSM, 0, 0, 1);}
int wave_get_high_micros(int pi)
{return pigpio_command(pi, PI_CMD_WVSM, 1, 0, 1);}
int wave_get_max_micros(int pi)
{return pigpio_command(pi, PI_CMD_WVSM, 2, 0, 1);}
int wave_get_pulses(int pi)
{return pigpio_command(pi, PI_CMD_WVSP, 0, 0, 1);}
int wave_get_high_pulses(int pi)
{return pigpio_command(pi, PI_CMD_WVSP, 1, 0, 1);}
int wave_get_max_pulses(int pi)
{return pigpio_command(pi, PI_CMD_WVSP, 2, 0, 1);}
int wave_get_cbs(int pi)
{return pigpio_command(pi, PI_CMD_WVSC, 0, 0, 1);}
int wave_get_high_cbs(int pi)
{return pigpio_command(pi, PI_CMD_WVSC, 1, 0, 1);}
int wave_get_max_cbs(int pi)
{return pigpio_command(pi, PI_CMD_WVSC, 2, 0, 1);}
int gpio_trigger(int pi, unsigned user_gpio, unsigned pulseLen, uint32_t level)
{
gpioExtent_t ext[1];
/*
p1=user_gpio
p2=pulseLen
p3=4
## extension ##
unsigned level
*/
ext[0].size = sizeof(uint32_t);
ext[0].ptr = &level;
return pigpio_command_ext(
pi, PI_CMD_TRIG, user_gpio, pulseLen, 4, 1, ext, 1);
}
int set_glitch_filter(int pi, unsigned user_gpio, unsigned steady)
{return pigpio_command(pi, PI_CMD_FG, user_gpio, steady, 1);}
int set_noise_filter(int pi, unsigned user_gpio, unsigned steady, unsigned active)
{
gpioExtent_t ext[1];
/*
p1=user_gpio
p2=steady
p3=4
## extension ##
unsigned active
*/
ext[0].size = sizeof(uint32_t);
ext[0].ptr = &active;
return pigpio_command_ext(
pi, PI_CMD_FN, user_gpio, steady, 4, 1, ext, 1);
}
int store_script(int pi, char *script)
{
unsigned len;
gpioExtent_t ext[1];
/*
p1=0
p2=0
p3=len
## extension ##
char[len] script
*/
len = strlen(script);
if (!len) return 0;
ext[0].size = len;
ext[0].ptr = script;
return pigpio_command_ext(pi, PI_CMD_PROC, 0, 0, len, 1, ext, 1);
}
int run_script(int pi, unsigned script_id, unsigned numPar, uint32_t *param)
{
gpioExtent_t ext[1];
/*
p1=script id
p2=0
p3=numPar * 4
## extension ##
uint32_t[numPar] pars
*/
ext[0].size = 4 * numPar;
ext[0].ptr = param;
return pigpio_command_ext
(pi, PI_CMD_PROCR, script_id, 0, numPar*4, 1, ext, 1);
}
int script_status(int pi, unsigned script_id, uint32_t *param)
{
int status;
uint32_t p[PI_MAX_SCRIPT_PARAMS+1]; /* space for script status */
status = pigpio_command(pi, PI_CMD_PROCP, script_id, 0, 0);
if (status > 0)
{
recvMax(pi, p, sizeof(p), status);
status = p[0];