Author Topic: PI GPIO  (Read 22 times)

admin

  • Administrator
  • *****
  • Posts: 7
    • View Profile
PI GPIO
« on: March 31, 2025, 12:05:59 AM »
Quote from: Alexfish - BaCon Forum
This is first post on PI GPIO

Split from 'pigpio picogpio' and extended from the PI3 gpio thread

In that thread there is a I2C keyboard project using I2C PCF8574 gpio port expander


the following code is how to set up a pcf8574

Set up is per pin IE: so can scan the pins :

BaCon Forum link

John Spikowski

  • BASIC Developer
  • ****
  • Posts: 334
    • View Profile
Re: PI GPIO
« Reply #1 on: March 31, 2025, 02:03:58 AM »
alexfish,

It would be great if you could join the RaspberryBASIC forum.

Here is the wiringPI extension I wrote for ScriptBasic. I'm using my CBASIC preprocessor include to make the C code look like BASIC.

FYI;  The wigingPi library has been held back on updates for over a month now on Ubuntu 24.04.

Code: C
  1. /*
  2. WiringPi Extension Module
  3. UXLIBS: -lc -lwiringPi
  4. */
  5.  
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include "../../basext.h"
  15. #include <wiringPi.h>
  16. #include <wiringPiI2C.h>
  17. #include <wiringShift.h>
  18. #include <wiringPiSPI.h>
  19.  
  20. #define MAXTIMINGS  85
  21.  
  22.  
  23.  
  24. /**************************
  25.  Extension Module Functions
  26. **************************/
  27.  
  28. typedef struct _ModuleObject {
  29.   void *HandleArray;
  30. }ModuleObject,*pModuleObject;
  31.  
  32.  
  33. besVERSION_NEGOTIATE
  34.   return (int)INTERFACE_VERSION;
  35. besEND
  36.  
  37.  
  38. besSUB_START
  39.   pModuleObject p;
  40.  
  41.   besMODULEPOINTER = besALLOC(sizeof(ModuleObject));
  42.   if( besMODULEPOINTER == NULL )return 0;
  43.  
  44.   p = (pModuleObject)besMODULEPOINTER;
  45.   return 0;
  46. besEND
  47.  
  48.  
  49. besSUB_FINISH
  50.   pModuleObject p;
  51.  
  52.   p = (pModuleObject)besMODULEPOINTER;
  53.   if( p == NULL )return 0;
  54.   return 0;
  55. besEND
  56.  
  57.  
  58.  
  59. /******************
  60.  WiringPi Functions
  61. ******************/
  62.  
  63. // Core wiringPi functions
  64.  
  65.  
  66. besFUNCTION(sb_wiringPiSetup)
  67.   int status;
  68.   status = wiringPiSetup();
  69.   besRETURN_LONG(status);
  70. besEND
  71.  
  72.  
  73. besFUNCTION(sb_wiringPiSetupSys)
  74.   int status;
  75.   status = wiringPiSetupSys();
  76.   besRETURN_LONG(status);
  77. besEND
  78.  
  79.  
  80. besFUNCTION(sb_wiringPiSetupGpio)
  81.   int status;
  82.   status = wiringPiSetupGpio();
  83.   besRETURN_LONG(status);
  84. besEND
  85.  
  86.  
  87. besFUNCTION(sb_wiringPiSetupPhys)
  88.   int status;
  89.   status = wiringPiSetupPhys();
  90.   besRETURN_LONG(status);
  91. besEND
  92.  
  93.  
  94. besFUNCTION(sb_pinModeAlt)
  95.   VARIABLE Argument;
  96.   int pin, mode;
  97.   besARGUMENTS("ii")
  98.     &pin, &mode
  99.   besARGEND
  100.   pinModeAlt(pin,mode);
  101.   besRETURNVALUE = NULL;
  102. besEND
  103.  
  104.  
  105. besFUNCTION(sb_pinMode)
  106.   VARIABLE Argument;
  107.   int pin, mode;
  108.   besARGUMENTS("ii")
  109.     &pin, &mode
  110.   besARGEND
  111.   pinMode(pin,mode);
  112.   besRETURNVALUE = NULL;
  113. besEND
  114.  
  115.  
  116. besFUNCTION(sb_pullUpDnControl)
  117.   VARIABLE Argument;
  118.   int pin, pud;
  119.   besARGUMENTS("ii")
  120.     &pin, &pud
  121.   besARGEND
  122.   pullUpDnControl(pin,pud);
  123.   besRETURNVALUE = NULL;
  124. besEND
  125.  
  126.  
  127. besFUNCTION(sb_digitalRead)
  128.   int pin, rtn;
  129.   besARGUMENTS("i")
  130.     &pin
  131.   besARGEND
  132.   rtn = digitalRead(pin);
  133.   besRETURN_LONG(rtn);
  134. besEND
  135.  
  136.  
  137. besFUNCTION(sb_digitalWrite)
  138.   VARIABLE Argument;
  139.   int pin, value;
  140.   besARGUMENTS("ii")
  141.     &pin, &value
  142.   besARGEND
  143.   digitalWrite(pin,value);
  144.   besRETURNVALUE = NULL;
  145. besEND
  146.  
  147.  
  148. besFUNCTION(sb_digitalRead8)
  149.   int pin;
  150.   unsigned int rtn;
  151.   besARGUMENTS("i")
  152.     &pin
  153.   besARGEND
  154.   rtn = digitalRead8(pin);
  155.   besRETURN_LONG(rtn);
  156. besEND
  157.  
  158.  
  159. besFUNCTION(sb_digitalWrite8)
  160.   VARIABLE Argument;
  161.   int pin, value;
  162.   besARGUMENTS("ii")
  163.     &pin, &value
  164.   besARGEND
  165.   digitalWrite8(pin,value);
  166.   besRETURNVALUE = NULL;
  167. besEND
  168.  
  169.  
  170. besFUNCTION(sb_pwmWrite)
  171.   VARIABLE Argument;
  172.   int pin, value;
  173.   besARGUMENTS("ii")
  174.     &pin, &value
  175.   besARGEND
  176.   pwmWrite(pin,value);
  177.   besRETURNVALUE = NULL;
  178. besEND
  179.  
  180.  
  181. besFUNCTION(sb_analogRead)
  182.   int pin, rtn;
  183.   besARGUMENTS("i")
  184.     &pin
  185.   besARGEND
  186.   rtn = analogRead(pin);
  187.   besRETURN_LONG(rtn);
  188. besEND
  189.  
  190.  
  191. besFUNCTION(sb_analogWrite)
  192.   VARIABLE Argument;
  193.   int pin, value;
  194.   besARGUMENTS("ii")
  195.     &pin, &value
  196.   besARGEND
  197.   analogWrite(pin,value);
  198.   besRETURNVALUE = NULL;
  199. besEND
  200.  
  201.  
  202. // On-Board Raspberry Pi hardware specific
  203.  
  204.  
  205. besFUNCTION(sb_piGpioLayout)
  206.   int rtn;
  207.   rtn = piGpioLayout();
  208.   besRETURN_LONG(rtn);
  209. besEND
  210.  
  211.  
  212. besFUNCTION(sb_wpiPinToGpio)
  213.   int wpiPin, rtn;
  214.   besARGUMENTS("i")
  215.     &wpiPin
  216.   besARGEND
  217.   rtn = wpiPinToGpio(wpiPin);
  218.   besRETURN_LONG(rtn);
  219. besEND
  220.  
  221.  
  222. besFUNCTION(sb_physPinToGpio)
  223.   int physPin, rtn;
  224.   besARGUMENTS("i")
  225.     &physPin
  226.   besARGEND
  227.   rtn = physPinToGpio(physPin);
  228.   besRETURN_LONG(rtn);
  229. besEND
  230.  
  231.  
  232. besFUNCTION(sb_setPadDrive)
  233.   VARIABLE Argument;
  234.   int group, value;
  235.   besARGUMENTS("ii")
  236.     &group, &value
  237.   besARGEND
  238.   setPadDrive(group,value);
  239.   besRETURNVALUE = NULL;
  240. besEND
  241.  
  242.  
  243. besFUNCTION(sb_getAlt)
  244.   int pin, rtn;
  245.   besARGUMENTS("i")
  246.     &pin
  247.   besARGEND
  248.   rtn = getAlt(pin);
  249.   besRETURN_LONG(rtn);
  250. besEND
  251.  
  252.  
  253. besFUNCTION(sb_pwmToneWrite)
  254.   VARIABLE Argument;
  255.   int pin, freq;
  256.   besARGUMENTS("ii")
  257.     &pin, &freq
  258.   besARGEND
  259.   pwmToneWrite(pin,freq);
  260.   besRETURNVALUE = NULL;
  261. besEND
  262.  
  263.  
  264. besFUNCTION(sb_pwmSetMode)
  265.   VARIABLE Argument;
  266.   int mode;
  267.   besARGUMENTS("i")
  268.     &mode
  269.   besARGEND
  270.   pwmSetMode(mode);
  271.   besRETURNVALUE = NULL;
  272. besEND
  273.  
  274.  
  275. besFUNCTION(sb_pwmSetRange)
  276.   VARIABLE Argument;
  277.   unsigned int range;
  278.   besARGUMENTS("i")
  279.     &range
  280.   besARGEND
  281.   pwmSetRange(range);
  282.   besRETURNVALUE = NULL;
  283. besEND
  284.  
  285.  
  286. besFUNCTION(sb_pwmSetClock)
  287.   VARIABLE Argument;
  288.   unsigned int divisor;
  289.   besARGUMENTS("i")
  290.     &divisor
  291.   besARGEND
  292.   pwmSetClock(divisor);
  293.   besRETURNVALUE = NULL;
  294. besEND
  295.  
  296.  
  297. besFUNCTION(sb_gpioClockSet)
  298.   VARIABLE Argument;
  299.   int pin, freq;
  300.   besARGUMENTS("ii")
  301.     &pin, &freq
  302.   besARGEND
  303.   gpioClockSet(pin,freq);
  304.   besRETURNVALUE = NULL;
  305. besEND
  306.  
  307.  
  308. besFUNCTION(sb_digitalReadByte)
  309.   unsigned int rtn;
  310.   rtn = digitalReadByte();
  311.   besRETURN_LONG(rtn);
  312. besEND
  313.  
  314.  
  315. besFUNCTION(sb_digitalReadByte2)
  316.   unsigned int rtn;
  317.   rtn = digitalReadByte2();
  318.   besRETURN_LONG(rtn);
  319. besEND
  320.  
  321.  
  322. besFUNCTION(sb_digitalWriteByte)
  323.   VARIABLE Argument;
  324.   int value;
  325.   besARGUMENTS("i")
  326.     &value
  327.   besARGEND
  328.   digitalWriteByte(value);
  329.   besRETURNVALUE = NULL;
  330. besEND
  331.  
  332.  
  333. besFUNCTION(sb_digitalWriteByte2)
  334.   VARIABLE Argument;
  335.   int value;
  336.   besARGUMENTS("i")
  337.     &value
  338.   besARGEND
  339.   digitalWriteByte2(value);
  340.   besRETURNVALUE = NULL;
  341. besEND
  342.  
  343.  
  344. besFUNCTION(sb_waitForInterrupt)
  345.   int pin, ms, rtn;
  346.   besARGUMENTS("ii")
  347.     &pin, &ms
  348.   besARGEND
  349.   rtn = waitForInterrupt(pin,ms);
  350.   besRETURN_LONG(rtn);
  351. besEND
  352.  
  353.  
  354. besFUNCTION(sb_piHiPri)
  355.   const int pri;
  356.   int rtn;
  357.   besARGUMENTS("i")
  358.     &pri
  359.   besARGEND
  360.   rtn = piHiPri(pri);
  361.   besRETURN_LONG(rtn);
  362. besEND
  363.  
  364.  
  365. besFUNCTION(sb_BitStreamRead)
  366.   char buf[850];
  367.   int pin, status, idx;
  368.   besARGUMENTS("i")
  369.     &pin
  370.   besARGEND
  371.   for (idx = 0; idx < 850; idx++){
  372.     status = digitalRead(pin);
  373.     delayMicroseconds(5);
  374.     if (status == 0){
  375.       buf[idx] = 0x30;
  376.     }else{
  377.       buf[idx] = 0x31;
  378.     }
  379.   }
  380.   besRETURN_STRING(buf);
  381. besEND
  382.  
  383.  
  384.  
  385. // Simplified I2C access routines
  386.  
  387.  
  388. besFUNCTION(sb_wiringPiI2CRead)
  389.   int fd,rtn;
  390.   VARIABLE Argument;
  391.   besARGUMENTS("i")
  392.     &fd
  393.   besARGEND
  394.   rtn = wiringPiI2CRead(fd);
  395.   besRETURN_LONG(rtn);
  396. besEND
  397.  
  398.  
  399. besFUNCTION(sb_wiringPiI2CReadReg8)
  400.   int fd,reg,rtn;
  401.   VARIABLE Argument;
  402.   besARGUMENTS("ii")
  403.     &fd,&reg
  404.   besARGEND
  405.   rtn = wiringPiI2CReadReg8(fd,reg);
  406.   besRETURN_LONG(rtn);
  407. besEND
  408.  
  409.  
  410. besFUNCTION(sb_wiringPiI2CReadReg16)
  411.   int fd,reg,rtn;
  412.   VARIABLE Argument;
  413.   besARGUMENTS("ii")
  414.     &fd,&reg
  415.   besARGEND
  416.   rtn = wiringPiI2CReadReg16(fd,reg);
  417.   besRETURN_LONG(rtn);
  418. besEND
  419.  
  420.  
  421. besFUNCTION(sb_wiringPiI2CWrite)
  422.   int fd,data,rtn;
  423.   VARIABLE Argument;
  424.   besARGUMENTS("ii")
  425.     &fd,&data
  426.   besARGEND
  427.   rtn = wiringPiI2CWrite(fd,data);
  428.   besRETURN_LONG(rtn);
  429. besEND
  430.  
  431.  
  432. besFUNCTION(sb_wiringPiI2CWriteReg8)
  433.   int fd,reg,data,rtn;
  434.   VARIABLE Argument;
  435.   besARGUMENTS("iii")
  436.     &fd,&reg,&data
  437.   besARGEND
  438.   rtn = wiringPiI2CWriteReg8(fd,reg,data);
  439.   besRETURN_LONG(rtn);
  440. besEND
  441.  
  442.  
  443. besFUNCTION(sb_wiringPiI2CWriteReg16)
  444.   int fd,reg,data,rtn;
  445.   VARIABLE Argument;
  446.   besARGUMENTS("iii")
  447.     &fd,&reg,&data
  448.   besARGEND
  449.   rtn = wiringPiI2CWriteReg16(fd,reg,data);
  450.   besRETURN_LONG(rtn);
  451. besEND
  452.  
  453.  
  454. besFUNCTION(sb_wiringPiI2CSetupInterface)
  455.   const char *device;
  456.   int devId,rtn;
  457.   VARIABLE Argument;
  458.   besARGUMENTS("zi")
  459.     &device,&devId
  460.   besARGEND
  461.   rtn = wiringPiI2CSetupInterface(device,devId);
  462.   besRETURN_LONG(rtn);
  463. besEND
  464.  
  465.  
  466. besFUNCTION(sb_wiringPiI2CSetup)
  467.   const int devId;
  468.   int rtn;
  469.   VARIABLE Argument;
  470.   besARGUMENTS("i")
  471.     &devId
  472.   besARGEND
  473.   rtn = wiringPiI2CSetup(devId);
  474.   besRETURN_LONG(rtn);
  475. besEND
  476.  
  477.  
  478.  
  479. // Shift Library
  480.  
  481.  
  482. besFUNCTION(sb_shiftIn)
  483.   VARIABLE Argument;
  484.   uint8_t dPin,cPin,order,rtn;
  485.   besARGUMENTS("iii")
  486.     &dPin,&cPin,&order
  487.   besARGEND
  488.   rtn = shiftIn(dPin,cPin,order);
  489.   besRETURN_LONG(rtn);
  490. besEND
  491.  
  492.  
  493. besFUNCTION(sb_shiftOut)
  494.   VARIABLE Argument;
  495.   uint8_t dPin,cPin,order,val;
  496.   besARGUMENTS("iiii")
  497.     &dPin,&cPin,&order,&val
  498.   besARGEND
  499.   shiftOut(dPin,cPin,order,val);
  500.   besRETURNVALUE = NULL;
  501. besEND
  502.  
  503.  
  504.  
  505. // SPI Library
  506.  
  507.  
  508. besFUNCTION(sb_wiringPiSPIGetFd)
  509.   int channel,rtn;
  510.   VARIABLE Argument;
  511.   besARGUMENTS("i")
  512.     &channel
  513.   besARGEND
  514.   rtn = wiringPiSPIGetFd(channel);
  515.   besRETURN_LONG(rtn);
  516. besEND
  517.  
  518.  
  519. besFUNCTION(sb_wiringPiSPIDataRW)
  520.   int channel,len,rtn;
  521.   unsigned char *data;
  522.   VARIABLE Argument;
  523.   besARGUMENTS("izi")
  524.     &channel,&data,&len
  525.   besARGEND
  526.   rtn = wiringPiSPIDataRW(channel,data,len);
  527.   besRETURN_LONG(rtn);
  528. besEND
  529.  
  530.  
  531. besFUNCTION(sb_wiringPiSPISetupMode)
  532.   int channel,speed,mode,rtn;
  533.   VARIABLE Argument;
  534.   besARGUMENTS("iii")
  535.     &channel,&speed,&mode
  536.   besARGEND
  537.   rtn = wiringPiSPISetupMode(channel,speed,mode);
  538.   besRETURN_LONG(rtn);
  539. besEND
  540.  
  541.  
  542. besFUNCTION(sb_wiringPiSPISetup)
  543.   int channel,speed,rtn;
  544.   VARIABLE Argument;
  545.   besARGUMENTS("ii")
  546.     &channel,&speed
  547.   besARGEND
  548.   rtn = wiringPiSPISetup(channel,speed);
  549.   besRETURN_LONG(rtn);
  550. besEND
  551.  
  552.  
  553.  
  554. /*****************
  555.  Utility Functions
  556. *****************/
  557.  
  558. besFUNCTION(sb_msSleep)
  559.   //DIM AS int msval, t;
  560.   long t;
  561.   besARGUMENTS("i")
  562.     &t
  563.   besARGEND
  564.   usleep(t);
  565.   besRETURNVALUE = NULL;
  566. besEND
  567.  
  568.  
  569. besFUNCTION(sb_delay)
  570.   unsigned int howLong;
  571.   besARGUMENTS("i")
  572.     &howLong
  573.   besARGEND
  574.   delay(howLong);
  575.   besRETURNVALUE = NULL;
  576. besEND
  577.  
  578.  
  579. besFUNCTION(sb_delayMicroseconds)
  580.   unsigned int howLong;
  581.   besARGUMENTS("i")
  582.     &howLong
  583.   besARGEND
  584.   delayMicroseconds(howLong);
  585.   besRETURNVALUE = NULL;
  586. besEND
  587.  
  588.  
  589. besFUNCTION(sb_bin2int)
  590.   const char* s;
  591.   besARGUMENTS("z")
  592.     &s
  593.   besARGEND
  594.   register unsigned char *p = s;
  595.   register unsigned int   r = 0;
  596.   while (p && *p ) {
  597.     r <<= 1;
  598.     r += (unsigned int)((*p++) & 0x01);
  599.   }
  600.   besRETURN_LONG(r);
  601. besEND
  602.  
« Last Edit: March 31, 2025, 06:22:09 PM by John Spikowski »