Raspberry BASIC

Recent Posts

Pages: [1] 2 3 4
1
Liberty BASIC / Liberty BASIC 5 update - new alpha build 350
« Last post by CarlGundel on August 10, 2019, 09:14:18 PM »
For those interested in trying out Liberty BASIC v5.0 which includes support for the Raspberry Pi please visit this page for more information.

  http://libertybasiccom.proboards.com/board/12/liberty-basic-v5

A summary of what's new in this build is below.

Thank you.

-Carl Gundel, author of Liberty BASIC

Build 350 Notes
==============================================================================

For Windows use the lb5alpha.exe file.
For Linux 32-bit use the lin32-350 file: ./lin32-350 lb5alpha.im
For Raspberry Pi use the rpi-alpha file: ./rpi-alpha lb5alpha.im
For MacOS double click on the lb5-350 app. You may need to press and hold
right click to popup a menu and use Ctrl+Open to overcome permissions.

- Added the SpecialKey$ global variable for capturing special keys in the
graphics window. If keys such as Page Up, F1, Home, etc. are pressed when
the "when characterInput" feature is specified, the value of the keypress
will be assigned to SpecialKey$ in the form of "PAGEUP", "F1", "HOME", etc.

- Fixed the setfocus feature of the graphicbox and graphics windows. It wasn't
doing anything for #handle "setfocus" and it wasn't implemented at all for
#handle setfocus().

- Implemented upto$() function from LB4.

- Implemented replstr$() function from LB4.

- Implemented remchar$() function from LB4.

- Implemented endswith() function from LB4.

- Implemented after$() function from LB4.

- Implemented afterlast$() function from LB4.

- Added HTTPS support for httpget$() function.

- Fixed #graphics set(x, y) to use the pen size to draw larger pixels.

- Enabled the BASIC Source Code Path option in the preferences dialog.

- Optimized some graphics drawing to make things a little faster.

- Fixed #text selectall(). Note that the window must be active for the
selection to be visible. This will be fixed later.

- Fixed the not understood error using LOF() on Linux, MacOS, and Raspian.

- Tweaked the BASIC code editor font to make it slightly smaller. Later the
font will be user specifiable.

- Fixed the Tab key handling in the BASIC code editor that it will insert
spaces when the tab key is used to make it work like earlier versions of
Liberty BASIC.

- Added support for ODBC database connectivity. See example callerODBC.bas.

- Added LPRINT and DUMP commands.

- Renamed the RUN command to LIBRARY and made a new RUN command that works
similar to the RUN from Liberty BASIC v4.x, but RUN doesn't yet run .BAS
programs. You can use LIBRARY for that.

- Added a #handle "print" command for graphics printing. Only partly functional
at this time.

- The confirm statement was not properly returning 0 or 1 and "no" and "yes"
depending on whether a numeric or string variable was used for the return value.

- BASIC Source Code Path is now supported in the Liberty BASIC preferences.

- Reactivated the Main window columns: rows: fields in the Liberty BASIC
preferences.
2
BBC BASIC / BBC BASIC for SDL 2.0 version 1.05a released
« Last post by Richard Russell on July 25, 2019, 02:54:02 PM »
I have released version 1.05a of BBC BASIC for SDL 2.0, the cross-platform programming language for Windows, MacOS, Linux, Raspbian (Raspberry Pi), Android and iOS.  The changes in this version are as follows:

  • BASIC Interpreter / Run Time Engine

        Fixed a bug causing MOUSE and graphics coordinates to diverge if the window is resized without doing a VDU 26.

        Incorporated Jonathan Harston's modification to ignore SOUND statements specifying non-internal sound channels.

  • IDEs and Utilities

        The SDLIDE list of functions and procedures can now be scrolled using the PgUp and PgDn keys.

        SDLIDE will now load programs in plain text (.bas) format even if they contain TAB characters.

        Added a Go To Line menu selection to SDLIDE, similar to that in BBC BASIC for Windows v6.12a.

        Modified the search utility to fix a problem with it not having keyboard focus initially.

  • Libraries

        dlglib.bbc now supports comboboxes!

        Fixed a bug in datelib.bbc causing the FN_date$() function to misbehave with months starting with an M (March and May).  D'oh!

        The DejaVuSans font has been updated to a newer version with more Unicode symbols.

        Added the (incomplete) bigint.bbc library in case somebody fancies developing it further.

  • Example Programs

        Added a graphics demo fluid.bbc which uses GPU shader code to create a plasma-like effect.

        Modified dlgdemo.bbc to demonstrate the new combobox capability of the dialogue box library.

        Modified hangman.bbc to remove code which worked around the now fixed coordinate disparity.

        Added a 3D animation to accompany skaters.bbc.  This has a rather risqu Easter Egg, can you find it?!
