Raspberry BASIC

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - John Spikowski

Pages: [1] 2
1
I have updated the first post with new source and binary attachment. The following enhancements / changes have been made in this release.

BitStreamRead function reads the pin at the C level and returns the sampled bits as a 850 byte string. This will be enhanced in the next release to be able to define the buffer size, delay between pin reads and optionally return the binary string pre-parsed and checksum verified.

bin2int takes a string of bits and returns an integer.

DHT11 This device function has been added to the wpi.bas include file and the dedicated C version removed. The plan is to do device / sensor interfaces in native ScriptBasic and add what needed C routines to the WPI extension modules that makes sense.

2
ScriptBasic / WPI - WiringPi and Sensor Interface Extension Module
« on: June 08, 2019, 12:14:15 AM »
The following is the current statues of the WPI extension module code. I have attached a pre-compiled extension module and include file compiled on the Raspberry Pi 3 B.

wpi.bas
Code: Script BASIC
  1. ' WiringPi & Device Interface Extension Module - JRS 2019-06-01
  2.  
  3. MODULE WPI
  4.  
  5.  
  6. ' WiringPi API Functions
  7. DECLARE SUB ::wiringPiSetup                ALIAS "sb_wiringPiSetup"                LIB "wpi"
  8. DECLARE SUB ::wiringPiSetupSys             ALIAS "sb_wiringPiSetupSys"             LIB "wpi"
  9. DECLARE SUB ::wiringPiSetupGpio            ALIAS "sb_wiringPiSetupGpio"            LIB "wpi"
  10. DECLARE SUB ::wiringPiSetupPhys            ALIAS "sb_wiringPiSetupPhys"            LIB "wpi"
  11. DECLARE SUB ::pinModeAlt                   ALIAS "sb_pinModeAlt"                   LIB "wpi"
  12. DECLARE SUB ::pinMode                      ALIAS "sb_pinMode"                      LIB "wpi"
  13. DECLARE SUB ::pullUpDnControl              ALIAS "sb_pullUpDnControl"              LIB "wpi"
  14. DECLARE SUB ::digitalRead                  ALIAS "sb_digitalRead"                  LIB "wpi"
  15. DECLARE SUB ::digitalWrite                 ALIAS "sb_digitalWrite"                 LIB "wpi"
  16. DECLARE SUB ::digitalRead8                 ALIAS "sb_digitalRead8"                 LIB "wpi"
  17. DECLARE SUB ::digitalWrite8                ALIAS "sb_digitalWrite8"                LIB "wpi"
  18. DECLARE SUB ::pwmWrite                     ALIAS "sb_pwmWrite"                     LIB "wpi"
  19. DECLARE SUB ::analogRead                   ALIAS "sb_analogRead"                   LIB "wpi"
  20. DECLARE SUB ::analogWrite                  ALIAS "sb_analogWrite"                  LIB "wpi"
  21. DECLARE SUB ::piGpioLayout                 ALIAS "sb_piGpioLayout"                 LIB "wpi"
  22. DECLARE SUB ::wpiPinToGpio                 ALIAS "sb_wpiPinToGpio"                 LIB "wpi"
  23. DECLARE SUB ::setPadDrive                  ALIAS "sb_setPadDrive"                  LIB "wpi"
  24. DECLARE SUB ::getAlt                       ALIAS "sb_getAlt"                       LIB "wpi"
  25. DECLARE SUB ::pwmToneWrite                 ALIAS "sb_pwmToneWrite"                 LIB "wpi"
  26. DECLARE SUB ::pwmSetMode                   ALIAS "sb_pwmSetMode"                   LIB "wpi"
  27. DECLARE SUB ::pwmSetRange                  ALIAS "sb_pwmSetRange"                  LIB "wpi"
  28. DECLARE SUB ::pwmSetClock                  ALIAS "sb_pwmSetClock"                  LIB "wpi"
  29. DECLARE SUB ::gpioClockSet                 ALIAS "sb_gpioClockSet"                 LIB "wpi"
  30. DECLARE SUB ::digitalReadByte              ALIAS "sb_digitalReadByte"              LIB "wpi"
  31. DECLARE SUB ::digitalReadByte2             ALIAS "sb_digitalReadByte2"             LIB "wpi"
  32. DECLARE SUB ::digitalWriteByte             ALIAS "sb_digitalWriteByte"             LIB "wpi"
  33. DECLARE SUB ::digitalWriteByte2            ALIAS "sb_digitalWriteByte2"            LIB "wpi"
  34. DECLARE SUB ::waitForInterrupt             ALIAS "sb_waitForInterrupt"             LIB "wpi"
  35. DECLARE SUB ::piHiPri                      ALIAS "sb_piHiPri"                      LIB "wpi"
  36. ' Utility Functions
  37. DECLARE SUB ::msSleep                      ALIAS "sb_msSleep"                      LIB "wpi"
  38. DECLARE SUB ::delay                        ALIAS "sb_delay"                        LIB "wpi"
  39. DECLARE SUB ::delayMicroseconds            ALIAS "sb_delayMicroseconds"            LIB "wpi"
  40. DECLARE SUB ::BitStreamRead                ALIAS "sb_BitStreamRead"                LIB "wpi"
  41. DECLARE SUB ::bin2int                      ALIAS "sb_bin2int"                      LIB "wpi"
  42. ' Simplified I2C access routines
  43. DECLARE SUB ::wiringPiI2CRead              ALIAS "sb_wiringPiI2CRead"              LIB "wpi"
  44. DECLARE SUB ::wiringPiI2CReadReg8          ALIAS "sb_wiringPiI2CReadReg8"          LIB "wpi"
  45. DECLARE SUB ::wiringPiI2CReadReg16         ALIAS "sb_wiringPiI2CReadReg16"         LIB "wpi"
  46. DECLARE SUB ::wiringPiI2CWrite             ALIAS "sb_wiringPiI2CWrite"             LIB "wpi"
  47. DECLARE SUB ::wiringPiI2CWriteReg8         ALIAS "sb_wiringPiI2CWriteReg8"         LIB "wpi"
  48. DECLARE SUB ::wiringPiI2CWriteReg16        ALIAS "sb_wiringPiI2CWriteReg16"        LIB "wpi"
  49. DECLARE SUB ::wiringPiI2CSetupInterface    ALIAS "sb_wiringPiI2CSetupInterface"    LIB "wpi"
  50. DECLARE SUB ::wiringPiI2CSetup             ALIAS "sb_wiringPiI2CSetup"             LIB "wpi"
  51. ' Shift Library
  52. DECLARE SUB ::shiftIn                      ALIAS "sb_shiftIn"                      LIB "wpi"
  53. DECLARE SUB ::shiftOut                     ALIAS "sb_shiftOut"                     LIB "wpi"
  54. ' SPI Library
  55. DECLARE SUB ::wiringPiSPIGetFd             ALIAS "sb_wiringPiSPIGetFd"             LIB "wpi"
  56. DECLARE SUB ::wiringPiSPIDataRW            ALIAS "sb_wiringPiSPIDataRW"            LIB "wpi"
  57. DECLARE SUB ::wiringPiSPISetupMode         ALIAS "sb_wiringPiSPISetupMode"         LIB "wpi"
  58. DECLARE SUB ::wiringPiSPISetup             ALIAS "sb_wiringPiSPISetup"             LIB "wpi"
  59.  
  60.  
  61.  
  62. ' Native Device Interfaces
  63.  
  64.  
  65. FUNCTION DHT11(pin, humidity, temperature)
  66.   WPI::wiringPiSetup()
  67.   valid = 0
  68.   retries = 0
  69.   WHILE valid = 0
  70.     bits = ""
  71.     WPI::pinMode(pin, 1)
  72.     WPI::digitalWrite(pin, 0)
  73.     WPI::delay(18)
  74.     WPI::digitalWrite(pin, 1)
  75.     WPI::delayMicroseconds(40)
  76.     WPI::pinMode(pin, 0)
  77.     bits = WPI::BitStreamRead(pin)
  78.     SPLITA bits BY "" TO samples
  79.  
  80.     ' **** Extract the pulse lengths ****                                            
  81.  
  82.     pulseSize = 0
  83.     last = 0
  84.     FOR index = 0 TO ubound(samples)
  85.       IF samples[index] = 1 THEN
  86.         IF last = 0 THEN
  87.           pulseSize += 1
  88.         END IF
  89.         IF pulseSize <= 40 THEN
  90.           pulses[pulseSize-1] += 1
  91.         END IF
  92.       END IF
  93.       last = samples[index]
  94.     NEXT
  95.     undef samples
  96.  
  97.     ' **** Determine minimum and maximum pulse sizes, and mid-way between ****
  98.  
  99.     minimum = pulses[0]
  100.     maximum = pulses[0]
  101.  
  102.     FOR index = 0 TO 40
  103.       IF pulses[index] < minimum THEN minimum = pulses[index]
  104.       IF pulses[index] > maximum THEN maximum = pulses[index]
  105.     NEXT
  106.     middle = ((maximum - minimum) \ 2) + minimum
  107.  
  108.     ' **** Determine outbits values ****
  109.  
  110.     outbits = ""
  111.     FOR index = 0 TO 40
  112.       IF pulses[index] > middle THEN
  113.         outbits &= 1
  114.       ELSE
  115.         outbits &= 0
  116.       END IF
  117.     NEXT
  118.     UNDEF pulses
  119.  
  120.     ' **** Form the bytes ****
  121.  
  122.     index = 0
  123.     FOR idx = 2 TO LEN(outbits) STEP 8
  124.       byte[index] = WPI::bin2int(mid(outbits,idx,8))
  125.       index += 1
  126.     NEXT
  127.     outbits = ""
  128.  
  129.     ' **** Return results ****
  130.  
  131.     IF byte[0] + byte[1] + byte[2] + byte[3] = byte[4] THEN
  132.       humidity = byte[0] & "." & byte[1]
  133.       temperature = byte[2] & "." & byte[3]
  134.       GOTO Done
  135.     ELSE
  136.       retries += 1
  137.     END IF
  138.     UNDEF byte
  139.     WPI::delay(1550)
  140.   WEND
  141.   Done:
  142.   DHT11 = retries
  143. END FUNCTION
  144.  
  145.  
  146. FUNCTION DS18B20(slave)
  147.   LOCAL temp_raw, pos
  148.   OPEN slave FOR INPUT AS #1
  149.   LINE INPUT #1, temp_raw
  150.   IF RIGHT(CHOMP(temp_raw),3) = "YES" THEN
  151.     msSleep(200)
  152.     LINE INPUT #1, temp_raw
  153.     temp_raw = CHOMP(temp_raw)
  154.     pos = INSTR(temp_raw,"t=")
  155.     DS18B20 = MID(temp_raw, pos + 2) / 1000
  156.   ELSE
  157.     DS18B20 = "<Sensor Error>"
  158.   END IF
  159.   CLOSE (1)
  160. END FUNCTION
  161.  
  162.  
  163. ' Helper Functions
  164.  
  165. function shifts(v,p,ar)
  166.   local bp,ba,co,cq,bi,x,y,d
  167.   bp=1
  168.   x=0xffffffff and v
  169.   for co=0 to 31
  170.     ba[co]=0
  171.   next
  172.   for co=0 to 31
  173.     bi=x and bp
  174.     cq=co+p
  175.     if (bi<>0) then
  176.       if ((cq>=0)and(cq<32)) then
  177.         ba[cq]=1
  178.       end if
  179.     end if
  180.     bp = bp + bp
  181.   next
  182.   bp=1
  183.   y=0
  184.   '
  185.  ' SUPPORT FOR ARITHMETIC RIGHT SHIFTS
  186.  '
  187.  d=100
  188.   if (ar) then
  189.     if (x and 0x80000000) then
  190.       d=31+p
  191.     end if
  192.   end if
  193.   '
  194.  for co=0 to 31
  195.    if ((ba[co]<>0)or(co>=d)) then
  196.       y=y or bp
  197.     end if
  198.     bp = bp + bp
  199.   next
  200.   shifts=y
  201. end function
  202.  
  203.  
  204. ' PRINT shifts(0x80000000,2),"\n"
  205. ' PRINT shifts(-32,-2,1),"\n"
  206. ' PRINT shifts(8,-2),"\n"
  207.  
  208.  
  209. END MODULE
  210.  

