Menu

[be8e4d]: / source / py_gpio.c  Maximize  Restore  History

Download this file

1080 lines (927 with data), 32.1 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
/*
Copyright (c) 2012-2021 Ben Croston
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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 OR COPYRIGHT HOLDERS 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.
*/
#include "Python.h"
#include "c_gpio.h"
#include "event_gpio.h"
#include "py_pwm.h"
#include "cpuinfo.h"
#include "constants.h"
#include "common.h"
static PyObject *rpi_revision; // deprecated
static PyObject *board_info;
static int gpio_warnings = 1;
struct py_callback
{
unsigned int gpio;
PyObject *py_cb;
struct py_callback *next;
};
static struct py_callback *py_callbacks = NULL;
static int mmap_gpio_mem(void)
{
int result;
if (module_setup)
return 0;
result = setup();
if (result == SETUP_DEVMEM_FAIL)
{
PyErr_SetString(PyExc_RuntimeError, "No access to /dev/mem. Try running as root!");
return 1;
} else if (result == SETUP_MALLOC_FAIL) {
PyErr_NoMemory();
return 2;
} else if (result == SETUP_MMAP_FAIL) {
PyErr_SetString(PyExc_RuntimeError, "Mmap of GPIO registers failed");
return 3;
} else if (result == SETUP_CPUINFO_FAIL) {
PyErr_SetString(PyExc_RuntimeError, "Unable to open /proc/cpuinfo");
return 4;
} else if (result == SETUP_NO_PERI_ADDR) {
PyErr_SetString(PyExc_RuntimeError, "Cannot determine SOC peripheral base address");
return 5;
} else { // result == SETUP_OK
module_setup = 1;
return 0;
}
}
// python function cleanup(channel=None)
static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
{
int i;
int chancount = -666;
int found = 0;
int channel = -666;
unsigned int gpio;
PyObject *chanlist = NULL;
PyObject *chantuple = NULL;
PyObject *tempobj;
static char *kwlist[] = {"channel", NULL};
void cleanup_one(void)
{
// clean up any /sys/class exports
event_cleanup(gpio);
// set everything back to input
if (gpio_direction[gpio] != -1) {
setup_gpio(gpio, INPUT, PUD_OFF);
gpio_direction[gpio] = -1;
found = 1;
}
}
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &chanlist))
return NULL;
if (chanlist == NULL) { // channel kwarg not set
// do nothing
#if PY_MAJOR_VERSION > 2
} else if (PyLong_Check(chanlist)) {
channel = (int)PyLong_AsLong(chanlist);
#else
} else if (PyInt_Check(chanlist)) {
channel = (int)PyInt_AsLong(chanlist);
#endif
if (PyErr_Occurred())
return NULL;
chanlist = NULL;
} else if (PyList_Check(chanlist)) {
chancount = PyList_Size(chanlist);
} else if (PyTuple_Check(chanlist)) {
chantuple = chanlist;
chanlist = NULL;
chancount = PyTuple_Size(chantuple);
} else {
// raise exception
PyErr_SetString(PyExc_ValueError, "Channel must be an integer or list/tuple of integers");
return NULL;
}
if (module_setup && !setup_error) {
if (channel == -666 && chancount == -666) { // channel not set - cleanup everything
// clean up any /sys/class exports
event_cleanup_all();
// set everything back to input
for (i=0; i<54; i++) {
if (gpio_direction[i] != -1) {
setup_gpio(i, INPUT, PUD_OFF);
gpio_direction[i] = -1;
found = 1;
}
}
gpio_mode = MODE_UNKNOWN;
} else if (channel != -666) { // channel was an int indicating single channel
if (get_gpio_number(channel, &gpio))
return NULL;
cleanup_one();
} else { // channel was a list/tuple
for (i=0; i<chancount; i++) {
if (chanlist) {
if ((tempobj = PyList_GetItem(chanlist, i)) == NULL) {
return NULL;
}
} else { // assume chantuple
if ((tempobj = PyTuple_GetItem(chantuple, i)) == NULL) {
return NULL;
}
}
#if PY_MAJOR_VERSION > 2
if (PyLong_Check(tempobj)) {
channel = (int)PyLong_AsLong(tempobj);
#else
if (PyInt_Check(tempobj)) {
channel = (int)PyInt_AsLong(tempobj);
#endif
if (PyErr_Occurred())
return NULL;
} else {
PyErr_SetString(PyExc_ValueError, "Channel must be an integer");
return NULL;
}
if (get_gpio_number(channel, &gpio))
return NULL;
cleanup_one();
}
}
}
// check if any channels set up - if not warn about misuse of GPIO.cleanup()
if (!found && gpio_warnings) {
PyErr_WarnEx(NULL, "No channels have been set up yet - nothing to clean up! Try cleaning up at the end of your program instead!", 1);
}
Py_RETURN_NONE;
}
// python function setup(channel(s), direction, pull_up_down=PUD_OFF, initial=None)
static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs)
{
unsigned int gpio;
int channel = -1;
int direction;
int i, chancount;
PyObject *chanlist = NULL;
PyObject *chantuple = NULL;
PyObject *tempobj;
int pud = PUD_OFF + PY_PUD_CONST_OFFSET;
int initial = -1;
static char *kwlist[] = {"channel", "direction", "pull_up_down", "initial", NULL};
int func;
int setup_one(void) {
if (get_gpio_number(channel, &gpio))
return 0;
func = gpio_function(gpio);
if (gpio_warnings && // warnings enabled and
((func != 0 && func != 1) || // (already one of the alt functions or
(gpio_direction[gpio] == -1 && func == 1))) // already an output not set from this program)
{
PyErr_WarnEx(NULL, "This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.", 1);
}
// warn about pull/up down on i2c channels
if (gpio_warnings) {
if (rpiinfo.p1_revision == 0) { // compute module - do nothing
} else if ((rpiinfo.p1_revision == 1 && (gpio == 0 || gpio == 1)) ||
(gpio == 2 || gpio == 3)) {
if (pud == PUD_UP || pud == PUD_DOWN)
PyErr_WarnEx(NULL, "A physical pull up resistor is fitted on this channel!", 1);
}
}
if (direction == OUTPUT && (initial == LOW || initial == HIGH)) {
output_gpio(gpio, initial);
}
setup_gpio(gpio, direction, pud);
gpio_direction[gpio] = direction;
return 1;
}
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii", kwlist, &chanlist, &direction, &pud, &initial))
return NULL;
#if PY_MAJOR_VERSION > 2
if (PyLong_Check(chanlist)) {
channel = (int)PyLong_AsLong(chanlist);
#else
if (PyInt_Check(chanlist)) {
channel = (int)PyInt_AsLong(chanlist);
#endif
if (PyErr_Occurred())
return NULL;
chanlist = NULL;
} else if (PyList_Check(chanlist)) {
// do nothing
} else if (PyTuple_Check(chanlist)) {
chantuple = chanlist;
chanlist = NULL;
} else {
// raise exception
PyErr_SetString(PyExc_ValueError, "Channel must be an integer or list/tuple of integers");
return NULL;
}
// check module has been imported cleanly
if (setup_error)
{
PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!");
return NULL;
}
if (mmap_gpio_mem())
return NULL;
if (direction != INPUT && direction != OUTPUT) {
PyErr_SetString(PyExc_ValueError, "An invalid direction was passed to setup()");
return 0;
}
if (direction == OUTPUT && pud != PUD_OFF + PY_PUD_CONST_OFFSET) {
PyErr_SetString(PyExc_ValueError, "pull_up_down parameter is not valid for outputs");
return 0;
}
if (direction == INPUT && initial != -1) {
PyErr_SetString(PyExc_ValueError, "initial parameter is not valid for inputs");
return 0;
}
if (direction == OUTPUT)
pud = PUD_OFF + PY_PUD_CONST_OFFSET;
pud -= PY_PUD_CONST_OFFSET;
if (pud != PUD_OFF && pud != PUD_DOWN && pud != PUD_UP) {
PyErr_SetString(PyExc_ValueError, "Invalid value for pull_up_down - should be either PUD_OFF, PUD_UP or PUD_DOWN");
return NULL;
}
if (chanlist) {
chancount = PyList_Size(chanlist);
} else if (chantuple) {
chancount = PyTuple_Size(chantuple);
} else {
if (!setup_one())
return NULL;
Py_RETURN_NONE;
}
for (i=0; i<chancount; i++) {
if (chanlist) {
if ((tempobj = PyList_GetItem(chanlist, i)) == NULL) {
return NULL;
}
} else { // assume chantuple
if ((tempobj = PyTuple_GetItem(chantuple, i)) == NULL) {
return NULL;
}
}
#if PY_MAJOR_VERSION > 2
if (PyLong_Check(tempobj)) {
channel = (int)PyLong_AsLong(tempobj);
#else
if (PyInt_Check(tempobj)) {
channel = (int)PyInt_AsLong(tempobj);
#endif
if (PyErr_Occurred())
return NULL;
} else {
PyErr_SetString(PyExc_ValueError, "Channel must be an integer");
return NULL;
}
if (!setup_one())
return NULL;
}
Py_RETURN_NONE;
}
// python function output(channel(s), value(s))
static PyObject *py_output_gpio(PyObject *self, PyObject *args)
{
unsigned int gpio;
int channel = -1;
int value = -1;
int i;
PyObject *chanlist = NULL;
PyObject *valuelist = NULL;
PyObject *chantuple = NULL;
PyObject *valuetuple = NULL;
PyObject *tempobj = NULL;
int chancount = -1;
int valuecount = -1;
int output(void) {
if (get_gpio_number(channel, &gpio))
return 0;
if (gpio_direction[gpio] != OUTPUT)
{
PyErr_SetString(PyExc_RuntimeError, "The GPIO channel has not been set up as an OUTPUT");
return 0;
}
if (check_gpio_priv())
return 0;
output_gpio(gpio, value);
return 1;
}
if (!PyArg_ParseTuple(args, "OO", &chanlist, &valuelist))
return NULL;
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(chanlist)) {
channel = (int)PyLong_AsLong(chanlist);
#else
if (PyInt_Check(chanlist)) {
channel = (int)PyInt_AsLong(chanlist);
#endif
if (PyErr_Occurred())
return NULL;
chanlist = NULL;
} else if (PyList_Check(chanlist)) {
// do nothing
} else if (PyTuple_Check(chanlist)) {
chantuple = chanlist;
chanlist = NULL;
} else {
PyErr_SetString(PyExc_ValueError, "Channel must be an integer or list/tuple of integers");
return NULL;
}
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(valuelist)) {
value = (int)PyLong_AsLong(valuelist);
#else
if (PyInt_Check(valuelist)) {
value = (int)PyInt_AsLong(valuelist);
#endif
if (PyErr_Occurred())
return NULL;
valuelist = NULL;
} else if (PyList_Check(valuelist)) {
// do nothing
} else if (PyTuple_Check(valuelist)) {
valuetuple = valuelist;
valuelist = NULL;
} else {
PyErr_SetString(PyExc_ValueError, "Value must be an integer/boolean or a list/tuple of integers/booleans");
return NULL;
}
if (chanlist)
chancount = PyList_Size(chanlist);
if (chantuple)
chancount = PyTuple_Size(chantuple);
if (valuelist)
valuecount = PyList_Size(valuelist);
if (valuetuple)
valuecount = PyTuple_Size(valuetuple);
if ((chancount != -1 && chancount != valuecount && valuecount != -1) || (chancount == -1 && valuecount != -1)) {
PyErr_SetString(PyExc_RuntimeError, "Number of channels != number of values");
return NULL;
}
if (chancount == -1) {
if (!output())
return NULL;
Py_RETURN_NONE;
}
for (i=0; i<chancount; i++) {
// get channel number
if (chanlist) {
if ((tempobj = PyList_GetItem(chanlist, i)) == NULL) {
return NULL;
}
} else { // assume chantuple
if ((tempobj = PyTuple_GetItem(chantuple, i)) == NULL) {
return NULL;
}
}
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(tempobj)) {
channel = (int)PyLong_AsLong(tempobj);
#else
if (PyInt_Check(tempobj)) {
channel = (int)PyInt_AsLong(tempobj);
#endif
if (PyErr_Occurred())
return NULL;
} else {
PyErr_SetString(PyExc_ValueError, "Channel must be an integer");
return NULL;
}
// get value
if (valuecount > 0) {
if (valuelist) {
if ((tempobj = PyList_GetItem(valuelist, i)) == NULL) {
return NULL;
}
} else { // assume valuetuple
if ((tempobj = PyTuple_GetItem(valuetuple, i)) == NULL) {
return NULL;
}
}
#if PY_MAJOR_VERSION >= 3
if (PyLong_Check(tempobj)) {
value = (int)PyLong_AsLong(tempobj);
#else
if (PyInt_Check(tempobj)) {
value = (int)PyInt_AsLong(tempobj);
#endif
if (PyErr_Occurred())
return NULL;
} else {
PyErr_SetString(PyExc_ValueError, "Value must be an integer or boolean");
return NULL;
}
}
if (!output())
return NULL;
}
Py_RETURN_NONE;
}
// python function value = input(channel)
static PyObject *py_input_gpio(PyObject *self, PyObject *args)
{
unsigned int gpio;
int channel;
PyObject *value;
if (!PyArg_ParseTuple(args, "i", &channel))
return NULL;
if (get_gpio_number(channel, &gpio))
return NULL;
// check channel is set up as an input or output
if (gpio_direction[gpio] != INPUT && gpio_direction[gpio] != OUTPUT)
{
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel first");
return NULL;
}
if (check_gpio_priv())
return NULL;
if (input_gpio(gpio)) {
value = Py_BuildValue("i", HIGH);
} else {
value = Py_BuildValue("i", LOW);
}
return value;
}
// python function setmode(mode)
static PyObject *py_setmode(PyObject *self, PyObject *args)
{
int new_mode;
if (!PyArg_ParseTuple(args, "i", &new_mode))
return NULL;
if (gpio_mode != MODE_UNKNOWN && new_mode != gpio_mode)
{
PyErr_SetString(PyExc_ValueError, "A different mode has already been set!");
return NULL;
}
if (setup_error)
{
PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!");
return NULL;
}
if (new_mode != BOARD && new_mode != BCM)
{
PyErr_SetString(PyExc_ValueError, "An invalid mode was passed to setmode()");
return NULL;
}
if (rpiinfo.p1_revision == 0 && new_mode == BOARD)
{
PyErr_SetString(PyExc_RuntimeError, "BOARD numbering system not applicable on compute module");
return NULL;
}
gpio_mode = new_mode;
Py_RETURN_NONE;
}
// python function getmode()
static PyObject *py_getmode(PyObject *self, PyObject *args)
{
PyObject *value;
if (setup_error)
{
PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!");
return NULL;
}
if (gpio_mode == MODE_UNKNOWN)
Py_RETURN_NONE;
value = Py_BuildValue("i", gpio_mode);
return value;
}
static unsigned int chan_from_gpio(unsigned int gpio)
{
int chan;
int chans;
if (gpio_mode == BCM)
return gpio;
if (rpiinfo.p1_revision == 0) // not applicable for compute module
return -1;
else if (rpiinfo.p1_revision == 1 || rpiinfo.p1_revision == 2)
chans = 26;
else
chans = 40;
for (chan=1; chan<=chans; chan++)
if (*(*pin_to_gpio+chan) == (int)gpio)
return chan;
return -1;
}
static void run_py_callbacks(unsigned int gpio)
{
PyObject *result;
PyGILState_STATE gstate;
struct py_callback *cb = py_callbacks;
while (cb != NULL)
{
if (cb->gpio == gpio) {
// run callback
gstate = PyGILState_Ensure();
result = PyObject_CallFunction(cb->py_cb, "i", chan_from_gpio(gpio));
if (result == NULL && PyErr_Occurred()){
PyErr_Print();
PyErr_Clear();
}
Py_XDECREF(result);
PyGILState_Release(gstate);
}
cb = cb->next;
}
}
static int add_py_callback(unsigned int gpio, PyObject *cb_func)
{
struct py_callback *new_py_cb;
struct py_callback *cb = py_callbacks;
// add callback to py_callbacks list
new_py_cb = malloc(sizeof(struct py_callback));
if (new_py_cb == 0)
{
PyErr_NoMemory();
return -1;
}
new_py_cb->py_cb = cb_func;
Py_XINCREF(cb_func); // Add a reference to new callback
new_py_cb->gpio = gpio;
new_py_cb->next = NULL;
if (py_callbacks == NULL) {
py_callbacks = new_py_cb;
} else {
// add to end of list
while (cb->next != NULL)
cb = cb->next;
cb->next = new_py_cb;
}
add_edge_callback(gpio, run_py_callbacks);
return 0;
}
// python function add_event_callback(gpio, callback)
static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject *kwargs)
{
unsigned int gpio;
int channel;
PyObject *cb_func;
char *kwlist[] = {"gpio", "callback", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iO|i", kwlist, &channel, &cb_func))
return NULL;
if (!PyCallable_Check(cb_func))
{
PyErr_SetString(PyExc_TypeError, "Parameter must be callable");
return NULL;
}
if (get_gpio_number(channel, &gpio))
return NULL;
// check channel is set up as an input
if (gpio_direction[gpio] != INPUT)
{
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel as an input first");
return NULL;
}
if (!gpio_event_added(gpio))
{
PyErr_SetString(PyExc_RuntimeError, "Add event detection using add_event_detect first before adding a callback");
return NULL;
}
if (add_py_callback(gpio, cb_func) != 0)
return NULL;
Py_RETURN_NONE;
}
// python function add_event_detect(gpio, edge, callback=None, bouncetime=None)
static PyObject *py_add_event_detect(PyObject *self, PyObject *args, PyObject *kwargs)
{
unsigned int gpio;
int channel, edge, result;
int bouncetime = -666;
PyObject *cb_func = NULL;
char *kwlist[] = {"gpio", "edge", "callback", "bouncetime", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|Oi", kwlist, &channel, &edge, &cb_func, &bouncetime))
return NULL;
if (cb_func != NULL && !PyCallable_Check(cb_func))
{
PyErr_SetString(PyExc_TypeError, "Parameter must be callable");
return NULL;
}
if (get_gpio_number(channel, &gpio))
return NULL;
// check channel is set up as an input
if (gpio_direction[gpio] != INPUT)
{
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel as an input first");
return NULL;
}
// is edge valid value
edge -= PY_EVENT_CONST_OFFSET;
if (edge != RISING_EDGE && edge != FALLING_EDGE && edge != BOTH_EDGE)
{
PyErr_SetString(PyExc_ValueError, "The edge must be set to RISING, FALLING or BOTH");
return NULL;
}
if (bouncetime <= 0 && bouncetime != -666)
{
PyErr_SetString(PyExc_ValueError, "Bouncetime must be greater than 0");
return NULL;
}
if (check_gpio_priv())
return NULL;
if ((result = add_edge_detect(gpio, edge, bouncetime)) != 0) // starts a thread
{
if (result == 1)
{
PyErr_SetString(PyExc_RuntimeError, "Conflicting edge detection already enabled for this GPIO channel");
return NULL;
} else {
PyErr_SetString(PyExc_RuntimeError, "Failed to add edge detection");
return NULL;
}
}
if (cb_func != NULL)
if (add_py_callback(gpio, cb_func) != 0)
return NULL;
Py_RETURN_NONE;
}
// python function remove_event_detect(gpio)
static PyObject *py_remove_event_detect(PyObject *self, PyObject *args)
{
unsigned int gpio;
int channel;
struct py_callback *cb = py_callbacks;
struct py_callback *temp;
struct py_callback *prev = NULL;
if (!PyArg_ParseTuple(args, "i", &channel))
return NULL;
if (get_gpio_number(channel, &gpio))
return NULL;
// remove all python callbacks for gpio
while (cb != NULL)
{
if (cb->gpio == gpio)
{
Py_XDECREF(cb->py_cb);
if (prev == NULL)
py_callbacks = cb->next;
else
prev->next = cb->next;
temp = cb;
cb = cb->next;
free(temp);
} else {
prev = cb;
cb = cb->next;
}
}
if (check_gpio_priv())
return NULL;
remove_edge_detect(gpio);
Py_RETURN_NONE;
}
// python function value = event_detected(channel)
static PyObject *py_event_detected(PyObject *self, PyObject *args)
{
unsigned int gpio;
int channel;
if (!PyArg_ParseTuple(args, "i", &channel))
return NULL;
if (get_gpio_number(channel, &gpio))
return NULL;
if (event_detected(gpio))
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
// python function channel = wait_for_edge(channel, edge, bouncetime=None, timeout=None)
static PyObject *py_wait_for_edge(PyObject *self, PyObject *args, PyObject *kwargs)
{
unsigned int gpio;
int channel, edge, result;
int bouncetime = -666; // None
int timeout = -1; // None
static char *kwlist[] = {"channel", "edge", "bouncetime", "timeout", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii", kwlist, &channel, &edge, &bouncetime, &timeout))
return NULL;
if (get_gpio_number(channel, &gpio))
return NULL;
// check channel is setup as an input
if (gpio_direction[gpio] != INPUT)
{
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel as an input first");
return NULL;
}
// is edge a valid value?
edge -= PY_EVENT_CONST_OFFSET;
if (edge != RISING_EDGE && edge != FALLING_EDGE && edge != BOTH_EDGE)
{
PyErr_SetString(PyExc_ValueError, "The edge must be set to RISING, FALLING or BOTH");
return NULL;
}
if (bouncetime <= 0 && bouncetime != -666)
{
PyErr_SetString(PyExc_ValueError, "Bouncetime must be greater than 0");
return NULL;
}
if (timeout <= 0 && timeout != -1)
{
PyErr_SetString(PyExc_ValueError, "Timeout must be greater than 0");
return NULL;
}
if (check_gpio_priv())
return NULL;
Py_BEGIN_ALLOW_THREADS // disable GIL
result = blocking_wait_for_edge(gpio, edge, bouncetime, timeout);
Py_END_ALLOW_THREADS // enable GIL
if (result == 0) {
Py_RETURN_NONE;
} else if (result == -1) {
PyErr_SetString(PyExc_RuntimeError, "Conflicting edge detection events already exist for this GPIO channel");
return NULL;
} else if (result == -2) {
PyErr_SetString(PyExc_RuntimeError, "Error waiting for edge");
return NULL;
} else {
return Py_BuildValue("i", channel);
}
}
// python function value = gpio_function(channel)
static PyObject *py_gpio_function(PyObject *self, PyObject *args)
{
unsigned int gpio;
int channel;
int f;
PyObject *func;
if (!PyArg_ParseTuple(args, "i", &channel))
return NULL;
if (get_gpio_number(channel, &gpio))
return NULL;
if (mmap_gpio_mem())
return NULL;
f = gpio_function(gpio);
switch (f)
{
case 0 : f = INPUT; break;
case 1 : f = OUTPUT; break;
// ALT 0
case 4 : switch (gpio)
{
case 0 :
case 1 :
case 2 :
case 3 : f = I2C; break;
case 7 :
case 8 :
case 9 :
case 10 :
case 11 : f = SPI; break;
case 12 :
case 13 : f = PWM; break;
case 14 :
case 15 : f = SERIAL; break;
case 28 :
case 29 : f = I2C; break;
default : f = MODE_UNKNOWN; break;
}
break;
// ALT 5
case 2 : if (gpio == 18 || gpio == 19) f = PWM; else f = MODE_UNKNOWN;
break;
// ALT 4
case 3 : switch (gpio)
{
case 16 :
case 17 :
case 18 :
case 19 :
case 20 :
case 21 : f = SPI; break;
default : f = MODE_UNKNOWN; break;
}
break;
default : f = MODE_UNKNOWN; break;
}
func = Py_BuildValue("i", f);
return func;
}
// python function setwarnings(state)
static PyObject *py_setwarnings(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, "i", &gpio_warnings))
return NULL;
if (setup_error)
{
PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!");
return NULL;
}
Py_RETURN_NONE;
}
static const char moduledocstring[] = "GPIO functionality of a Raspberry Pi using Python";
PyMethodDef rpi_gpio_methods[] = {
{"setup", (PyCFunction)py_setup_channel, METH_VARARGS | METH_KEYWORDS, "Set up a GPIO channel or list of channels with a direction and (optional) pull/up down control\nchannel - either board pin number or BCM number depending on which mode is set.\ndirection - IN or OUT\n[pull_up_down] - PUD_OFF (default), PUD_UP or PUD_DOWN\n[initial] - Initial value for an output channel"},
{"cleanup", (PyCFunction)py_cleanup, METH_VARARGS | METH_KEYWORDS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection\n[channel] - individual channel or list/tuple of channels to clean up. Default - clean every channel that has been used."},
{"output", py_output_gpio, METH_VARARGS, "Output to a GPIO channel or list of channels\nchannel - either board pin number or BCM number depending on which mode is set.\nvalue - 0/1 or False/True or LOW/HIGH"},
{"input", py_input_gpio, METH_VARARGS, "Input from a GPIO channel. Returns HIGH=1=True or LOW=0=False\nchannel - either board pin number or BCM number depending on which mode is set."},
{"setmode", py_setmode, METH_VARARGS, "Set up numbering mode to use for channels.\nBOARD - Use Raspberry Pi board numbers\nBCM - Use Broadcom GPIO 00..nn numbers"},
{"getmode", py_getmode, METH_VARARGS, "Get numbering mode used for channel numbers.\nReturns BOARD, BCM or None"},
{"add_event_detect", (PyCFunction)py_add_event_detect, METH_VARARGS | METH_KEYWORDS, "Enable edge detection events for a particular GPIO channel.\nchannel - either board pin number or BCM number depending on which mode is set.\nedge - RISING, FALLING or BOTH\n[callback] - A callback function for the event (optional)\n[bouncetime] - Switch bounce timeout in ms for callback"},
{"remove_event_detect", py_remove_event_detect, METH_VARARGS, "Remove edge detection for a particular GPIO channel\nchannel - either board pin number or BCM number depending on which mode is set."},
{"event_detected", py_event_detected, METH_VARARGS, "Returns True if an edge has occurred on a given GPIO. You need to enable edge detection using add_event_detect() first.\nchannel - either board pin number or BCM number depending on which mode is set."},
{"add_event_callback", (PyCFunction)py_add_event_callback, METH_VARARGS | METH_KEYWORDS, "Add a callback for an event already defined using add_event_detect()\nchannel - either board pin number or BCM number depending on which mode is set.\ncallback - a callback function"},
{"wait_for_edge", (PyCFunction)py_wait_for_edge, METH_VARARGS | METH_KEYWORDS, "Wait for an edge. Returns the channel number or None on timeout.\nchannel - either board pin number or BCM number depending on which mode is set.\nedge - RISING, FALLING or BOTH\n[bouncetime] - time allowed between calls to allow for switchbounce\n[timeout] - timeout in ms"},
{"gpio_function", py_gpio_function, METH_VARARGS, "Return the current GPIO function (IN, OUT, PWM, SERIAL, I2C, SPI)\nchannel - either board pin number or BCM number depending on which mode is set."},
{"setwarnings", py_setwarnings, METH_VARARGS, "Enable or disable warning messages"},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION > 2
static struct PyModuleDef rpigpiomodule = {
PyModuleDef_HEAD_INIT,
"RPi._GPIO", // name of module
moduledocstring, // module documentation, may be NULL
-1, // size of per-interpreter state of the module, or -1 if the module keeps state in global variables.
rpi_gpio_methods
};
#endif
#if PY_MAJOR_VERSION > 2
PyMODINIT_FUNC PyInit__GPIO(void)
#else
PyMODINIT_FUNC init_GPIO(void)
#endif
{
int i;
PyObject *module = NULL;
#if PY_MAJOR_VERSION > 2
if ((module = PyModule_Create(&rpigpiomodule)) == NULL)
return NULL;
#else
if ((module = Py_InitModule3("RPi._GPIO", rpi_gpio_methods, moduledocstring)) == NULL)
return;
#endif
define_constants(module);
for (i=0; i<54; i++)
gpio_direction[i] = -1;
// detect board revision and set up accordingly
if (get_rpi_info(&rpiinfo))
{
PyErr_SetString(PyExc_RuntimeError, "This module can only be run on a Raspberry Pi!");
setup_error = 1;
#if PY_MAJOR_VERSION > 2
return NULL;
#else
return;
#endif
}
board_info = Py_BuildValue("{sissssssssss}",
"P1_REVISION",rpiinfo.p1_revision,
"REVISION",&rpiinfo.revision,
"TYPE",rpiinfo.type,
"MANUFACTURER",rpiinfo.manufacturer,
"PROCESSOR",rpiinfo.processor,
"RAM",rpiinfo.ram);
PyModule_AddObject(module, "RPI_INFO", board_info);
if (rpiinfo.p1_revision == 1) {
pin_to_gpio = &pin_to_gpio_rev1;
} else if (rpiinfo.p1_revision == 2) {
pin_to_gpio = &pin_to_gpio_rev2;
} else { // assume model B+ or A+ or 2B
pin_to_gpio = &pin_to_gpio_rev3;
}
rpi_revision = Py_BuildValue("i", rpiinfo.p1_revision); // deprecated
PyModule_AddObject(module, "RPI_REVISION", rpi_revision); // deprecated
// Add PWM class
if (PWM_init_PWMType() == NULL)
#if PY_MAJOR_VERSION > 2
return NULL;
#else
return;
#endif
Py_INCREF(&PWMType);
PyModule_AddObject(module, "PWM", (PyObject*)&PWMType);
#if PY_MAJOR_VERSION < 3 || PY_MINOR_VERSION < 7
if (!PyEval_ThreadsInitialized())
PyEval_InitThreads();
#endif
// register exit functions - last declared is called first
if (Py_AtExit(cleanup) != 0)
{
setup_error = 1;
cleanup();
#if PY_MAJOR_VERSION > 2
return NULL;
#else
return;
#endif
}
if (Py_AtExit(event_cleanup_all) != 0)
{
setup_error = 1;
cleanup();
#if PY_MAJOR_VERSION > 2
return NULL;
#else
return;
#endif
}
#if PY_MAJOR_VERSION > 2
return module;
#else
return;
#endif
}