This new version may be downloaded, for all the supported platforms, from the usual location.  The GitHub repository has been updated (used to build the MacOS, Raspbian, iOS and 64-bit Linux editions, currently).
3
BBC BASIC / BBC BASIC for SDL 2.0 version 1.04a released
« Last post by Richard Russell on June 24, 2019, 04:19:25 PM »
I have released version 1.04a of BBC BASIC for SDL 2.0, the cross-platform programming language for Windows, MacOS, Linux, Raspbian, Android and iOS.  The changes in this version are as follows:
  • BASIC Interpreter / Run Time Engine

    The VDU 23,24,n| command updates the 'character spacing adjustment' when a proportional-spaced font is used (negative values cause the characters to close up, positive values cause them to spread apart).

    The @tmp$ system variable has been changed on Linux (including Raspbian) and MacOS so that it points to a user-specific directory.  Previously, problems could arise if BBC BASIC was run as 'root' (causing files with root ownership to be stored in @tmp$) and then subsequently as a user without privileges to delete them.  On other platforms @tmp$ has always been user-specific.
  • Example Programs

    hangman.bbc: David Williams' nice hangman program, ported to BBCSDL whilst preserving its original look-and-feel as closely as possible.

    figleaf.bbc: A rendition of Scott Joplin's 'Fig Leaf Rag' (transcribed by Ron Stickley for my Z80 Music program in 1984) accompanied by an animated 3D piano keyboard.
This new version may be downloaded, for all the supported platforms, from the usual location.  The GitHub repository has been updated (used to build the MacOS, Raspbian, iOS and 64-bit Linux editions, currently) although doubts have been expressed over whether GitHub is the right solution going forwards.

Please remember that if you use the Android Application Generator you should download a new APK template to ensure that any updates to the run-time engine are incorporated in your own apps.
4
ScriptBasic / Re: WPI - WiringPi and Sensor Interface Extension Module
« Last post by John Spikowski on June 11, 2019, 07:44:46 AM »
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.
5
ScriptBasic / WPI - WiringPi and Sensor Interface Extension Module
« Last post by John Spikowski 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.  
6
BBC BASIC / Re: Interfacing with a DHT11/12/22 sensor
« Last post by John Spikowski on June 07, 2019, 07:27:49 AM »
 I fixed the Smiley problem.  8)
7
BBC BASIC / Re: Interfacing with a DHT11/12/22 sensor
« Last post by Richard Russell on June 04, 2019, 08:18:41 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.

That's a pity.  I find manual selection difficult (on this laptop PC, at least), particularly if it needs to scroll; maybe if I had a proper mouse with a scrollwheel it would be easier.  It seems odd that one useful feature would be removed at the same time as adding another.  [I'd add a frown but emoticons are seemingly broken on this forum!]
8
BBC BASIC / Re: Interfacing with a DHT11/12/22 sensor
« Last post by John Spikowski 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.
9
BBC BASIC / Re: Interfacing with a DHT11/12/22 sensor
« Last post by Richard Russell on June 03, 2019, 08:16:33 PM »
The syntax highlighting is great, thank you, but the 'Select All' option seems to have gone! It was there before, wasn't it, or did I imagine it?

10
BBC BASIC / Interfacing with a DHT11/12/22 sensor
« Last post by Richard Russell on June 03, 2019, 04:21:14 PM »
The BBC BASIC program below interfaces with a DHT11/12 or DHT22 humidity and temperature sensor connected to a Raspberry Pi GPIO pin.  These sensors use a proprietary serial data format on a one-wire bidirectional bus, which has to be decoded in software.

Because of the variable latency associated with a user-mode program reading the GPIO, the data cannot always be decoded reliably.  The code uses a number of strategies to minimize this problem but mis-reads must be expected and dealt with (e.g. by means of retries) in any application using it.  Note that the DHT11/12 may be polled no faster than once per second and the DHT22 no faster than once every two seconds.

The code has been written to be functional rather than pretty!