interface.c
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.  

3
BBC BASIC / Re: Interfacing with a DHT11/12/22 sensor
« on: June 07, 2019, 07:27:49 AM »
 I fixed the Smiley problem.  8)

4
BBC BASIC / Re: Interfacing with a DHT11/12/22 sensor
« on: June 04, 2019, 03:40:54 AM »
It seems when you use the custom syntax highlighting option, Select All is no longer available. You can still select the text but you have to do it manually.

5
ScriptBasic / IUP GUI Toolkit
« on: May 19, 2019, 06:51:37 AM »
I created an IUP distibution for the Raspberry Pi 3B class boards. I also include the IUP extension module for ScriptBasic.

IUP (Portable User Interface) is a multi-platform toolkit for building graphical user interfaces.
IUP Project Site
IUP Download


CD (Canvas Draw) is a vector graphics toolkit with device independent output.
CD Project Site
CD Download


IM (Imaging) is a toolkit for image representation, storage, capture and processing.
IM Project Site
IM Download


ScriptBasic IUP Extension Module Download


Install Instructions

Download / extract each of the zips into a temporary work directory. Run sudo ./install in the IUP, CD and IM directories. If you want to install the development files, run sudo ./install_dev as well. Copy the iup.bas to your /usr/local/include/scriba directory. Copy the pui.so to your /usr/local/lib/scriba directory.

