Author Topic: SB π  (Read 7550 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #225 on: April 30, 2019, 06:53:15 PM »
I may try wrapping this code for the SLED extension module.

https://github.com/hzeller/rpi-rgb-led-matrix/blob/master/include/led-matrix-c.h

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #226 on: April 30, 2019, 08:16:45 PM »
I installed the RTIMULibCal tool which is pretty cool.


pi@RPi3B:~/rpicalc/RTEllipsoidFit $ RTIMULibCal
RTIMULibCal - using RTIMULib.ini
Settings file RTIMULib.ini loaded
Using fusion algorithm RTQF
Using min/max compass calibration
Ellipsoid compass calibration not in use
Accel calibration not in use
LSM9DS1 init complete

Options are:

  m - calibrate magnetometer with min/max
  e - calibrate magnetometer with ellipsoid (do min/max first)
  a - calibrate accelerometers
  x - exit

Enter option: m

Magnetometer min/max calibration
--------------------------------
Waggle the IMU chip around, ensuring that all six axes
(+x, -x, +y, -y and +z, -z) go through their extrema.
When all extrema have been achieved, enter 's' to save, 'r' to reset
or 'x' to abort and discard the data.

Press any key to start...


Min x: 1000.00  min y: 1000.00  min z: 1000.00
Max x: -1000.00  max y: -1000.00  max z: -1000.00

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #227 on: May 03, 2019, 05:41:35 PM »
I was able to compile from the latest IUP source and Gtk3 a release for the Raspberry Pi.

RPi IUP Online Dictionary

Offline petelomax

  • BASIC Developer
  • Posts: 8
  • Author of Phix
    • The Phix Programming Language
Re: SB π
« Reply #228 on: May 18, 2019, 01:59:47 PM »
sorry wrong thread
« Last Edit: May 18, 2019, 02:12:31 PM by petelomax »

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #229 on: June 03, 2019, 07:03:24 PM »
I built a WiringPi GPIO extension module. I will push the WPI extension module to the sandbox as soon as I'm done adding a few optional WiringPi extensions.

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #230 on: June 07, 2019, 10:23:09 PM »
AIR,

I could use your help with a function I want to add to the WiringPi (WPI) extension module. SB is too slow to poll the status of the GPIO pin. My C version of the DHT11 interface works fine. I would like a function you pass the pin number and the number of bits (length of string containing the pins 1/0 output). The pinmode() function returns a INT 0 or 1. I just need a string returned of the sample period.

The WPI extension module source and binary is on the RaspberryBASIC.org forum.

This a Python version that is able to sample the data fast enough to return a result.

RPi Forum Post

Thanks!
« Last Edit: June 07, 2019, 10:40:20 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #231 on: June 08, 2019, 10:08:25 AM »
Is the fastest way to do this is create a C string array and populate it with a charter 1 or a 0 based on the result of the pin read? This way there is no conversation of INT to CHAR but a IF / THEN choice. Then it's a simple assignment of the string array to besRETURN_STRING().

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #232 on: June 08, 2019, 11:17:13 AM »
I got it working.

RPi Post

It would be great if I  could pass a buffer size and delay argument and the function dynamically creates the proper size array. I'm assuming I don't have to free the buf array as it's freed when the function returns.

Code: C
  1. besFUNCTION(sb_BitStreamRead)
  2.   char buf[5000];
  3.   int pin, status, idx;
  4.   besARGUMENTS("i")
  5.     &pin
  6.   besARGEND
  7.   for (idx = 0; idx < 5000; idx++){
  8.     status = digitalRead(pin);
  9.     delayMicroseconds(1);
  10.     if (status == 0){
  11.       buf[idx] = 0x30;
  12.     }else{
  13.       buf[idx] = 0x31;
  14.     }
  15.   }
  16.   besRETURN_STRING(buf);
  17. besEND
  18.  
« Last Edit: June 08, 2019, 11:38:22 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #233 on: June 08, 2019, 05:28:48 PM »
AIR,

You're a Python pro and I'm trying to convert the following to SB. What am I doing wrong?

Code: Python
  1. # .-------------------------------------------------------------------------.
  2. # |  Extract the pulse lengths                                              |
  3. # `-------------------------------------------------------------------------'
  4.  
  5. pulseSize = 0
  6. last = 0
  7. for index in range(SAMPLE_SIZE):
  8.   if samples[index] == 1:
  9.     if last == 0:
  10.       pulseSize += 1
  11.     if pulseSize <= 40:
  12.       pulses[pulseSize-1] += 1
  13.   last = samples[index]
  14.  
  15. # .-------------------------------------------------------------------------.
  16. # |  Determine min and max pulse sizes, and mid-way between                 |
  17. # `-------------------------------------------------------------------------'
  18.  
  19. min = pulses[0]
  20. max = pulses[0]
  21.  
  22. for index in range(40):
  23.   if pulses[index] < min : min = pulses[index]
  24.   if pulses[index] > max : max = pulses[index]
  25.  
  26. mid = ((max - min) // 2) + min
  27.  
  28. # .-------------------------------------------------------------------------.
  29. # |  Determine bit values                                                   |
  30. # `-------------------------------------------------------------------------'
  31.  
  32. for index in range(40):
  33.   if pulses[index] > mid : bits[index] = 1
  34.   else                   : bits[index] = 0
  35.  
  36. # .-------------------------------------------------------------------------.
  37. # |  Form the bytes                                                         |
  38. # `-------------------------------------------------------------------------'
  39.  
  40. for index in range(5):
  41.   byte[index] = 0
  42.  
  43. for index in range( 0, 7) : byte[0] += bits[index] << (7-(index & 7))
  44. for index in range( 8,15) : byte[1] += bits[index] << (7-(index & 7))
  45. for index in range(16,23) : byte[2] += bits[index] << (7-(index & 7))
  46. for index in range(24,31) : byte[3] += bits[index] << (7-(index & 7))
  47. for index in range(32,39) : byte[4] += bits[index] << (7-(index & 7))
  48.  
  49. # .-------------------------------------------------------------------------.
  50. # |  Print results                                                          |
  51. # `-------------------------------------------------------------------------'
  52.  
  53. if ((byte[0] + byte[1] + byte[2] + byte[3]) & 0xFF) == byte[4]:
  54.   print "Checksum matches"
  55. else:
  56.   print "Checksum does not match"
  57.  
  58. print Hex(byte[0])
  59. print Hex(byte[1])
  60. print Hex(byte[2])
  61. print Hex(byte[3])
  62. print Hex(byte[4])
  63.  

Here is my ScriptBasic attempt.

Code: Script BASIC
  1. IMPORT wpi.bas
  2.  
  3. WPI::wiringPiSetup()
  4. WPI::piHiPri(99)
  5.  
  6. valid = 0
  7. WHILE valid = 0
  8.   laststate = 1
  9.   bits = ""
  10.  
  11.   WPI::pinMode(7, 1)
  12.   WPI::digitalWrite(7, 0)
  13.   WPI::delay(18)
  14.   WPI::digitalWrite(7, 1)
  15.   WPI::delayMicroseconds(38)
  16.   WPI::pinMode(7, 0)
  17.   bits = WPI::BitStreamRead(7)
  18.  
  19.   splita bits by "" to samples
  20.  
  21.  
  22. ' --------------------------
  23. ' Extract the pulse lengths                                              |
  24. ' --------------------------
  25.  
  26.   pulseSize = 0
  27.   last = 0
  28.   for index = 1 to ubound(samples)
  29.     if samples[index] = 1 then
  30.       if last = 0 then
  31.         pulseSize += 1
  32.       end if
  33.       if pulseSize <= 40 then
  34.         pulses[pulseSize-1] += 1
  35.       end if
  36.     end if
  37.     last = samples[index]
  38.   next
  39.  
  40.  
  41. ' -------------------------------------------------------
  42. ' Determine minimum and maximum pulse sizes, and mid-way between
  43. ' -------------------------------------------------------
  44.  
  45. minimum = pulses[0]
  46. maximum = pulses[0]
  47.  
  48. for index = 0 to 40
  49.   if pulses[index] < minimum then minimum = pulses[index]
  50.   if pulses[index] > maximum then maximum = pulses[index]
  51. next
  52. middle = ((maximum - minimum) \ 2) + minimum
  53.  
  54.  
  55. ' ---------------------
  56. ' Determine outbit values
  57. ' ---------------------
  58.  
  59. for index = 0 to 40
  60.   if pulses[index] > middle then
  61.     outbits[index] = 1
  62.   else
  63.     outbits[index] = 0
  64.   end if
  65. next
  66.  
  67. ' for idx = 0 to ubound(outbits)
  68. '   print outbits[idx]
  69. ' next
  70. ' print
  71.  
  72.  
  73. ' ---------------
  74. ' Form the bytes
  75. ' ---------------
  76.  
  77. for index = 0 to 5
  78.   byte[index] = 0
  79. next
  80. for index = 0 to 7
  81.   byte[0] += bits[index] * (7-(index % 7))
  82. next
  83. for index = 8 to 15
  84.   byte[1] += bits[index] * (7-(index % 7))
  85. next
  86. for index = 16 to 23
  87.   byte[2] += bits[index] * (7-(index % 7))
  88. next
  89. for index = 24 to 31
  90.   byte[3] += bits[index] * (7-(index % 7))
  91. next
  92. for index = 32 to 39
  93.   byte[4] += bits[index] * (7-(index % 7))
  94. next
  95.  
  96.  
  97. ' --------------
  98. ' Print results
  99. ' --------------
  100.  
  101. if ((byte[0] + byte[1] + byte[2] + byte[3]) % 0xFF) = byte[4] then
  102.   print "Checksum matches\n"
  103. else
  104.   print "Checksum does not match\n"
  105. end if
  106. print Hex(byte[0]),"\n"
  107. print Hex(byte[1]),"\n"
  108. print Hex(byte[2]),"\n"
  109. print Hex(byte[3]),"\n"
  110. print Hex(byte[4]),"\n"
  111.  
  112.  
  113. ' PRINT bits,"<*>\n"
  114.  WPI::delay(1500)
  115. WEND
  116. WPI::piHiPri(0)  
  117.  

This is what the data looks like before trying to convert it to readable values. (remarked out routine in above code)


pi@RPi3B:~/sbrpi/examples $ scriba allbits.sb
00110101000000000001011000000011010011100
10111011000000000001111100000011011011100
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000
10011011000000000000101100000010001010000

« Last Edit: June 08, 2019, 05:32:35 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 684
Re: SB π
« Reply #234 on: June 08, 2019, 05:51:36 PM »
The bit shift left operator (<<) is not the same as simply multiplying.

Consider (this part is in a python prompt):

>>> x = 2
>>> y = 5
>>> x*y
10
>>> x << y
64

So in this example, what you're actually doing is x * (x to the power of y)

In SB, the above would look like:

x = 2
y = 5
print x * y,"\n"
print x * x^y,"\n"

riveraa@dpi:~/sb $ ./AppRun math.sb
10
64

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #235 on: June 08, 2019, 06:09:42 PM »
I made the changes I think you were suggesting but no luck getting results. I uncommented the printing of the raw bits.

Code: Script BASIC
  1. ' ---------------
  2. ' Form the bytes
  3. ' ---------------
  4.  
  5. for index = 0 to 5
  6.   byte[index] = 0
  7. next
  8. for index = 0 to 7
  9.   byte[0] += bits[index] * bits[index] ^ (7-(index % 7))
  10. next
  11. for index = 8 to 15
  12.   byte[1] += bits[index] * bits[index] ^ (7-(index % 7))
  13. next
  14. for index = 16 to 23
  15.   byte[2] += bits[index] * bits[index] ^ (7-(index % 7))
  16. next
  17. for index = 24 to 31
  18.   byte[3] += bits[index] * bits[index] ^ (7-(index % 7))
  19. next
  20. for index = 32 to 39
  21.   byte[4] += bits[index] * bits[index] ^ (7-(index % 7))
  22. next
  23.  


pi@RPi3B:~/sbrpi/examples $ scriba allbits.sb
00011010100000000000101100000011010100010
Checksum does not match
7FFFFFFF
0
0
0
0
10011010100000000000101100000011000000000
Checksum does not match
7FFFFFFF
0
0
0
0
10011010100000000000101100000011001010000
Checksum does not match
7FFFFFFF
0
0
0
0
10011010100000000000101100000011001000000
Checksum does not match
7FFFFFFF
0
0
0
0
10011010100000000000101100000011001100010
Checksum does not match
7FFFFFFF
0
0
0
0
^C
pi@RPi3B:~/sbrpi/examples $


Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #236 on: June 08, 2019, 06:16:53 PM »
I screwed up. It was suppose to be outbits not bits which I already used. We are getting numbers but they don't seem right and the checksum fails.


pi@RPi3B:~/sbrpi/examples $ scriba allbits.sb
10011010100000000000101100000011101010010
Checksum does not match
4
1
3
2
4
10010000000000000000001000000000000000010
Checksum does not match
2
0
1
0
1
10011010100000000000101100000000001010010
Checksum does not match
4
1
3
0
3
10011010100000000000101100000000001010010
Checksum does not match
4
1
3
0
3
10011010100000000000101100000000001010010
Checksum does not match
4
1
3
0
3
^C
pi@RPi3B:~/sbrpi/examples $


Code: Script BASIC
  1. ' ---------------
  2. ' Form the bytes
  3. ' ---------------
  4.  
  5. for index = 0 to 5
  6.   byte[index] = 0
  7. next
  8. for index = 0 to 7
  9.   byte[0] += outbits[index] * outbits[index] ^ (7-(index % 7))
  10. next
  11. for index = 8 to 15
  12.   byte[1] += outbits[index] * outbits[index] ^ (7-(index % 7))
  13. next
  14. for index = 16 to 23
  15.   byte[2] += outbits[index] * outbits[index] ^ (7-(index % 7))
  16. next
  17. for index = 24 to 31
  18.   byte[3] += outbits[index] * outbits[index] ^ (7-(index % 7))
  19. next
  20. for index = 32 to 39
  21.   byte[4] += outbits[index] * outbits[index] ^ (7-(index % 7))
  22. next
  23.  

This what I get if I run my DHT11 function in the WIP extension module.


pi@RPi3B:~/sbrpi/examples $ scriba dht11.sb
[2] 53.0 %  22.9 C
[1] 53.0 %  22.9 C
[1] 53.0 %  22.9 C
[2] 53.0 %  23.0 C
[1] 53.0 %  22.9 C
[3] 53.0 %  22.9 C
[1] 53.0 %  23.0 C
[3] 53.0 %  23.0 C
[1] 53.0 %  23.0 C
[3] 53.0 %  23.0 C
pi@RPi3B:~/sbrpi/examples $


« Last Edit: June 08, 2019, 06:21:27 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #237 on: June 08, 2019, 07:54:00 PM »
It looks like the data starts on the second byte.


1 00110110 00000000 00010111 00000010 00001100
     54        0       23       2        12

« Last Edit: June 08, 2019, 09:13:11 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #238 on: June 09, 2019, 09:48:00 AM »
Hi AIR,

I have given up on trying to convert this Python bit to byte routine. I may go back to the C method or write my own parsing routine.

I could use your help with a safe method of creating a dynamic character array in a SB extension module.

Offline John

  • Forum Support / SB Dev
  • Posts: 2745
    • ScriptBasic Open Source Project
Re: SB π
« Reply #239 on: June 09, 2019, 03:49:01 PM »
I was able to get my native DHT11 example going. I created a C function to convert a string of bits and return an integer. The last thing still on my plate with the WPI extension module is creating a variable sized buffer based on a argument the user passes. Any help you can provide with that would be appreciated.
« Last Edit: June 09, 2019, 03:58:43 PM by John »