Code: BBC BASIC
  1.       REM DHT11/12/22 sensor interface for Raspberry Pi 3/3+ (Raspbian)
  2.       REM (c) Richard Russell, http://www.rtrussell.co.uk/, 03-Jun-2019
  3.  
  4.       INSTALL @lib$ + "gpiolib"
  5.       GPIO% = FN_gpio_setup
  6.       PIN% = 2 : REM BCM pin no.
  7.  
  8.       @% = &2010A
  9.       REPEAT
  10.         CASE FN_dht_read(GPIO%, PIN%, humidity, temperature) OF
  11.           WHEN 0: PRINT TIME$ "  Humidity = "; humidity "%, temperature = "; temperature "C"
  12.           WHEN 1: PRINT TIME$ "  DHT11/12/22 read failed: wrong number of transitions"
  13.           WHEN 2: PRINT TIME$ "  DHT11/12/22 read failed: checksum error"
  14.           WHEN 3: PRINT TIME$ "  DHT11/12/22 read failed: no response"
  15.         ENDCASE
  16.         WAIT 200
  17.       UNTIL FALSE
  18.       END
  19.  
  20.       REM Read from DHT11/12/22, G% is value returned from FN_gpio_setup, P% is BCM pin number:
  21.       DEF FN_dht_read(G%, P%, RETURN humidity, RETURN temperature)
  22.       LOCAL B%, C%, I%, J%, M%, N%, O%, T%, d&(), t%() : N% = 6000 : REM Adjust for CPU speed
  23.       DIM d&(4), t%(90), C% LOCAL N%*4 + 3
  24.       B% = G% + &34 : M% = 1 << P%
  25.  
  26.       REM Initialise the GPIO pin to be used, happens only on first call:
  27.       PRIVATE initialised
  28.       IF NOT initialised THEN
  29.         PROC_gpio_inp(G%, P%) : PROC_gpio_pull(G%, 2) : REM pull-up
  30.         PROC_gpio_pullclk0(G%, 1 << P%) : PROC_gpio_pullclk0(G%, 0)
  31.         initialised = TRUE
  32.         WAIT 10
  33.       ENDIF
  34.  
  35.       REM Send the start signal to the DHT11/12/22 and sample the data stream it responds with:
  36.       PROC_gpio_out(G%,P%) : G%!&28 = M% : REM Set data line to output and low
  37.       WAIT 2                             : REM Wait 20 ms
  38.       G%!&1C = M% : PROC_gpio_inp(G%,P%) : REM Set data line high and to input
  39.       FOR I% = C% TO C% + N%*4 STEP 4 !I%=!B% : NEXT : REM Sample at ~1 us intervals
  40.  
  41.       REM Scan the captured data for transitions and store their positions in an array:
  42.       FOR I% = C% + 32 TO C% + N%*4 STEP 4
  43.         IF !I% AND M% EOR O% t%(J%) = I% : O% EOR= M% : J% += 1 : IF J% > DIM(t%(),1) EXIT FOR
  44.       NEXT
  45.       IF J% < 5 THEN = 3 : REM If fewer than 5 transitions report no response
  46.       IF O% = 0 ERROR 100, "Sampling period not long enough to capture entire data stream"
  47.  
  48.       REM Measure the total 'low' time (except for the initial pulse) to establish timebase:
  49.       FOR I% = 1 TO J%-2 STEP 2 : T% += t%(I%+1) - t%(I%) : NEXT : T% DIV= 41
  50.  
  51.       REM If too few transitions attempt to infer where the missing pulse(s) was/were:
  52.       IF J% = 79 PROC_dht_fix(t%(), J%)
  53.       IF J% = 81 PROC_dht_fix(t%(), J%)
  54.       IF J% <> 83 THEN = 1 : REM Report uncorrectable wrong number of transitions
  55.  
  56.       REM Decode data using only the high-to-low transitions:
  57.       FOR I% = 0 TO 39 : d&(I% DIV 8) = d&(I% DIV 8) * 2 - ((t%(I%*2+3) - t%(I%*2+1) > 2 * T%)) : NEXT
  58.       IF d&(4) <> (SUM(d&()) - d&(4) AND &FF) THEN
  59.         REM If checksum error attempt to decode using only the low-to-high transitions:
  60.         FOR I% = 0 TO 39 : d&(I% DIV 8) = d&(I% DIV 8) * 2 - ((t%(I%*2+4) - t%(I%*2+2) > 2 * T%)) : NEXT
  61.       ELSE IF d&(4) <> (SUM(d&()) - d&(4) AND &FF) THEN ;
  62.         REM If still a checksum error attempt to decode using widths of high pulses:
  63.         FOR I% = 0 TO 39 : d&(I% DIV 8) = d&(I% DIV 8) * 2 - ((t%(I%*2+3) - t%(I%*2+2) > T%)) : NEXT
  64.       ENDIF
  65.       IF d&(4) <> (SUM(d&()) - d&(4) AND &FF) THEN = 2 : REM Report uncorrectable checksum error
  66.  
  67.       REM Get the humidity and temperature; REM or unREM as required for DHT11/12 or DHT22:
  68.       humidity = d&(0) + d&(1) / 10 : temperature = d&(2) + d&(3) / 10 : REM DHT11/12
  69.       REM humidity = (d&(0) * 256 + d&(1)) / 10 : REM DHT22
  70.       REM temperature = ((d&(2) AND &7F) * 256 + d&(3)) / 10 : IF d&(2) >= &80 temperature *= -1 : REM DHT22
  71.       = 0
  72.  
  73.       REM If the number of transitions was wrong attempt to insert the 'missing' pulse:
  74.       DEF PROC_dht_fix(t%(), RETURN J%)
  75.       LOCAL I%, M%, S%, T%
  76.       FOR I% = 1 TO J%-2 STEP 2
  77.         T% = t%(I%+1) - t%(I%)
  78.         IF T% > M% M% = T% : S% = I%
  79.       NEXT
  80.       FOR I% = J%+1 TO S%+3 STEP -1
  81.         t%(I%) = t%(I%-2)
  82.       NEXT
  83.       t%(S%+1) = t%(S%) + M% * 2 / 5 + 0.5 : REM new rising edge
  84.       t%(S%+2) = t%(S%) + M% * 3 / 5 + 0.5 : REM new falling edge
  85.       J% += 2
  86.       ENDPROC
  87.  
Pages: [1] 2 3 4