IUP ScriptBasic Online Dictionary
Code: Script BASIC
  1. IMPORT iup.bas
  2.  
  3. servers[0]="dict.org"
  4. servers[1]="dict1.us.dict.org"
  5. servers[2]="all.dict.org"
  6.  
  7. about="""This is a Demo
  8. of the IUP GUI Binding
  9. for Scriptbasic"""
  10.  
  11. ' Initialize IUP
  12. Iup::Open()
  13.  
  14. ' Create main window
  15.  
  16. win = Iup::Create("dialog")
  17.   Iup::SetAttributes(win, "TITLE=\"ScriptBasic IUP Online Dictionary\", SIZE=500x300")
  18.   Iup::SetCallback(win,"CLOSE_CB",ADDRESS(Win_exit()))
  19.  
  20. ' Create container to house ALL GUI objects
  21.  
  22. vbox = Iup::Create("vbox")
  23.   Iup::SetAttributes(vbox, "MARGIN=10x10")
  24.  
  25. ' Create server panel
  26.  
  27. topBox = Iup::Create("hbox")
  28.   Iup::SetAttributes(topBox, "GAP=10")
  29.   Iup::Append(vbox, topBox)
  30. serverFrame = Iup::Create("frame")
  31.   Iup::SetAttributes(serverFrame, "TITLE=Servers, EXPAND=YES")
  32.   Iup::Append(topBox, serverFrame)
  33. serverBox = Iup::Create("hbox")
  34.   Iup::SetAttributes(serverBox, "GAP=5")
  35.   Iup::Append(serverFrame, serverBox)
  36. serverCombo = Iup::Create("list")
  37.   Iup::SetAttributes(serverCombo, "DROPDOWN=YES, SIZE=120x, EXPAND=HORIZONTAL, VALUE=1")
  38.   Iup::Append(serverBox, serverCombo)
  39.   Iup::SetCallback(serverCombo, "ACTION", ADDRESS(serverCombo_selected()))
  40. btnFetch = Iup::Create("button")
  41.   Iup::SetAttributes(btnFetch, "TITLE=Fetch, SIZE = 50x")
  42.   Iup::Append(serverBox, btnFetch)
  43.   Iup::SetCallback(btnFetch, "ACTION", ADDRESS(btnFetch_clicked()))
  44.  
  45. ' Create control panel
  46.  
  47. controlFrame = Iup::Create("frame")
  48.   Iup::SetAttributes(controlFrame, "TITLE=Controls")
  49.   Iup::Append(topBox, controlFrame)
  50. controlBox = Iup::Create("hbox")
  51.   Iup::SetAttributes(controlBox, "GAP=5")
  52.   Iup::Append(controlFrame, controlBox)
  53. btnAbout = Iup::Create("button")
  54.   Iup::SetAttributes(btnAbout, "TITLE=About, SIZE = 50x")
  55.   Iup::Append(controlBox, btnAbout)
  56.   Iup::SetCallback(btnAbout, "ACTION", ADDRESS(btnAbout_clicked()))
  57. btnClear = Iup::Create("button")
  58.   Iup::SetAttributes(btnClear, "TITLE=Clear, SIZE = 50x")
  59.   Iup::Append(controlBox, btnClear)
  60.   Iup::SetCallback(btnClear, "ACTION", ADDRESS(btnClear_clicked()))
  61. btnExit = Iup::Create("button")
  62.   Iup::SetAttributes(btnExit, "TITLE=Exit, SIZE = 50x")
  63.   Iup::Append(controlBox, btnExit)
  64.   Iup::SetCallback(btnExit,"ACTION",ADDRESS(Win_exit()))
  65.  
  66. ' Create dictionary panel
  67.  
  68. dictFrame = Iup::Create("frame")
  69.   Iup::SetAttributes(dictFrame, "TITLE=Dictionaries")
  70.   Iup::Append(vbox, dictFrame)
  71. serverList = Iup::Create("list")
  72.   Iup::SetAttributes(serverList, "EXPAND=YES, VISIBLELINES=1")
  73.   Iup::Append(dictFrame, serverList)
  74.   Iup::SetCallback(serverList, "ACTION", ADDRESS(serverList_selected()))
  75.  
  76. ' Create text part
  77.  
  78. transFrame = IUP::Create("frame")
  79.   Iup::SetAttributes(transFrame, "TITLE=Translation")
  80.   Iup::Append(vbox, transFrame)
  81. text = Iup::Create("text")
  82.   Iup::SetAttributes(text, "MULTILINE=YES, EXPAND=YES")
  83.   Iup::Append(transFrame, text)
  84.  
  85. ' Create entry and search button
  86.  
  87. bottomBox = Iup::Create("hbox")
  88.   Iup::SetAttributes(bottomBox, "GAP=10")
  89.   Iup::Append(vbox, bottomBox)
  90. label = Iup::Create("label")
  91.   Iup::SetAttributes(label, "TITLE=\"Enter Word to Search For:\", SIZE=x12")
  92.   Iup::Append(bottomBox, label)
  93. entry = Iup::Create("text")
  94.   Iup::SetAttributes(entry, "EXPAND=HORIZONTAL")
  95.   Iup::Append(bottomBox, entry)
  96. btnSearch = Iup::Create("button")
  97.   Iup::SetAttributes(btnSearch,"TITLE=Search, SIZE=50x")
  98.   Iup::Append(bottomBox, btnSearch)
  99.   Iup::SetCallback(btnSearch, "ACTION", ADDRESS(btnSearch_clicked()))
  100. chkAll = Iup::Create("toggle")
  101.   Iup::SetAttributes(chkAll, "TITLE=ALL, SIZE=x12")
  102.   Iup::Append(bottomBox, chkAll)
  103. chkUTF = Iup::Create("toggle")
  104.   Iup::SetAttributes(chkUTF, "TITLE=UTF-8, SIZE=x12")
  105.   Iup::Append(bottomBox, chkUTF)
  106.  
  107. ' Add the main GUI container to the Window
  108.  
  109. Iup::Append(win, vbox)
  110.  
  111. ' Setup dialog defaults
  112.  
  113. Iup::Show(win)
  114. Iup::SetFocus(btnFetch)
  115. FOR i = 0 TO UBOUND(servers)
  116.   Iup::SetAttribute(serverCombo, "APPENDITEM", servers[i])
  117. NEXT
  118. Iup::SetAttribute(serverCombo, "VALUE", "1")
  119. Iup::Update(serverCombo)
  120. server_selection = servers[0]
  121.  
  122. ' Main processing loop
  123.  
  124. Iup::MainLoop()
  125. Iup::Close()
  126. END
  127.  
  128. ' Callback routines
  129.  
  130. SUB Win_exit
  131.   Iup::ExitLoop = TRUE
  132. END SUB
  133.  
  134. SUB btnAbout_clicked
  135.   Iup::Message("ABOUT", about)
  136. END SUB
  137.  
  138. SUB serverCombo_selected
  139.   server_selection = Iup::GetListText()
  140. END SUB
  141.  
  142. SUB serverList_selected
  143.   whichDictionary = Iup::GetListText()
  144. END SUB
  145.  
  146. SUB btnFetch_clicked
  147.   LOCAL dat, total, count
  148.   ON ERROR GOTO G_NetError
  149.   OPEN server_selection & ":2628" FOR SOCKET AS #1
  150.   PRINT#1,"SHOW DB\n"
  151.   LINE INPUT#1, dat
  152.   LINE INPUT#1, dat
  153.   count = 0
  154.   WHILE LEFT(dat, 1) <> "."
  155.     LINE INPUT#1, dat
  156.     IF LEFT(dat, 1) <> "." THEN total[count] = TRIM(dat)
  157.     count+=1
  158.   WEND
  159.   PRINT#1,"QUIT\n"
  160.   CLOSE(#1)
  161.   FOR cnt = 0 TO count - 2
  162.     Iup::SetAttribute(serverList, "APPENDITEM", total[cnt])
  163.   NEXT
  164.   Iup::SetAttribute(serverList, "VALUE", "1")
  165.   Iup::Update(serverCombo)
  166.   whichDictionary = total[0]
  167.   EXIT SUB
  168.  
  169.   G_NetError:
  170.   PRINT "Server ",server_selection," not available. (",ERROR,")\n"
  171. END SUB
  172.  
  173. SUB btnClear_clicked
  174.   Iup::ClearList(serverList)
  175.   Iup::SetAttribute(text, "VALUE", "")
  176.   Iup::SetAttribute(entry, "VALUE", "")
  177. END SUB
  178.  
  179. SUB btnSearch_clicked
  180.   LOCAL dict, dat, total, info
  181.   IUP::SetAttribute(text, "VALUE","Fetching....")
  182.   ON ERROR GOTO L_NetError
  183.   dict = LEFT(whichDictionary, INSTR(whichDictionary, " "))
  184.   OPEN server_selection & ":2628" FOR SOCKET AS 1
  185.   IF Iup::GetAttribute(chkAll, "VALUE") THEN
  186.     PRINT#1,"DEFINE * " & Iup::GetAttribute(entry,"VALUE") & "\n"
  187.   ELSE
  188.     PRINT#1,"DEFINE " & dict & " " & Iup::GetAttribute(entry,"VALUE") & "\n"
  189.   END IF
  190.   REPEAT
  191.     LINE INPUT#1, dat
  192.     IF LEFT(dat, 3) = "151" THEN
  193.       total$ &= "------------------------------\r\n"
  194.       total$ &= RIGHT(dat, LEN(dat) - LEN(Iup::GetAttribute(entry, "VALUE")) - LEN(dict))
  195.       total$ &= "------------------------------\r\n"
  196.       REPEAT
  197.         LINE INPUT#1, info
  198.         info = REPLACE(info, CHR(34), CHR(92) & CHR(34))
  199.         IF LEFT(info, 1) <> "." THEN total &= TRIM(info) & "\n"
  200.       UNTIL LEFT(info, 1) = "."
  201.       total &= "\n"
  202.     END IF
  203.   UNTIL LEFT(dat, 3) = "250" OR VAL(LEFT(dat, 3)) > 499
  204.   PRINT#1,"QUIT\n"
  205.   CLOSE(#1)
  206.   IF LEFT(dat, 3) = "552" THEN
  207.     total = "No match found."
  208.   ELSE IF LEFT(dat, 3) = "501" THEN
  209.     total = "Select a dictionary first!"
  210.   ELSE IF LEFT(dat, 3) = "550" THEN
  211.     total = "Invalid database!"
  212.   END IF
  213.   Iup::SetAttribute(text, "VALUE", total)
  214. EXIT SUB
  215.  
  216. L_NetError:
  217.   dat[0] = "Could not lookup word! (" & ERROR & ")"
  218.   Iup::SetAttribute(text, "VALUE", dat)
  219. END SUB
  220.  

6
BBC BASIC / Re: Simple GPIO demo
« on: April 30, 2019, 10:44:31 PM »
/dev/i2c-* is what I used for the ScriptBasic SHAT extension module.

Thanks for the info how this stuff works!

7
BBC BASIC / Re: Simple GPIO demo
« on: April 30, 2019, 08:29:37 PM »
 Very cool Richard. Have you had any luck with the RPi Sense HAT board? Will your GPIO library allow use of the Sense HAT emulator that comes with Raspbian?

8
ScriptBasic / Sense HAT Unchained
« on: April 27, 2019, 06:12:51 AM »
Sense HAT Unchained

The LED and Joystick functions will be in a separate extension module called SLED.

Code: C
  1. /* Raspberry Pi Sense HAT
  2. UXLIBS: -lpthread -lm -lc /usr/lib/gcc/arm-linux-gnueabihf/6/libgcc.a
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <sys/ioctl.h>
  12. #include <linux/i2c-dev.h>
  13. #include "../../basext.h"
  14.  
  15.  
  16. /***************************
  17.  Extension Module Functions
  18. ***************************/
  19.  
  20. typedef struct _ModuleObject {
  21.   void *HandleArray;
  22. }ModuleObject,*pModuleObject;
  23.  
  24.  
  25. besVERSION_NEGOTIATE
  26.   return (int)INTERFACE_VERSION;
  27. besEND
  28.  
  29.  
  30. besSUB_START
  31.   pModuleObject p;
  32.  
  33.   besMODULEPOINTER = besALLOC(sizeof(ModuleObject));
  34.   if( besMODULEPOINTER == NULL )return 0;
  35.  
  36.   p = (pModuleObject)besMODULEPOINTER;
  37.   return 0;
  38. besEND
  39.  
  40.  
  41. besSUB_FINISH
  42.   pModuleObject p;
  43.  
  44.   p = (pModuleObject)besMODULEPOINTER;
  45.   if( p == NULL )return 0;
  46.   return 0;
  47. besEND
  48.  
  49.  
  50. /***************
  51.  GPIO Functions
  52. ***************/
  53.  
  54. // I2C file handles
  55. static int file_bus = -1; // i2c bus
  56. static int file_hum = -1; // humidity/temp sensor
  57. static int file_pres = -1; // pressure sensor
  58. static int file_acc = -1; // accelerometer/gyro
  59. static int file_mag = -1; // magnetometer
  60.  
  61. static int i2cRead(int iHandle, unsigned char ucAddr, unsigned char *buf, int iLen);
  62. static int i2cWrite(int iHandle, unsigned char ucAddr, unsigned char *buf, int iLen);
  63. // humidity/temp calibration values
  64. static int H0_rH_x2, H1_rH_x2, T0_degC_x8;
  65. static int T1_degC_x8, H0_T0_OUT;
  66. static int H1_T0_OUT, T0_OUT, T1_OUT;
  67.  
  68. static int i2cRead(int iHandle, unsigned char ucAddr, unsigned char *buf, int iLen)
  69. {
  70. int rc;
  71.  
  72.   rc = write(iHandle, &ucAddr, 1);
  73.   if (rc == 1)
  74.   {
  75.     rc = read(iHandle, buf, iLen);
  76.   }
  77.   return rc;
  78. } /* i2cRead() */
  79.  
  80. static int i2cWrite(int iHandle, unsigned char ucAddr, unsigned char *buf, int iLen)
  81. {
  82. unsigned char ucTemp[512];
  83. int rc;
  84.  
  85.   if (iLen > 511 || iLen < 1 || buf == NULL)
  86.     return -1; // invalid write
  87.  
  88.   ucTemp[0] = ucAddr; // send the register number first
  89.   memcpy(&ucTemp[1], buf, iLen); // followed by the data
  90.   rc = write(iHandle, ucTemp, iLen+1);
  91.   return rc-1;
  92.  
  93. } /* i2cWrite() */
  94.  
  95.  
  96. besFUNCTION(shInit)
  97.   int iChannel;
  98.  
  99.   besARGUMENTS("i")
  100.     &iChannel
  101.   besARGEND
  102.  
  103.   unsigned char ucTemp[32];
  104.   char filename[32];
  105.  
  106.   sprintf(filename, "/dev/i2c-%d", iChannel);
  107.   if ((file_bus = open(filename, O_RDWR)) < 0)
  108.   {
  109.     fprintf(stderr, "Failed to open the i2c bus\n");
  110.     besRETURN_LONG(-1);
  111.   }
  112.  
  113.  
  114.   file_acc = open(filename, O_RDWR);
  115.   if (ioctl(file_acc, I2C_SLAVE, 0x6a) < 0)
  116.   {
  117.     fprintf(stderr, "Failed to acquire bus for accelerometer\n");
  118.     goto badexit;
  119.   }
  120.  
  121.   file_mag = open(filename, O_RDWR);
  122.   if (ioctl(file_mag, I2C_SLAVE, 0x1c) < 0)
  123.   {
  124.     fprintf(stderr, "Failed to acquire bus for magnetometer\n");
  125.     goto badexit;
  126.   }
  127.  
  128.   file_hum = open(filename, O_RDWR);
  129.   if (ioctl(file_hum, I2C_SLAVE, 0x5f) < 0)
  130.   {
  131.     fprintf(stderr, "Failed to acquire bus for Humidity sensor\n");
  132.     goto badexit;
  133.   }
  134.   file_pres = open(filename, O_RDWR);
  135.   if (ioctl(file_pres, I2C_SLAVE, 0x5C) < 0)
  136.   {
  137.     fprintf(stderr, "Failed to aquire bus for Pressure sensor\n");
  138.     goto badexit;
  139.   }
  140.   // Prepare humidity sensor
  141.   i2cRead(file_hum, 0x10, ucTemp, 1); // AV_CONF
  142.   ucTemp[0] &= 0xc0;
  143.   ucTemp[0] |= 0x1b; // avgt=16, avgh=32
  144.   i2cWrite(file_hum, 0x10, ucTemp, 1);
  145.  
  146.   i2cRead(file_hum, 0x20+0x80, ucTemp, 3); // get CTRL_REG 1-3
  147.   ucTemp[0] &= 0x78; // keep reserved bits
  148.   ucTemp[0] |= 0x81; // turn on + 1Hz sample rate
  149.   ucTemp[1] &= 0x7c; // turn off heater + boot + one shot
  150.   i2cWrite(file_hum, 0x20+0x80, ucTemp, 3); // turn on + set sample rate
  151.  
  152.   // Get the H/T calibration values
  153.   i2cRead(file_hum, 0x30+0x80, ucTemp, 16);
  154.   H0_rH_x2 = ucTemp[0];
  155.   H1_rH_x2 = ucTemp[1];
  156.   T0_degC_x8 = ucTemp[2];
  157.   T1_degC_x8 = ucTemp[3];
  158.   T0_degC_x8 |= ((ucTemp[5] & 0x3) << 8); // 2 msb bits
  159.   T1_degC_x8 |= ((ucTemp[5] & 0xc) << 6);
  160.   H0_T0_OUT = ucTemp[6] | (ucTemp[7] << 8);
  161.   H1_T0_OUT = ucTemp[10] | (ucTemp[11] << 8);
  162.   T0_OUT = ucTemp[12] | (ucTemp[13] << 8);
  163.   T1_OUT = ucTemp[14] | (ucTemp[15] << 8);
  164.   if (H0_T0_OUT > 32767) H0_T0_OUT -= 65536; // signed
  165.   if (H1_T0_OUT > 32767) H1_T0_OUT -= 65536;
  166.   if (T0_OUT > 32767) T0_OUT -= 65536;
  167.   if (T1_OUT > 32767) T1_OUT -= 65536;
  168.  
  169.   // prepare pressure sensor
  170.   ucTemp[0] = 0x90; // turn on and set 1Hz update
  171.   i2cWrite(file_pres, 0x20, ucTemp, 1);
  172.  
  173.   // Init magnetometer
  174.   ucTemp[0] = 0x48; // output data rate/power mode
  175.   ucTemp[1] = 0x00; // default scale
  176.   ucTemp[2] = 0x00; // continuous conversion
  177.   ucTemp[3] = 0x08; // high performance mode
  178.   i2cWrite(file_mag, 0x20+0x80, ucTemp, 4);
  179.  
  180.   // Init accelerometer/gyroscope
  181.   ucTemp[0] = 0x60; // 119hz accel
  182.   i2cWrite(file_acc, 0x20, ucTemp, 1);
  183.   ucTemp[0] = 0x38; // enable gyro on all axes
  184.   i2cWrite(file_acc, 0x1e, ucTemp, 1);
  185.         ucTemp[0] = 0x28; // data rate + full scale + bw selection
  186.   // bits:        ODR_G2 | ODR_G1 | ODR_G0 | FS_G1 | FS_G0 | 0 | BW_G1 | BW_G0
  187.   // 0x28 = 14.9hz, 500dps
  188.   i2cWrite(file_acc, 0x10, ucTemp, 1); // gyro ctrl_reg1
  189.  
  190.   besRETURN_LONG(1);
  191.  
  192.   // problems opening the I2C handles/addresses
  193.   badexit:
  194.   if (file_hum != -1)
  195.   {
  196.     close(file_hum);
  197.     file_hum = -1;
  198.   }
  199.   if (file_pres != -1)
  200.   {
  201.     close(file_pres);
  202.     file_pres = -1;
  203.   }
  204.   if (file_acc != -1)
  205.   {
  206.     close(file_acc);
  207.     file_acc = -1;
  208.   }
  209.   if (file_mag != -1)
  210.   {
  211.     close(file_mag);
  212.     file_mag = -1;
  213.   }
  214.   besRETURN_LONG(0);
  215. besEND
  216.  
  217.  
  218. besFUNCTION(shGetAccel)
  219.   VARIABLE Argument1;
  220.   VARIABLE Argument2;
  221.   VARIABLE Argument3;
  222.   unsigned long __refcount_;
  223.   LEFTVALUE Lval1;
  224.   LEFTVALUE Lval2;
  225.   LEFTVALUE Lval3;
  226.  
  227.   Argument1 = besARGUMENT(1);
  228.   besLEFTVALUE(Argument1,Lval1);
  229.   besRELEASE(*Lval1);
  230.   *Lval1 = NULL;
  231.  
  232.   Argument2 = besARGUMENT(2);
  233.   besLEFTVALUE(Argument2,Lval2);
  234.   besRELEASE(*Lval2);
  235.   *Lval2 = NULL;
  236.  
  237.   Argument3 = besARGUMENT(3);
  238.   besLEFTVALUE(Argument3,Lval3);
  239.   besRELEASE(*Lval3);
  240.   *Lval3 = NULL;
  241.  
  242.   unsigned char ucTemp[8];
  243.   int rc;
  244.  
  245.   *Lval1 = besNEWLONG;
  246.   *Lval2 = besNEWLONG;
  247.   *Lval3 = besNEWLONG;
  248.  
  249.   rc = i2cRead(file_acc, 0x28+0x80, ucTemp, 6);
  250.   if (rc == 6)
  251.   {
  252.     int x, y, z;
  253.  
  254.     x = ucTemp[0] + (ucTemp[1] << 8);
  255.     y = ucTemp[2] + (ucTemp[3] << 8);
  256.     z = ucTemp[4] + (ucTemp[5] << 8);
  257.     // fix the signed values
  258.     if (x > 32767) x -= 65536;
  259.     if (y > 32767) y -= 65536;
  260.     if (z > 32767) z -= 65536;
  261.  
  262.     LONGVALUE(*Lval1) = x;
  263.     LONGVALUE(*Lval2) = y;
  264.     LONGVALUE(*Lval3) = z;
  265.     besRETURN_LONG(1);
  266.   }
  267.   besRETURN_LONG(0);
  268. besEND
  269.  
  270.  
  271. besFUNCTION(shGetGyro)
  272.   VARIABLE Argument1;
  273.   VARIABLE Argument2;
  274.   VARIABLE Argument3;
  275.   unsigned long __refcount_;
  276.   LEFTVALUE Lval1;
  277.   LEFTVALUE Lval2;
  278.   LEFTVALUE Lval3;
  279.  
  280.   Argument1 = besARGUMENT(1);
  281.   besLEFTVALUE(Argument1,Lval1);
  282.   besRELEASE(*Lval1);
  283.   *Lval1 = NULL;
  284.  
  285.   Argument2 = besARGUMENT(2);
  286.   besLEFTVALUE(Argument2,Lval2);
  287.   besRELEASE(*Lval2);
  288.   *Lval2 = NULL;
  289.  
  290.   Argument3 = besARGUMENT(3);
  291.   besLEFTVALUE(Argument3,Lval3);
  292.   besRELEASE(*Lval3);
  293.   *Lval3 = NULL;
  294.  
  295.   unsigned char ucTemp[8];
  296.   int rc;
  297.  
  298.   *Lval1 = besNEWLONG;
  299.   *Lval2 = besNEWLONG;
  300.   *Lval3 = besNEWLONG;
  301.  
  302.   rc = i2cRead(file_acc, 0x18+0x80, ucTemp, 6);
  303.   if (rc == 6)
  304.   {
  305.     LONGVALUE(*Lval1) = ucTemp[0] + (ucTemp[1] << 8);
  306.     LONGVALUE(*Lval2) = ucTemp[2] + (ucTemp[3] << 8);
  307.     LONGVALUE(*Lval3) = ucTemp[4] + (ucTemp[5] << 8);
  308.     besRETURN_LONG(1);
  309.   }
  310.   besRETURN_LONG(0);
  311. besEND
  312.  
  313.  
  314. besFUNCTION(shGetMagneto)
  315.   VARIABLE Argument1;
  316.   VARIABLE Argument2;
  317.   VARIABLE Argument3;
  318.   unsigned long __refcount_;
  319.   LEFTVALUE Lval1;
  320.   LEFTVALUE Lval2;
  321.   LEFTVALUE Lval3;
  322.  
  323.   Argument1 = besARGUMENT(1);
  324.   besLEFTVALUE(Argument1,Lval1);
  325.   besRELEASE(*Lval1);
  326.   *Lval1 = NULL;
  327.  
  328.   Argument2 = besARGUMENT(2);
  329.   besLEFTVALUE(Argument2,Lval2);
  330.   besRELEASE(*Lval2);
  331.   *Lval2 = NULL;
  332.  
  333.   Argument3 = besARGUMENT(3);
  334.   besLEFTVALUE(Argument3,Lval3);
  335.   besRELEASE(*Lval3);
  336.   *Lval3 = NULL;
  337.  
  338.   unsigned char ucTemp[8];
  339.   int rc;
  340.  
  341.   *Lval1 = besNEWLONG;
  342.   *Lval2 = besNEWLONG;
  343.   *Lval3 = besNEWLONG;
  344.  
  345.   rc = i2cRead(file_mag, 0x28+0x80, ucTemp, 6);
  346.   if (rc == 6)
  347.   {
  348.     int x, y, z;
  349.     x = ucTemp[0] + (ucTemp[1] << 8);
  350.     y = ucTemp[2] + (ucTemp[3] << 8);
  351.     z = ucTemp[4] + (ucTemp[5] << 8);
  352.     // fix signed values
  353.     if (x > 32767) x -= 65536;
  354.     if (y > 32767) y -= 65536;
  355.     if (z > 32767) z -= 65536;
  356.  
  357.     LONGVALUE(*Lval1) = x;
  358.     LONGVALUE(*Lval2) = y;
  359.     LONGVALUE(*Lval3) = z;
  360.     besRETURN_LONG(1);
  361.   }
  362.   besRETURN_LONG(0);
  363. besEND
  364.  
  365. besFUNCTION(shGetPressure)
  366.   VARIABLE Argument1;
  367.   VARIABLE Argument2;
  368.   unsigned long __refcount_;
  369.   LEFTVALUE Lval1;
  370.   LEFTVALUE Lval2;
  371.  
  372.   Argument1 = besARGUMENT(1);
  373.   besLEFTVALUE(Argument1,Lval1);
  374.   besRELEASE(*Lval1);
  375.   *Lval1 = NULL;
  376.  
  377.   Argument2 = besARGUMENT(2);
  378.   besLEFTVALUE(Argument2,Lval2);
  379.   besRELEASE(*Lval2);
  380.   *Lval2 = NULL;
  381.  
  382.   unsigned char ucTemp[8];
  383.   int rc, P, T;
  384.  
  385.   *Lval1 = besNEWLONG;
  386.   *Lval2 = besNEWLONG;
  387.  
  388.   if (file_pres != -1)
  389.   {
  390.     rc = i2cRead(file_pres, 0x28+0x80, ucTemp, 5);
  391.     if (rc == 5)
  392.     {
  393.       P = ucTemp[0] + (ucTemp[1]<<8) + (ucTemp[2]<<16);
  394.       LONGVALUE(*Lval1) = P / 4096; //hPa
  395.       T = ucTemp[3] + (ucTemp[4] << 8);
  396.       if (T > 32767) T -= 65536; // twos compliment
  397.       T = 425 + (T / 48); // 42.5 + T value/480
  398.       LONGVALUE(*Lval2) = T;
  399.     }
  400.     besRETURN_LONG(1);
  401.   }
  402.   besRETURN_LONG(0);
  403. besEND
  404.  
  405.  
  406. besFUNCTION(shGetTempHumid)
  407.   VARIABLE Argument1;
  408.   VARIABLE Argument2;
  409.   unsigned long __refcount_;
  410.   LEFTVALUE Lval1;
  411.   LEFTVALUE Lval2;
  412.  
  413.   Argument1 = besARGUMENT(1);
  414.   besLEFTVALUE(Argument1,Lval1);
  415.   besRELEASE(*Lval1);
  416.   *Lval1 = NULL;
  417.  
  418.   Argument2 = besARGUMENT(2);
  419.   besLEFTVALUE(Argument2,Lval2);
  420.   besRELEASE(*Lval2);
  421.   *Lval2 = NULL;
  422.  
  423.   unsigned char ucTemp[4];
  424.   int rc;
  425.   int H_T_out, T_out, T0_degC, T1_degC;
  426.   int H0_rh, H1_rh;
  427.   int tmp;
  428.  
  429.   *Lval1 = besNEWLONG;
  430.   *Lval2 = besNEWLONG;
  431.  
  432.   rc = i2cRead(file_hum, 0x28+0x80, ucTemp, 4);
  433.   if (rc == 4)
  434.   {
  435.     H_T_out = ucTemp[0] + (ucTemp[1] << 8);
  436.     T_out = ucTemp[2] + (ucTemp[3] << 8);
  437.     if (H_T_out > 32767) H_T_out -=65536;
  438.     if (T_out > 32767) T_out -= 65536;
  439.     T0_degC = T0_degC_x8 / 8;
  440.     T1_degC = T1_degC_x8 / 8;
  441.     H0_rh = H0_rH_x2 / 2;
  442.     H1_rh = H1_rH_x2 / 2;
  443.     tmp = (H_T_out - H0_T0_OUT) * (H1_rh - H0_rh)*10;
  444.     LONGVALUE(*Lval2) = tmp / (H1_T0_OUT - H0_T0_OUT) + H0_rh*10;
  445.     tmp = (T_out - T0_OUT) * (T1_degC - T0_degC)*10;
  446.     LONGVALUE(*Lval1) = tmp / (T1_OUT - T0_OUT) + T0_degC*10;
  447.     besRETURN_LONG(1);
  448.   }
  449.   besRETURN_LONG(0); // not ready
  450. besEND
  451.  
  452. besFUNCTION(shShutdown)
  453.   // Close all I2C file handles
  454.   if (file_bus != -1) close(file_bus);
  455.   if (file_hum != -1) close(file_hum);
  456.   if (file_pres != -1) close(file_pres);
  457.   if (file_acc != -1) close(file_acc);
  458.   if (file_mag != -1) close(file_mag);
  459.   file_bus = file_hum = file_pres = file_acc = file_mag = -1;
  460. besEND
  461.  

Code: Script BASIC
  1. ' Raspberry Pi Sense HAT
  2.  
  3. MODULE SHAT
  4.  
  5. DECLARE SUB  ::Init          ALIAS  "shInit"          LIB  "shat"
  6. DECLARE SUB  ::GetAccel      ALIAS  "shGetAccel"      LIB  "shat"
  7. DECLARE SUB  ::GetGyro       ALIAS  "shGetGyro"       LIB  "shat"
  8. DECLARE SUB  ::GetMagneto    ALIAS  "shGetMagneto"    LIB  "shat"
  9. DECLARE SUB  ::GetPressure   ALIAS  "shGetPressure"   LIB  "shat"
  10. DECLARE SUB  ::GetTempHumid  ALIAS  "shGetTempHumid"  LIB  "shat"
  11. DECLARE SUB  ::Shutdown      ALIAS  "shShutdown"      LIB  "shat"
  12.  
  13. END MODULE
  14.  


Code: Script BASIC
  1. IMPORT shat.bas
  2.  
  3. SHAT::Init(1)
  4.  
  5. SHAT::GetPressure(pressure, ptemp)
  6. SHAT::GetTempHumid(htemp, humid)
  7. SHAT::GetAccel(Ax, Ay, Az)
  8. SHAT::GetMagneto(Mx, My, Mz)
  9. SHAT::GetGyro(Gx, Gy, Gz)
  10.  
  11. PRINT "Pressure: ", pressure, "\n"
  12. PRINT "P-Temp: ", ptemp,"\n"
  13. PRINT "Humidity: ", humid,"\n"
  14. PRINT "H-Temp: ", htemp,"\n"
  15. PRINT "Accel-x: ", Ax, "\n"
  16. PRINT "Accel-y: ", Ay, "\n"
  17. PRINT "Accel-z: ", Az, "\n"
  18. PRINT "Mag-x: ", Mx, "\n"
  19. PRINT "Mag-y: ", My, "\n"
  20. PRINT "Mag-z: ", Mz, "\n"
  21. PRINT "Gyro-x: ", Gx, "\n"
  22. PRINT "Gyro-y: ", Gy, "\n"
  23. PRINT "Gyro-z: ", Gz, "\n"
  24.  
  25. SHAT::Shutdown
  26.  


pi@RPi3B:~/sbrpi/examples $ time scriba testhat.sb
Pressure: 1026
P-Temp: 320
Humidity: 287
H-Temp: 325
Accel-x: -443
Accel-y: -186
Accel-z: 15830
Mag-x: -1050
Mag-y: 2290
Mag-z: -1314
Gyro-x: 211
Gyro-y: 65435
Gyro-z: 74

real   0m0.043s
user   0m0.014s
sys   0m0.013s
pi@RPi3B:~/sbrpi/examples $




9
ScriptBasic / Re: RPi GPIO Support
« on: April 26, 2019, 07:36:36 PM »
Hippy,

I believe the routines themselves are doing the low level memory access. I would like to know how I can use this library with the RPi Sense HAT board/emulator.

10
ScriptBasic / Re: RPi GPIO Support
« on: April 26, 2019, 06:58:30 AM »
I was looking at the fb.h include and for grins I tried this little script. To my surprise the Sense HAT LEDs turned on. (one bright green and two blueish)

I have no idea what this did but it's interesting just opening the device with ScriptBasic produced a visible response.

Code: Script BASIC
  1. OPEN "/dev/fb1" FOR BINARY AS #1
  2. PRINT #1, 0x4600
  3. CLOSE(1)
  4.  

#define FBIOGET_VSCREENINFO   0x4600


11
ScriptBasic / Re: RPi GPIO Support
« on: April 25, 2019, 06:26:03 PM »
Hi Hippy,

I hope this post finds you and you join us here on the Raspberry BASIC forum.

Quote from: hippy@Rpi Forum
Quote from: John@Rpi Forum
    I'm assuming the gpio argument is a 0 or 1 indicating the physical hardware or an emuator.

I'm not sure what that means or where it would be set. Your test program worked for me but I can't do much more at present.

There doesn't seem to be any peek or poke to allow access to the GPIO memory, so I can't read the magic "aux0" register.

I didn't particularly like the names used for the RPi.GPIO routines, don't find them descriptive enough, so changed those in my library. For example, "gpio_function" became "SetAltFunction", "output_gpio" became "SetOutput", "input_gpio" became "GetInput". There's no need for the "gpio" in any when that is already used in the main program, eg -

GPIO::SetAltFunction( 17, GPIO::ALT0 )

You need the other ALTn definitions as well. IN=1 and OUT=0 will be contentious for some, but that is what I have also used in my library.

"short_wait" is only used internally by the library so doesn't need to be available. I turned that into "void Nops(int n)" and made that available, updated the library to use Nops(150).

It would be useful if you could provide the library source code and details of how to build it.

Continued ...

One aside, and I haven't really investigated it yet ...

The Broardcom BCM 2835 Peripheral PDF talks of read and write memory barriers which should be executed before the first read and after the last write (page 7). The best I can ascertain is this is because one isn't actually updating things at the instant when a register is written or read but passing stuff through a bus and/or buffer to get things updated or read. Don't use the barriers and things can go screwy.

But I'm not exactly sure when they need to be used or how they are specified, and most of the GPIO interfacing libraries don't seem to use them nor even mention them. Does that mean they aren't needed, don't need to be explicitly specified, or the libraries aren't as safe or reliable as they should be ? Are memory barriers only required for BCM 2835 or also for later variants ? Should code be different for differing Pi variants ? Will libraries not using memory barriers not work as safely or as reliably, or differently, on a BCM 2835 ?

I have no idea at present. I thought the GPIO peripheral block was the same for all Pi variants and those barriers should be there. But they aren't.


What is the difference between BOARD and BCM for GPIO pin numbering?

I think I need to add the other C library functions as well to the gpio extension module. The event C / .h seems to be a major missing piece.

If you want to build the gpio extension module and don't want to run ./setup from scratch, just add a gpio directory in bin/mod/obj/ and then do a ./setup --module=gpio.

gpio extension module (interface.c)
Code: C
  1. /* GPIO Extension Module
  2. UXLIBS: -lc
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <sys/mman.h>
  12. #include "c_gpio.h"
  13. #include "../../basext.h"
  14.  
  15. #define BCM2708_PERI_BASE_DEFAULT   0x20000000
  16. #define BCM2709_PERI_BASE_DEFAULT   0x3f000000
  17. #define GPIO_BASE_OFFSET            0x200000
  18. #define FSEL_OFFSET                 0   // 0x0000
  19. #define SET_OFFSET                  7   // 0x001c / 4
  20. #define CLR_OFFSET                  10  // 0x0028 / 4
  21. #define PINLEVEL_OFFSET             13  // 0x0034 / 4
  22. #define EVENT_DETECT_OFFSET         16  // 0x0040 / 4
  23. #define RISING_ED_OFFSET            19  // 0x004c / 4
  24. #define FALLING_ED_OFFSET           22  // 0x0058 / 4
  25. #define HIGH_DETECT_OFFSET          25  // 0x0064 / 4
  26. #define LOW_DETECT_OFFSET           28  // 0x0070 / 4
  27. #define PULLUPDN_OFFSET             37  // 0x0094 / 4
  28. #define PULLUPDNCLK_OFFSET          38  // 0x0098 / 4
  29.  
  30. #define PAGE_SIZE  (4*1024)
  31. #define BLOCK_SIZE (4*1024)
  32.  
  33. static volatile uint32_t *gpio_map;
  34.  
  35. static void short_wait(void)
  36. {
  37.     int i;
  38.  
  39.     for (i=0; i<150; i++) {    // wait 150 cycles
  40.         asm volatile("nop");
  41.     }
  42. }
  43.  
  44. static void clear_event_detect(int gpio)
  45. {
  46.     int offset = EVENT_DETECT_OFFSET + (gpio/32);
  47.     int shift = (gpio%32);
  48.  
  49.     *(gpio_map+offset) |= (1 << shift);
  50.     short_wait();
  51.     *(gpio_map+offset) = 0;
  52. }
  53.  
  54. static void set_pullupdn(int gpio, int pud)
  55. {
  56.     int clk_offset = PULLUPDNCLK_OFFSET + (gpio/32);
  57.     int shift = (gpio%32);
  58.  
  59.     if (pud == PUD_DOWN)
  60.         *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_DOWN;
  61.     else if (pud == PUD_UP)
  62.         *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_UP;
  63.     else  // pud == PUD_OFF
  64.         *(gpio_map+PULLUPDN_OFFSET) &= ~3;
  65.  
  66.     short_wait();
  67.     *(gpio_map+clk_offset) = 1 << shift;
  68.     short_wait();
  69.     *(gpio_map+PULLUPDN_OFFSET) &= ~3;
  70.     *(gpio_map+clk_offset) = 0;
  71. }
  72.  
  73. /**************************
  74.  Extension Module Functions
  75. **************************/
  76.  
  77. typedef struct _ModuleObject {
  78.   void *HandleArray;
  79. }ModuleObject,*pModuleObject;
  80.  
  81.  
  82. besVERSION_NEGOTIATE
  83.   return (int)INTERFACE_VERSION;
  84. besEND
  85.  
  86.  
  87. besSUB_START
  88.   pModuleObject p;
  89.  
  90.   besMODULEPOINTER = besALLOC(sizeof(ModuleObject));
  91.   if( besMODULEPOINTER == NULL )return 0;
  92.  
  93.   p = (pModuleObject)besMODULEPOINTER;
  94.   return 0;
  95. besEND
  96.  
  97.  
  98. besSUB_FINISH
  99.   pModuleObject p;
  100.  
  101.   p = (pModuleObject)besMODULEPOINTER;
  102.   if( p == NULL )return 0;
  103.   return 0;
  104. besEND
  105.  
  106.  
  107.  
  108. /***************
  109.  GPIO Functions
  110. ***************/
  111.  
  112.  
  113. besFUNCTION(sb_short_wait)
  114.     int i;
  115.  
  116.     for (i=0; i<150; i++) {    // wait 150 cycles
  117.         asm volatile("nop");
  118.     }
  119. besEND
  120.  
  121.  
  122.  
  123. besFUNCTION(sb_setup)
  124.     int mem_fd;
  125.     uint8_t *gpio_mem;
  126.     uint32_t peri_base = 0;
  127.     uint32_t gpio_base;
  128.     unsigned char buf[4];
  129.     FILE *fp;
  130.     char buffer[1024];
  131.     char hardware[1024];
  132.     int found = 0;
  133.  
  134.     // try /dev/gpiomem first - this does not require root privs
  135.     if ((mem_fd = open("/dev/gpiomem", O_RDWR|O_SYNC)) > 0)
  136.     {
  137.         if ((gpio_map = (uint32_t *)mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, 0)) == MAP_FAILED) {
  138.             besRETURN_LONG(SETUP_MMAP_FAIL);
  139.         } else {
  140.             besRETURN_LONG(SETUP_OK);
  141.         }
  142.     }
  143.     // revert to /dev/mem method - requires root
  144.  
  145.     // determine peri_base
  146.     if ((fp = fopen("/proc/device-tree/soc/ranges", "rb")) != NULL) {
  147.         // get peri base from device tree
  148.         fseek(fp, 4, SEEK_SET);
  149.         if (fread(buf, 1, sizeof buf, fp) == sizeof buf) {
  150.             peri_base = buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3] << 0;
  151.         }
  152.         fclose(fp);
  153.     } else {
  154.         // guess peri base based on /proc/cpuinfo hardware field
  155.         if ((fp = fopen("/proc/cpuinfo", "r")) == NULL)
  156.             besRETURN_LONG(SETUP_CPUINFO_FAIL);
  157.  
  158.         while(!feof(fp) && !found && fgets(buffer, sizeof(buffer), fp)) {
  159.             sscanf(buffer, "Hardware    : %s", hardware);
  160.             if (strcmp(hardware, "BCM2708") == 0 || strcmp(hardware, "BCM2835") == 0) {
  161.                 // pi 1 hardware
  162.                 peri_base = BCM2708_PERI_BASE_DEFAULT;
  163.                 found = 1;
  164.             } else if (strcmp(hardware, "BCM2709") == 0 || strcmp(hardware, "BCM2836") == 0) {
  165.                 // pi 2 hardware
  166.                 peri_base = BCM2709_PERI_BASE_DEFAULT;
  167.                 found = 1;
  168.             }
  169.         }
  170.         fclose(fp);
  171.         if (!found)
  172.             besRETURN_LONG(SETUP_NOT_RPI_FAIL);
  173.     }
  174.  
  175.     if (!peri_base)
  176.         besRETURN_LONG(SETUP_NOT_RPI_FAIL);
  177.     gpio_base = peri_base + GPIO_BASE_OFFSET;
  178.  
  179.     // mmap the GPIO memory registers
  180.     if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0)
  181.         besRETURN_LONG(SETUP_DEVMEM_FAIL);
  182.  
  183.     if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL)
  184.         besRETURN_LONG(SETUP_MALLOC_FAIL);
  185.  
  186.     if ((uint32_t)gpio_mem % PAGE_SIZE)
  187.         gpio_mem += PAGE_SIZE - ((uint32_t)gpio_mem % PAGE_SIZE);
  188.  
  189.     if ((gpio_map = (uint32_t *)mmap( (void *)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, gpio_base)) == MAP_FAILED)
  190.         besRETURN_LONG(SETUP_MMAP_FAIL);
  191.  
  192.     besRETURN_LONG(SETUP_OK);
  193. besEND
  194.  
  195.  
  196. besFUNCTION(sb_clear_event_detect)
  197.   int gpio;
  198.   besARGUMENTS("i")
  199.     &gpio
  200.   besARGEND
  201.   int offset = EVENT_DETECT_OFFSET + (gpio/32);
  202.   int shift = (gpio%32);
  203.   *(gpio_map+offset) |= (1 << shift);
  204.   short_wait();
  205.   *(gpio_map+offset) = 0;
  206. besEND
  207.  
  208.  
  209. besFUNCTION(sb_eventdetected)
  210.   int gpio;
  211.   besARGUMENTS("i")
  212.     &gpio
  213.   besARGEND
  214.   int offset, value, bit;
  215.   offset = EVENT_DETECT_OFFSET + (gpio/32);
  216.   bit = (1 << (gpio%32));
  217.   value = *(gpio_map+offset) & bit;
  218.   if (value)
  219.     clear_event_detect(gpio);
  220.   besRETURN_LONG(value);
  221. besEND
  222.  
  223.  
  224. besFUNCTION(sb_set_rising_event)
  225.   int gpio, enable;
  226.   besARGUMENTS("ii")
  227.     &gpio, &enable
  228.   besARGEND
  229.   int offset = RISING_ED_OFFSET + (gpio/32);
  230.   int shift = (gpio%32);
  231.   if (enable)
  232.     *(gpio_map+offset) |= 1 << shift;
  233.   else
  234.     *(gpio_map+offset) &= ~(1 << shift);
  235.   clear_event_detect(gpio);
  236. besEND
  237.  
  238.  
  239. besFUNCTION(sb_set_falling_event)
  240.   int gpio, enable;
  241.   besARGUMENTS("ii")
  242.     &gpio, &enable
  243.   besARGEND
  244.   int offset = FALLING_ED_OFFSET + (gpio/32);
  245.   int shift = (gpio%32);
  246.   if (enable) {
  247.     *(gpio_map+offset) |= (1 << shift);
  248.     *(gpio_map+offset) = (1 << shift);
  249.   } else {
  250.     *(gpio_map+offset) &= ~(1 << shift);
  251.   }
  252.   clear_event_detect(gpio);
  253. besEND
  254.  
  255.  
  256. besFUNCTION(sb_set_high_event)
  257.   int gpio, enable;
  258.   besARGUMENTS("ii")
  259.     &gpio, &enable
  260.   besARGEND
  261.   int offset = HIGH_DETECT_OFFSET + (gpio/32);
  262.   int shift = (gpio%32);
  263.   if (enable)
  264.     *(gpio_map+offset) |= (1 << shift);
  265.   else
  266.     *(gpio_map+offset) &= ~(1 << shift);
  267.   clear_event_detect(gpio);
  268. besEND
  269.  
  270.  
  271. besFUNCTION(sb_set_low_event)
  272.   int gpio, enable;
  273.   besARGUMENTS("ii")
  274.     &gpio, &enable
  275.   besARGEND
  276.   int offset = LOW_DETECT_OFFSET + (gpio/32);
  277.   int shift = (gpio%32);
  278.   if (enable)
  279.     *(gpio_map+offset) |= 1 << shift;
  280.   else
  281.     *(gpio_map+offset) &= ~(1 << shift);
  282.   clear_event_detect(gpio);
  283. besEND
  284.  
  285.  
  286. besFUNCTION(sb_set_pullupdn)
  287.   int gpio, pud;
  288.   besARGUMENTS("ii")
  289.     &gpio, &pud
  290.   besARGEND
  291.   int clk_offset = PULLUPDNCLK_OFFSET + (gpio/32);
  292.   int shift = (gpio%32);
  293.   if (pud == PUD_DOWN)
  294.     *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_DOWN;
  295.   else if (pud == PUD_UP)
  296.     *(gpio_map+PULLUPDN_OFFSET) = (*(gpio_map+PULLUPDN_OFFSET) & ~3) | PUD_UP;
  297.   else  // pud == PUD_OFF
  298.     *(gpio_map+PULLUPDN_OFFSET) &= ~3;
  299.     short_wait();
  300.     *(gpio_map+clk_offset) = 1 << shift;
  301.     short_wait();
  302.     *(gpio_map+PULLUPDN_OFFSET) &= ~3;
  303.     *(gpio_map+clk_offset) = 0;
  304. besEND
  305.  
  306.  
  307. besFUNCTION(sb_setup_gpio)
  308. int gpio, direction, pud;
  309.   besARGUMENTS("iii")
  310.     &gpio, &direction, &pud
  311.   besARGEND
  312.   int offset = FSEL_OFFSET + (gpio/10);
  313.   int shift = (gpio%10)*3;
  314.   set_pullupdn(gpio, pud);
  315.   if (direction == OUTPUT)
  316.     *(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift)) | (1<<shift);
  317.   else  // direction == INPUT
  318.     *(gpio_map+offset) = (*(gpio_map+offset) & ~(7<<shift));
  319. besEND
  320.  
  321.  
  322. besFUNCTION(sb_gpio_function)
  323.   int gpio;
  324.   besARGUMENTS("i")
  325.     &gpio
  326.   besARGEND
  327.     int offset = FSEL_OFFSET + (gpio/10);
  328.     int shift = (gpio%10)*3;
  329.     int value = *(gpio_map+offset);
  330.     value >>= shift;
  331.     value &= 7;
  332.     besRETURN_LONG(value); // 0=input, 1=output, 4=alt0
  333. besEND
  334.  
  335.  
  336. besFUNCTION(sb_output_gpio)
  337.   int gpio, value;
  338.   besARGUMENTS("ii")
  339.     &gpio, &value
  340.   besARGEND
  341.   int offset, shift;
  342.   if (value) // value == HIGH
  343.     offset = SET_OFFSET + (gpio/32);
  344.   else       // value == LOW
  345.     offset = CLR_OFFSET + (gpio/32);
  346.   shift = (gpio%32);
  347.   *(gpio_map+offset) = 1 << shift;
  348. besEND
  349.  
  350.  
  351. besFUNCTION(sb_input_gpio)
  352.   int gpio;
  353.   besARGUMENTS("i")
  354.     &gpio
  355.   besARGEND
  356.   int offset, value, mask;
  357.   offset = PINLEVEL_OFFSET + (gpio/32);
  358.   mask = (1 << gpio%32);
  359.   value = *(gpio_map+offset) & mask;
  360.   besRETURN_LONG(value);
  361. besEND
  362.  
  363.  
  364. besFUNCTION(sb_cleanup)
  365.   munmap((void *)gpio_map, BLOCK_SIZE);
  366. besEND
  367.  


12
ScriptBasic / RPi GPIO Support
« on: April 25, 2019, 10:35:00 AM »
I have the GPIO extension module built with limited testing. I'm assuming the gpio argument is a 0 or 1 indicating the physical hardware or an emuator. It would be sweet if I can access the Python based Sense HAT emulator.

* Unzip to a tmp directory.
* Copy the gpio.so file to your /usr/local/lib/scriba/ directory.
* Copy the gpio.bas file to your /usr/local/include/scriba/ directory.

Simple Test

Code: Script BASIC
  1. ' Test GPIO
  2.  
  3. IMPORT gpio.bas
  4.  
  5. status = GPIO::Setup()
  6. PRINT status,"\n"
  7. GPIO::Cleanup
  8.  

pi@RPi3B:~/sbrpi/examples $ scriba testgpio.sb
0
pi@RPi3B:~/sbrpi/examples $

gpio.bas (extension module include file)
Code: Script BASIC
  1. 'GPIO Extension Mondule
  2.  
  3. GLOBAL CONST SETUP_OK           = 0
  4. GLOBAL CONST SETUP_DEVMEM_FAIL  = 1
  5. GLOBAL CONST SETUP_MALLOC_FAIL  = 2
  6. GLOBAL CONST SETUP_MMAP_FAIL    = 3
  7. GLOBAL CONST SETUP_CPUINFO_FAIL = 4
  8. GLOBAL CONST SETUP_NOT_RPI_FAIL = 5
  9.  
  10. ' Is really 0 for control register!
  11. GLOBAL CONST IN   = 1
  12. ' Is really 1 for control register!
  13. GLOBAL CONST OUT  = 0
  14. GLOBAL CONST ALT0 = 4
  15.  
  16. GLOBAL CONST HIGH    = 1
  17. GLOBAL CONST LOW     = 0
  18.  
  19. GLOBAL CONST PUD_OFF  = 0
  20. GLOBAL CONST PUD_DOWN = 1
  21. GLOBAL CONST PUD_UP   = 2
  22.  
  23.  
  24. MODULE GPIO
  25.  
  26. DECLARE SUB    ::Setup             ALIAS     "sb_setup"                 LIB "gpio"
  27. DECLARE SUB    ::ClearEventDetect  ALIAS     "sb_clear_event_detect"    LIB "gpio"
  28. DECLARE SUB    ::EventDetected     ALIAS     "sb_eventdetected"         LIB "gpio"
  29. DECLARE SUB    ::SetRisingEvent    ALIAS     "sb_set_rising_event"      LIB "gpio"
  30. DECLARE SUB    ::SetFallingEvent   ALIAS     "sb_set_falling_event"     LIB "gpio"
  31. DECLARE SUB    ::SetHighEvent      ALIAS     "sb_set_high_event"        LIB "gpio"
  32. DECLARE SUB    ::SetLowEvent       ALIAS     "sb_set_low_event"         LIB "gpio"
  33. DECLARE SUB    ::SetPullUpDn       ALIAS     "sb_set_pullupdn"          LIB "gpio"
  34. DECLARE SUB    ::SetupGPIO         ALIAS     "sb_setup_gpio"            LIB "gpio"
  35. DECLARE SUB    ::GPIOFunction      ALIAS     "sb_gpio_function"         LIB "gpio"
  36. DECLARE SUB    ::OutputGPIO        ALIAS     "sb_output_gpio"           LIB "gpio"
  37. DECLARE SUB    ::InputGPIO         ALIAS     "sb_input_gpio"            LIB "gpio"
  38. DECLARE SUB    ::Cleanup           ALIAS     "sb_cleanup"               LIB "gpio"
  39. DECLARE SUB    ::ShortWait         ALIAS     "sb_short_wait"            LIB "gpio"
  40.  
  41. END MODULE
  42.  

c_gpio.h (function declarations)
Code: C
  1. int setup(void);
  2. void setup_gpio(int gpio, int direction, int pud);
  3. int gpio_function(int gpio);
  4. void output_gpio(int gpio, int value);
  5. int input_gpio(int gpio);
  6. void set_rising_event(int gpio, int enable);
  7. void set_falling_event(int gpio, int enable);
  8. void set_high_event(int gpio, int enable);
  9. void set_low_event(int gpio, int enable);
  10. int eventdetected(int gpio);
  11. void cleanup(void)
  12. void short_wait(void) // wait 150 cycles
  13.  

raspberry-gpio-python (wrapped C interface as an ext. module))

BCM2835-ARM-Peripherals.pdf

Note: This was compiled on my RPi 3B. I will compile a Zero version after this is tested.

I have a Raspberry Pi Sense HAT board and a KooKye Smart Home IoT Sensor Kit.

Looking forward to your test scripts!

13
C BASIC / Hello C BASIC
« on: April 19, 2019, 07:30:38 PM »
Code: C
  1. #include <stdio.h>
  2. #include "cbasic.h"
  3.  
  4. MAIN
  5. BEGIN_FUNCTION
  6.   PRINT("Hello, World!\n");
  7. END_FUNCTION
  8.  


$ gcc hello.c -o hello
$ ./hello
Hello, World!
$


14
ScriptBasic / ScriptBasic Keyword List
« on: April 18, 2019, 02:42:02 AM »
This is the ScriptBasic keyword list.

ABS
ACOS
ACOSECANT
ACTAN
ADDDAY
ADDHOUR
ADDMINUTE
ADDMONTH
ADDRESS
ADDSECOND
ADDWEEK
ADDYEAR
ALIAS
AND
AS
ASC
ASECANT
ASIN
ATAN
ATN
BIN
BINMODE
BY
BYVAL
CALL
CHDIR
CHOMP
CHR
CHR$
CINT
CLOSE
CLOSEALL
COMMAND
CONF
CONST
COS
COSECANT
COTAN
COTAN2
CRYPT
CURDIR
CVD
CVI
CVL
CVS
DAY
DECLARE
DELETE
DELTREE
DIRECTORY
DO
ELIF
ELSE
ELSEIF
ELSIF
END
ENDIF
ENVIRON
ENVIRON$
EOD
EOF
ERROR
ERROR$
EVEN
EXECUTE
EXIT
EXP
FALSE
FILE
FILEACCESSTIME
FILECOPY
FILECREATETIME
FILEEXISTS
FILELEN
FILEMODIFYTIME
FILEOWNER
FIX
FOR
FORK
FORMAT
FORMATDATE
FORMATTIME
FRAC
FREEFILE
FROM
FUNCTION
GCD
GLOBAL
GMTIME
GMTIMETOLOCALTIME
GO
GOSUB
GOTO
HCOS
HCOSECANT
HCTAN
HEX
HEX$
HOSTNAME
HOUR
HSECANT
HSIN
HTAN
ICALL
IF
IMAX
IMIN
INPUT
INSTR
INSTRREV
INT
ISARRAY
ISDEFINED
ISDIRECTORY
ISEMPTY
ISFILE
ISINTEGER
ISNUMERIC
ISREAL
ISSTRING
ISUNDEF
JOIN
JOKER
KILL
LBOUND
LCASE
LCASE$
LCM
LEFT
LEFT$
LEN
LET
LIB
LIKE
LINE
LOC
LOCAL
LOCALTIMETOGMTIME
LOCK
LOF
LOG
LOG10
LOOP
LOWER
LOWER$
LTRIM
LTRIM$
MAX
MAXINT
MID
MID$
MIN
MININT
MINUTE
MKD
MKD$
MKDIR
MKI
MKI$
MKL
MKL$
MKS
MKS$
MODULE
MONTH
NAME
NEXT
NEXTFILE
NO
NOT
NOW
NULL
OCT
OCT$
ODD
ON
OPEN
OPTION
OR
OUTPUT
PACK
PATTERN
PAUSE
PI
POP
POW
PRINT
PRINTNL
QUOTE
RANDOMIZE
REF
REGION
REPEAT
REPLACE
RESET
RESUME
RETURN
REWIND
RIGHT
RIGHT$
RND
ROUND
RTRIM
RTRIM$
SEC
SECANT
SEEK
SET
SGN
SIN
SLEEP
SPACE
SPACE$
SPLIT
SPLITA
SPLITAQ
SQR
STEP
STOP
STR
STR$
STRING
STRING$
STRREVERSE
STRREVERSE$
SUB
SWAP
SYSTEM
TAN
TAN2
TEXTMODE
THEN
TIME
TIMEVALUE
TO
TRIM
TRIM$
TRUE
TRUNCATE
TYPE
UBOUND
UCASE
UCASE$
UNDEF
UNPACK
UNTIL
UPPER
UPPER$
VAL
VAR
WAITPID
WEEKDAY
WEND
WHILE
WILD
XOR
YEAR
YEARDAY

Not Implemented
  • BIN - This is a planned function to convert the argument number to binary format. (aka. format as a binary number containing only 0 and 1 characters and return this string)
  • GCD - Mathematical function has become a reserved word, but are not implemented.
  • LCM - Mathematical function has become a reserved word, but are not implemented.
  • ATN - This is a planned function to calculate the arcus tangent of the argument.
  • ATAN - This is a planned function to calculate the arcus tangent of the argument.
  • TAN - This is a planned function to calculate the tangent of the argument.
  • TAN2 - This is a planned function to calculate the tangent of the ratio of the two arguments.
  • COTAN - This is a planned function to calculate the cotangent of the argument.
  • COTAN2 - This is a planned function to calculate the cotangent of the ratio of the two arguments.
  • ACTAN - This is a planned function to calculate the arcus cotangent of the argument.
  • SECANT - This is a planned function to calculate the secant of the argument.
  • COSECANT - This is a planned function to calculate the cosecant of the argument.
  • ASECANT - This is a planned function to calculate the arcus secant of the argument.
  • ACOSECANT - This is a planned function to calculate the arcus cosecant of the argument.
  • HSIN - This is a planned function to calculate the sinus hyperbolicus of the argument.
  • HCOS - This is a planned function to calculate the cosinus hyperbolicus of the argument.
  • HTAN - This is a planned function to calculate the tangent hyperbolicus of the argument.
  • HCTAN - This is a planned function to calculate the cotangent hyperbolicus of the argument.
  • HSECANT - This is a planned function to calculate the secant hyperbolicus of the argument.
  • HCOSECANT - This is a planned function to calculate the cosecant hyperbolicus of the argument.
  • MAX - This is a planned function to select and return the maximum of the arguments.
  • MIN - This is a planned function to select and return the minimum of the arguments.
  • IMAX - This is a planned function to select and return the index of the maximum of the arguments.
  • IMIN - This is a planned function to select and return the index of the minimum of the arguments.
  • CVD - This is a planned function to convert the argument string into a real number. (8 byte)
  • CVI - This is a planned function to convert the argument string into an integer. (2 bytes)
  • CVL - This is a planned function to convert the argument string into an long integer. (4 bytes)
  • CVS - This is a planned function to convert the argument string into an integer. (4 byte)
  • MKD - This is a planned function to convert the argument real number to an 8 byte string. (8 byte)
  • MKI - This is a planned function to convert the argument integer number to a string. (2 byte)
  • MKS - This is a planned function to converts a single-precision number "n" into a  string so it can later be retrieved from a random-access file as a numeric value. (4 byte)
  • MKL - This is a planned function converts a long-integer number "n" into a string so it can later be retrieved from a random-access file as a numeric value. (4-byte)

Valid ScriptBasic Operators

Power operator (^)
Multiplication operator (*)
Division operator (/)
Integer division operator (\)
Modulus operator (%)
Addition and subtraction operators (+, -)
Bit-wise and logical NOT (NOT)
Equality operator (=)
Not equal operator (<>)
Compare operators (<, <=, >, >=)
Logical operators (and, or, xor)
Concatenation operator (&)
ByVal operator
LIKE operator



 Operators that are undefined by default but can be used by external modules are:


?     !     #     `     @
+^    +<    +>    +?    +=    +*    +/    +%    +!    +#    +&    +\    +`    +'   
+@    -^    -<    ->    -?    -=    -*    -/    -%    -!    -#    -&    -\    -`   
-'    -@    ^^    ^<    ^>    ^?    ^=    ^*    ^/    ^%    ^!    ^#    ^&    ^\   
^`    ^'    ^@    <^    <<    <?    <*    </    <%    <!    <#    <&    <\    <`   
<'    <@    >^    ><    >>    >?    >*    >/    >%    >!    >#    >&    >\    >`   
>'    >@    ?^    ?<    ?>    ??    ?=    ?*    ?/    ?%    ?!    ?#    ?&    ?\   
?`    ?'    ?@    =^    =<    =>    =?    ==    =*    =/    =%    =!    =#    =&   
=\    =`    ='    =@    *^    *<    *>    *?    *=    **    */    *%    *!    *#   
*&    *\    *`    *'    *@    /^    /<    />    /?    /=    /*    //    /%    /!   
/#    /&    /\    /`    /'    /@    %^    %<    %>    %?    %=    %*    %/    %%   
%!    %#    %&    %\    %`    %'    %@    !^    !<    !>    !?    !=    !*    !/   
!%    !!    !#    !&    !\    !`    !'    !@    #^    #<    #>    #?    #=    #*   
#/    #%    #!    ##    #&    #\    #`    #'    #@    &^    &<    &>    &?    &=   
&*    &/    &%    &!    &#    &&    &\    &`    &'    &@    \^    \<    \>    \?   


\=    \*    \/    \%    \!    \#    \&    \\    \`    \'    \@    `^    `<    `>   
`?    `=    `*    `/    `%    `!    `#    `&    `\    ``    `'    `@    '^    '<   
'>    '?    '=    '*    '/    '%    '!    '#    '&    '\    '`    ''    '@    @^   
@<    @>    @?    @=    @*    @/    @%    @!    @#    @&    @\    @`    @'    @@


15
BBC BASIC / Re: BBC BASIC Compiler?
« on: April 18, 2019, 02:30:06 AM »
Thanks Richard for the clarification. That is a sweet feature to have in a BASIC. The best ScriptBasic can do is call C extension modules. On Windows, Charles Pegge (OxygenBasic author) wrote an extension module that provides a dynamic FFI definition and full use of the O2.JIT compiler to create virtual DLLs and code in ASM.

I was fortunate that the Brandy author isoated the BBC BASIC graphic primatives in separate SDL 1.2 module. It made making a generic share object usable in most languages that can call a shared object.

Pages: [1] 2