User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sat Jun 08, 2019 2:06 pm

So the conclusion is, ScriptBasic should be able to do it natively.
That is great news @hippy!

I'm working on a new function for the WPI extension module to do a stream read. You pass the pin and the number of samples and it will return a string of the sample time.
Last edited by ScriptBasic on Sat Jun 08, 2019 4:17 pm, edited 1 time in total.

jalih
Posts: 64
Joined: Mon Apr 15, 2019 3:54 pm

Re: ScriptBasic

Sat Jun 08, 2019 3:04 pm

RichardRussell wrote:
Sat Jun 08, 2019 9:53 am
FOR loops are the fastest type in BBC BASIC (and probably other BASICs too) because there is less 'per loop' overhead than with a REPEAT or WHILE. Specifically, the termination condition is determined in the FOR statement and doesn't need to be 'interpreted' each time around the loop as it does with the others. To give you a feel for the speed (and remember that BBC BASIC is a 'classic' interpreter with no pre-processing shortcuts) this empty FOR loop:

Code: Select all

      FOR I% = 1 TO 1000000
      NEXT
takes just under 0.2 seconds on my RPi 3B (not +) which is 0.2μs per loop or 240 cycles at 1.2 GHz. That's obviously a lot compared with a compiled language (assuming it doesn't optimise the loop away entirely!) but sounds in the right ball park for a 'pure' interpreter compiled from C. I expect an interpreter coded in assembly language, like Sophie Wilson (CBE)'s ARM BBC BASIC, would be considerably faster.
I am thinking about joining the fun with 8th programming language. I think speed wise it should be able to manage it. I tested execution time of empty loop repeating 'no operation' word million times and by using loop construct to repeat million times. Loop construct was a lot slower. As 8th is a stack based programming language, loop index gets pushed onto stack and have to be dropped on every iteration. It's an interpreter, but words get compiled into native code. Test was run on ROCK64 board, as I have no RPI.

Code: Select all


: test1
  ' noop 1000000 times ;

: test2
  ' drop 1 1000000 loop ;


: app:main
  d:msec
  test1
  d:msec swap n:- . space "msec" . cr

  d:msec
  test2
  d:msec swap n:- . space "msec" . cr
  bye ;
  
Result is:

Code: Select all

14 msec
80 msec
ScriptBasic is using string to store sample bits and most people here are using array. I think I will directly use buffer to store bits. That way it's really easy to process and convert buffer data by using unpack.

User avatar
RichardRussell
Posts: 531
Joined: Thu Jun 21, 2012 10:48 am

Re: ScriptBasic

Sat Jun 08, 2019 3:34 pm

jalih wrote:
Sat Jun 08, 2019 3:04 pm
ScriptBasic is using string to store sample bits and most people here are using array. I think I will directly use buffer to store bits. That way it's really easy to process and convert buffer data by using unpack.
I'm using a buffer, allocated on the stack, to store the bits directly (24000 bytes, holding up to 6000 32-bit values each representing a snapshot of all the GPIO pins). It's a little faster than using an array in BBC BASIC.

It's amusing to think that one could interrogate many DHT11 sensors simultaneously that way, without the time-critical code taking any longer. Perhaps one Raspberry Pi monitoring a dozen different temperatures in a cool store!

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sat Jun 08, 2019 3:42 pm

The DS18B20 can be dasiychained on a single wire with each device having it's own unique address. The one wire interface seems pretty fast.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sat Jun 08, 2019 6:12 pm

This is an example of the new WPI extension module function BitStreamRead. I think this gives us more to work with.

allbits.sb

Code: Select all

IMPORT wpi.bas

WPI::wiringPiSetup()
WPI::piHiPri(99)

valid = 0
WHILE valid = 0
  bits = ""

  WPI::pinMode(7, 1)
  WPI::digitalWrite(7, 0)
  WPI::delay(18)
  WPI::digitalWrite(7, 1)
  WPI::delayMicroseconds(40)
  WPI::pinMode(7, 0)
  bits = WPI::BitStreamRead(7)
  PRINT bits,"<*>\n"
  WPI::delay(1500)
WEND
WPI::piHiPri(0)  
Output

Code: Select all

[email protected]:~/sbrpi/examples $ scriba allbits.sb
11111111111000000000000000000000000000000000000000000000000001111111111111111111111110000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000011111111111111111111111100000000000000000000000000000000000001111111111111110000000000000000000000000000000000000000000000000001111111111111111111111111000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000011111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000111111111111111111111111000000000000000000000000000000000000000000000000000011111111111111111111111100000000000000000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000000000000000111111111111111111111110000000000000000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111000000000000000000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000000000000000011111111111111111111100000000000000000000000000000000000000000000000000001111111111111111111111100000000000000000000000000000000000000000000000000011111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000111111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
00000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000000111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111110000000000000000000000000000000000000000000000000000000111111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111110000000000000000000000000000000000000000000000000000000111111111111111111111110000000000000000000000000000000000000000000000000000000111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111000000000000000000000000000000000000000000000000000000111111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111000000000000000000000000000000000000000000000000000000111111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111111100000000000000000000000000000000000000000000000000000011111111111111111111111110000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111111000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000001111111111111111111111111000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
^C
[email protected]:~/sbrpi/examples $ 
Last edited by ScriptBasic on Sat Jun 08, 2019 8:45 pm, edited 1 time in total.

hippy
Posts: 5363
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Sat Jun 08, 2019 7:06 pm

But is your output correct ? Until you can parse it, extract the data bits and bytes, match the checksum it's just zero and one digits. It doesn't pass my tests in my code but I haven't tried to determine why not. It could be right, maybe not, we have no way of knowing.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sat Jun 08, 2019 7:12 pm

I'm reading the pin every microsecond and return a 5000 byte result string. Is that sampling too fast?

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sat Jun 08, 2019 7:28 pm

This is sampling every 5 microseconds and returning a string of 1024 characters. In the C version of SB DHT11 it assume the initial data is garbage and ignores it.

Code: Select all

[email protected]:~/sbrpi/examples $ scriba allbits.sb
0000000000011110000000000011111111111111000000000001111111111111100000000000111110000000000111111111111110000000000011111000000000001111100000000000111100000000000111110000000000111110000000000011111000000000001111000000000001111100000000001111100000000000111110000000000111110000000000111110000000000011111000000000001111111111111100000000000111110000000000111111111111111000000000011111000000000001111111111111110000000000111110000000000011111000000000001111100000000000111100000000000111111111111110000000000011111000000000001111000000000001111100000000000111110000000000111111111111111000000000001111000000000001111111111111100000000000111100000000001111100000000000111100000000000111111111111110000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000111111111111111111000000000001111100000000000111100000000000111111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111000000000001111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111110000000000011111000000000001111111111111100110000000000011111111111111100000000001111100000000000111110000000000011111000000000001111111111111100000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000111111111111111111000000000001111100000000000111110000000000111111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000000111111111111110000000000011111000000000011111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111110000000000011111111111111000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000011111111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000111111000000000011111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111000000000001111100000000000111110000000000111111111111111000000000001111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000111111111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111000000000011111000000000001111100000000000000111110000000000011111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000000111111111111110000000000011111000000000001111000000000001111111111111110000000000011111000000000011111111111111100000000000111110000000000111111111111111000000000001111100000000001111100000000000111111111111111000000000011111000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000111111111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000111111000000000011111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111000000000001111100000000000111110000000000111111111111111000000000001111000000000001111100000000000111111111111111000000000001111000000000001111111111111110000000000011110000000000011111111111111100000000000111100000000000111110000000000011111111111111000000000001111100000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000111111111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111000000000001111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111111111111110000000000011111000000000011000000000001111100000000000111111111111110000000000011111000000000001111100000000000111111111111110000000000011111000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000011111111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111100000000000111111111111111000000000001111100000000000111111111111110000000000011111000000000011111111111111100000000000111110000000000111110000000000011111111111111100000000001111100000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
^C
[email protected]:~/sbrpi/examples $ 
Last edited by ScriptBasic on Sat Jun 08, 2019 8:05 pm, edited 1 time in total.

jalih
Posts: 64
Joined: Mon Apr 15, 2019 3:54 pm

Re: ScriptBasic

Sat Jun 08, 2019 8:03 pm

RichardRussell wrote:
Sat Jun 08, 2019 3:34 pm
I'm using a buffer, allocated on the stack, to store the bits directly (24000 bytes, holding up to 6000 32-bit values each representing a snapshot of all the GPIO pins). It's a little faster than using an array in BBC BASIC.

It's amusing to think that one could interrogate many DHT11 sensors simultaneously that way, without the time-critical code taking any longer. Perhaps one Raspberry Pi monitoring a dozen different temperatures in a cool store!
Using buffer to store bits is really simple with 8th. One can do something like:

Code: Select all


: bitmap \ maxbits -- b
  8 n:/mod swap 
  if n:1+ then 
  b:new b:clear ;


: app:main
  \ create bitmap to hold 40 bits
  40 bitmap
  \ Set some bits to demo bit access
  0 1 b:bit!
  1 1 b:bit!
  2 1 b:bit!
  3 1 b:bit!
  6 1 b:bit!
  16 1 b:bit!
  17 1 b:bit!
  18 1 b:bit!
  20 1 b:bit!
  35 1 b:bit!
  36 1 b:bit!
  37 1 b:bit!

  \ Check for a valid checksum and decode data
  "5B" unpack drop a:open 4 a:close dup ' n:+ 0 a:reduce 0xff n:band rot n:= if
    a:open "Humidity: %d.%d %%, Temperature: %d.%d celsius.\n" s:strfmt .
  else
    "Checksum mismatch!" . cr
  then
  bye ;
  
And the result is:

Code: Select all

Humidity: 56.0 %, Temperature: 23.0 celsius.
There are GPIO support words included with 8th and it's possible to use memory-map accessors for GPIO access, but support words still only access one pin at a time.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sat Jun 08, 2019 9:03 pm

I notice the range for the delay is between 20 - 40 before data starts being sent. I change it to 35 and it seems to have more consistent start patterns.

Code: Select all

[email protected]:~/sbrpi/examples $ scriba allbits.sb
1111000000000001111100000000000111111111111110000000000111111111111111000000000011111000000000001111111111111100000000000111100000000000111110000000000111110000000000011110000000000011111000000000001111100000000001111100000000111110000000000000111111000000000011111000000000011111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000001111111111111100000000000111110000000000011111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000111111111111111000000000001111100000000001111111111111100000000000111110000000000011111000000000001111111111111100000000000111100000000000111111111111110000000000011111111111111000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0001111111111111111110000000000011111000000000001111100000000001111111111111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111111111111110000000000011111000000000001111100000000000111110000000000111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000011111111111111111100000000000111100000000000111110000000000011111111111111000000000001111111111111110000000000111110000000000011111111111111000000000001111100000000000111111111111111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111110000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111100000000001111111111111110000000000011110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111111111111110000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000011111111111111111100000000000111110000000000111110000000000011111111111111100000000001111111111111110000000000011110000000000011111111111111100000000000111110000000000111111111111111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111000000000001111100000000000111111111111110000000000011111000000000001111000000000001111111111111110000000000011110000000000011111111111111100000000000111111111111110000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000011111111111111111100000000000111110000000000011110000000000011111111111111100000000000111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111000000000001111111111111110000000000111110000000000011111111111111100000000001111111111111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000111110000000000011111111111111100000000000111110000000000111111111111111000000000001111100000000001111100000000000111111111111111000000000011111111111111100000000000111100000000000111110000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000011111111111111111100000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111111111111111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000111111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000001111111111111110000000000011111111111111000000000001111100000000000111110000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000011111111111111111100000000000111110000000000011111000000000011111111111111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111000000000001111100000000000111110000000000111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111111111111100000000000111111000000000011111000000000001111100000000000000011111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000011111111111111111100000000000111110000000000111110000000000011111111111111100000000001111111111111110000000000011110000000000011111111111111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000011111111111111100000000000111110000000000111111111111111000000000001111111111111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111100000000000111111111111111000000000001111100000000000111111111111110000000000011111000000000001111000000000001111111111111110000000000011110000000000011111111111111100000000000111111111111110000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
0000011111111111111111100000000000111110000000000011110000000000011111111111111100000000001111111111111110000000000011111000000000011111111111111100000000000111100000000000111111111111111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111111111111110000000000011111111111111000000000001111100000000000111110000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111<*>
^C
[email protected]:~/sbrpi/examples $ 

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sat Jun 08, 2019 9:32 pm

This is what I have come up with (delay adjustments) for the best results. I think it's possible to correct bad / short data segments as they can be isolated as such.


Extension Module Addition

Code: Select all

besFUNCTION(sb_BitStreamRead)
  char buf[850];
  int pin, status, idx;
  besARGUMENTS("i")
    &pin
  besARGEND
  for (idx = 0; idx < 850; idx++){
    status = digitalRead(pin);
    delayMicroseconds(5);
    if (status == 0){
      buf[idx] = 0x30;
    }else{
      buf[idx] = 0x31;
    }
  }
  besRETURN_STRING(buf);
besEND
allbits.sb

Code: Select all

IMPORT wpi.bas

WPI::wiringPiSetup()
WPI::piHiPri(99)

valid = 0
WHILE valid = 0
  laststate = 1
  bits = ""

  WPI::pinMode(7, 1)
  WPI::digitalWrite(7, 0)
  WPI::delay(18)
  WPI::digitalWrite(7, 1)
  WPI::delayMicroseconds(38)
  WPI::pinMode(7, 0)
  bits = WPI::BitStreamRead(7)
  PRINT bits,"<*>\n"
  WPI::delay(1500)
WEND
WPI::piHiPri(0)  
Output

Code: Select all

1110000000000011111000000000001111100000000001111111111111110000000000111111111111111000000000011111000000000001111111111111100000000000111100000000000111111111111111000000000001111000000000001111100000000000111110000000000111110000000000011111000000000011111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111111111111111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011110000000000011111111111111100000000000111111111111110000000000011111000000000001111111111111100000000000111110000000000011111000000000011111111111111100000000000111111111111110000000000011111111111111000000000001111100000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111�<*>
0000011111111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111111111111111000000000011111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000011111111111111100000000000111110000000000111111111111111000000000001111111111111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111111111111110000000000011111000000000001111000000000001111111111111110000000000111111111111111000000000001111100000000001111111111111110000000000011111111111111111111111111111111111111111111111111111111111111111111111�<*>
0000111111111111111111000000000001111000000000001111100000000000111111111111110000000000011111111111111100000000000111100000000000111111111111111000000000011111000000000001111111111111110000000000011111000000000011111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111000001000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111111111111110000000000011111000000000001111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111�<*>
0000011111111111111111100000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111111111111111000000000011111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000111111111111111000000000001111111111111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111111111111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111111111111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111111111111111111111111111111111111111111111111111�<*>
0000111111111111111111000000000001111000000000001111100000000000111111111111110000000000011111111111111100000000000111100000000000111111111000000011110000000000011111111111111100000000000111110000000000111110000000000011111000000000001111100000000000111110000000000011111000000000011111000000000001111100000000000111110000000000011111000000000001111100000000000111111111111110000000000011111000000000001111111111111100000000000111111111111110000000000011111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111111111111110000000000011111000000000001111111111111111111111111111111111111111111111111111111111111111111111111�<*>
0000011111111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111111111111111000000000011111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000011111111111111100000000000111110000000000111111111111111000000000001111111111111100000000000111110000000000011111000000000001111000000000001111110000000000011111000000000001111100000000000111111111111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111111111111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111111111111111111111111111111111111111111111111111111111111111111�<*>
0000111111111111111111000000000001111100000000001111100000000000111111111111110000000000011111111111111100000000000111110000000000111111111111111000000000001111000000000001111111111111110000000000011111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111000000000001111111111111100000000000111111111111110000000000011111111111111000000000001111111111111110000000000111111111111111111111111111111111111111111111111111111111111111�<*>
0000011111111111111111100000000001111100000000000111110000000000011111111111111000000000001111111111111110000000000111110000000000011111111111111000000000001111100000000000111111111111111000000000001111000000000001111100000000000111110000000000011111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111111111111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111111111111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111111111111111111111111111111111111111111111111111�<*>
0000111111111111111111000000000001111100000000000111110000000000111111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000000111110000000000111110000000000011111000000000001111111111111100000000000111111111111111000000000001111000000000001111111111111110000000000011110000000000011111000111111111100000000000111111111111110000000000011111111111111000000000001111100000000000111111111111111111111111111111111111111111111111111111111111111111111111111�<*>
0000111111111111111111000000000001111100000000000111110000000000111111111100001111111111111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111000000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000000111110000000000111110000000000011111000000000001111111111111100000000000111111111111111000000000011111000000000001111111111111100000000000111110000000000011111000000000001111111111111100000000000111111111111110000000000011111111111111100000000001111110000000000111111111111111111111111111111111111111111111111111111111111111111111111111�<*>
0000011111111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111000000000001111100000000000111110000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111111111111100000000000111110000000000011110000000000011111000000000001111100000000000111110000000000011111000000000001111111111111100000000000111110000000000011111000000000001111000000000001111111111111110000000000111110000000000011111000000000001111111111111100000000000111111111111110000000000011111111111111100000000000111111111111110000000000011111111111111111111111111111111111111111111111111111111111111�<*>
0000111111111111111111000000000001111100000000000111110000000000011111111111111000000000001111111111111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111000000000001111100000000000111110000000000111110000000000011111000000000001111100000000000111110000000000011111000000000001111100000000001111100000000000111110000000000011111111111111000000000001111100000000000111111111111110000000000011111111111111000000000001111100000000000111110000000000011111000000000001111100000000000111110000000000011110000000000011111000000000001111111111111100000000000111111111111111000000000001111100000000000111111111111110000000000011111000000000011111000000000001111111111111110000000000111111111111111000000000001111111111111100000000000111110000000000011111111111111111111111111111111111111111111111111111111111111�<*>
[email protected]:~/sbrpi/examples $ 

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sat Jun 08, 2019 11:28 pm

@hippy,

I adapted part of your Python decoding code which looks likes it's working.

Code: Select all

IMPORT wpi.bas

WPI::wiringPiSetup()
WPI::piHiPri(99)

valid = 0
WHILE valid = 0
  laststate = 1
  bits = ""

  WPI::pinMode(7, 1)
  WPI::digitalWrite(7, 0)
  WPI::delay(18)
  WPI::digitalWrite(7, 1)
  WPI::delayMicroseconds(38)
  WPI::pinMode(7, 0)
  bits = WPI::BitStreamRead(7)

  splita bits by "" to samples


' --------------------------
' Extract the pulse lengths                                              |
' --------------------------
   
  pulseSize = 0
  last = 0
  for index = 1 to ubound(samples)
    if samples[index] = 1 then
      if last = 0 then
        pulseSize += 1
      end if
      if pulseSize <= 40 then
        pulses[pulseSize-1] += 1
      end if
    end if
    last = samples[index]
  next

 
' -------------------------------------------------------
' Determine minimum and maximum pulse sizes, and mid-way between
' -------------------------------------------------------

minimum = pulses[0]
maximum = pulses[0]

for index = 0 to 40
  if pulses[index] < minimum then minimum = pulses[index]
  if pulses[index] > maximum then maximum = pulses[index]
next
middle = ((maximum - minimum) \ 2) + minimum


' ---------------------
' Determine outbit values
' ---------------------

for index = 0 to 40
  if pulses[index] > middle then
    outbits[index] = 1
  else
    outbits[index] = 0
  end if
next

for idx = 0 to ubound(outbits)
  print outbits[idx]
next
print

' PRINT bits,"<*>\n"
  WPI::delay(1500)
WEND
WPI::piHiPri(0)  
Output

Code: Select all

[email protected]:~/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

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 09, 2019 2:59 am

It looks like the data starts on the second byte.

Code: Select all

1 00110110 00000000 00010111 00000010 00001100
     54        0       23       2        12

jalih
Posts: 64
Joined: Mon Apr 15, 2019 3:54 pm

Re: ScriptBasic

Sun Jun 09, 2019 6:53 am

ScriptBasic wrote:
Sun Jun 09, 2019 2:59 am
It looks like the data starts on the second byte.

Code: Select all

1 00110110 00000000 00010111 00000010 00001100
     54        0       23       2        12
There seems to be a checksum mismatch.

User avatar
RichardRussell
Posts: 531
Joined: Thu Jun 21, 2012 10:48 am

Re: ScriptBasic

Sun Jun 09, 2019 2:57 pm

RichardRussell wrote:
Sat Jun 08, 2019 3:34 pm
It's amusing to think that one could interrogate many DHT11 sensors simultaneously
I've added a second DHT11, external to the RPi's case; here are the results with my BBC BASIC code modified to read both:

Code: Select all

Sun. 9 Jun 2019,15:48:26  Sensor 1: 15.0% RH, 38.5C  Sensor 2: 50.0% RH, 23.9C
Sun. 9 Jun 2019,15:48:28  Sensor 1: 16.0% RH, 38.4C  Sensor 2: 50.0% RH, 23.8C
Sun. 9 Jun 2019,15:48:30  Sensor 1: 16.0% RH, 38.4C  Sensor 2: 51.0% RH, 23.8C
Sun. 9 Jun 2019,15:48:32  Sensor 1: checksum error   Sensor 2: checksum error
Sun. 9 Jun 2019,15:48:34  Sensor 1: 16.0% RH, 38.4C  Sensor 2: 50.0% RH, 23.9C
Sun. 9 Jun 2019,15:48:36  Sensor 1: 16.0% RH, 38.4C  Sensor 2: 50.0% RH, 23.9C
Sun. 9 Jun 2019,15:48:39  Sensor 1: 16.0% RH, 38.4C  Sensor 2: 50.0% RH, 23.8C
Sun. 9 Jun 2019,15:48:41  Sensor 1: 16.0% RH, 38.4C  Sensor 2: 50.0% RH, 23.9C
Sun. 9 Jun 2019,15:48:43  Sensor 1: checksum error   Sensor 2: checksum error
Sun. 9 Jun 2019,15:48:45  Sensor 1: checksum error   Sensor 2: checksum error
Sun. 9 Jun 2019,15:48:47  Sensor 1: wrong count      Sensor 2: wrong count
Sun. 9 Jun 2019,15:48:49  Sensor 1: 15.0% RH, 38.5C  Sensor 2: 50.0% RH, 24.0C
Sun. 9 Jun 2019,15:48:51  Sensor 1: 15.0% RH, 38.3C  Sensor 2: 50.0% RH, 24.0C
Sun. 9 Jun 2019,15:48:53  Sensor 1: 15.0% RH, 38.5C  Sensor 2: 50.0% RH, 23.9C
Sun. 9 Jun 2019,15:48:55  Sensor 1: 15.0% RH, 38.4C  Sensor 2: 50.0% RH, 24.0C
Sun. 9 Jun 2019,15:48:57  Sensor 1: checksum error   Sensor 2: 50.0% RH, 23.9C
Sun. 9 Jun 2019,15:48:59  Sensor 1: checksum error   Sensor 2: 50.0% RH, 23.9C
Sun. 9 Jun 2019,15:49:01  Sensor 1: 15.0% RH, 38.4C  Sensor 2: 50.0% RH, 24.0C
Sun. 9 Jun 2019,15:49:04  Sensor 1: 15.0% RH, 38.4C  Sensor 2: 50.0% RH, 23.9C
Sun. 9 Jun 2019,15:49:06  Sensor 1: 15.0% RH, 38.4C  Sensor 2: 50.0% RH, 24.0C
Sun. 9 Jun 2019,15:49:08  Sensor 1: 15.0% RH, 38.4C  Sensor 2: 50.0% RH, 23.9C
Sun. 9 Jun 2019,15:49:10  Sensor 1: 15.0% RH, 38.4C  Sensor 2: 50.0% RH, 24.0C
Sun. 9 Jun 2019,15:49:12  Sensor 1: 15.0% RH, 38.5C  Sensor 2: 50.0% RH, 24.0C
Sun. 9 Jun 2019,15:49:14  Sensor 1: 15.0% RH, 38.4C  Sensor 2: checksum error
It's of interest that there is clear correlation between the two sensors in respect of errors, confirming that the source is almost certainly sampling related rather than timing instability arising from the device itself.
Last edited by RichardRussell on Sun Jun 09, 2019 3:02 pm, edited 1 time in total.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 09, 2019 2:57 pm

I may go back to how the C version of this works. I not getting anywhere with this direction.

I would like to see more ScriptBasic example code posted in this thread. If your direction is Python based, please start another thread.

Richard,

It's great you have your interpreter working with the DHT11. I would appreciate any help as a BASIC developer resolving the issues we are having with ScriptBasic. BBC BASIC news and progress reports should go in your thread.

@jalih,

It would be great if you could post your 8th and non-RPi board progress on the AllBASIC forum. Maybe we can get AIR involved. Do you have a DHT11?

jalih
Posts: 64
Joined: Mon Apr 15, 2019 3:54 pm

Re: ScriptBasic

Sun Jun 09, 2019 5:17 pm

ScriptBasic wrote:
Sun Jun 09, 2019 2:57 pm
@jalih,

It would be great if you could post your 8th and non-RPi board progress on the AllBASIC forum. Maybe we can get AIR involved. Do you have a DHT11?
I don't have DHT11 yet but I am planning to order one! Currently I am playing with sampling and decoding stuff. I will post something working soon, I hope!

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 09, 2019 5:28 pm

I will be happy to share my DHT11 bitstream strings for you to play with until you get your own DHT11.

The new BitStreamRead WPI function is going to act like a mini data analyzer passing the pin, buffer size and a delay in microseconds value which returns a 1/0 string of the data captured.

User avatar
RichardRussell
Posts: 531
Joined: Thu Jun 21, 2012 10:48 am

Re: ScriptBasic

Sun Jun 09, 2019 5:48 pm

ScriptBasic wrote:
Sun Jun 09, 2019 2:57 pm
BBC BASIC news and progress reports should go in your thread.
I think it would be better to take the 'DHT11 interfacing' discussion away from this thread altogether, a good place for it might be in the 'Projects... Automation, Sensing and Robotics' section. That way we can continue to compare implementations in different languages, which is valuable, without trampling over your ScriptBasic thread.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 09, 2019 5:54 pm

I plan to continue on with other common used sensors in this thread once I put the DHT11 native version behind me. My point is if you're going to offer suggestions in this thread, use ScriptBasic to explain your concepts.

What I need now is a fast 8 bit binary string to byte routine.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 09, 2019 7:48 pm

I created a new bin2int WPI function and got the native DHT11 ScriptBasic script to generate >10 error free reads of the sensor.

Code: Select all

IMPORT wpi.bas

WPI::wiringPiSetup()
WPI::piHiPri(99)

valid = 0
WHILE valid = 0
  laststate = 1
  bits = ""

  WPI::pinMode(7, 1)
  WPI::digitalWrite(7, 0)
  WPI::delay(18)
  WPI::digitalWrite(7, 1)
  WPI::delayMicroseconds(40)
  WPI::pinMode(7, 0)
  bits = WPI::BitStreamRead(7)

  splita bits by "" to samples


' --------------------------
' Extract the pulse lengths                                              |
' --------------------------
   
  pulseSize = 0
  last = 0
  for index = 2 to ubound(samples)
    if samples[index] = 1 then
      if last = 0 then
        pulseSize += 1
      end if
      if pulseSize <= 40 then
        pulses[pulseSize-1] += 1
      end if
    end if
    last = samples[index]
  next

 
' -------------------------------------------------------
' Determine minimum and maximum pulse sizes, and mid-way between
' -------------------------------------------------------

minimum = pulses[0]
maximum = pulses[0]

for index = 0 to 40
  if pulses[index] < minimum then minimum = pulses[index]
  if pulses[index] > maximum then maximum = pulses[index]
next
middle = ((maximum - minimum) \ 2) + minimum


' ---------------------
' Determine outbit values
' ---------------------

for index = 0 to 40
  if pulses[index] > middle then
    outbits[index] = 1
  else
    outbits[index] = 0
  end if
next
strbits = ""
for idx = 0 to ubound(outbits)
  print outbits[idx]
  strbits &= outbits[idx]
next
print


' ---------------
' Form the bytes
' ---------------
index = 0
for idx = 2 TO LEN(strbits) STEP 8
  byte[index] = WPI::bin2int(mid(strbits,idx,8))
  index += 1
next  

' --------------
' Print results
' --------------

if ((byte[0] + byte[1] + byte[2] + byte[3]) % 0xFF) = byte[4] then
  print "Checksum matches\n"
else
  print "Checksum does not match\n"
end if

print byte[0],"\n"
print byte[1],"\n"
print byte[2],"\n"
print byte[3],"\n"
print byte[4],"\n"

undef byte

SLEEP(2)
WEND
WPI::piHiPri(0)  
Output

Code: Select all

[email protected]:~/sbrpi/examples $ scriba allbits.sb
00011011100000000000101010000000101001100
Checksum does not match
55
0
21
1
76
10011011100000000000101010000000000001100
Checksum does not match
55
0
21
0
12
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
10011011100000000000101010000000001001100
Checksum matches
55
0
21
0
76
^C
[email protected]:~/sbrpi/examples $ 

hippy
Posts: 5363
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Sun Jun 09, 2019 8:31 pm

ScriptBasic wrote:
Sun Jun 09, 2019 5:54 pm
My point is if you're going to offer suggestions in this thread, use ScriptBasic to explain your concepts.
But 'no one else is using ScriptBasic' and those who may be able to help with hardware interfacing probably have little desire to learn ScriptBasic as a language

People are more than willing to share their knowledge to help you with your ScriptBasic ventures, show what they have done, explain how it works, what sort of performance they are getting, what problems there have been, what things to watch out for, but, if you want code examples only in ScriptBasic, things will likely go very quiet on the code front.

It is common practice to provide pseudo code to explain concepts, and when those concepts have actually been implemented, providing real language examples serves as very good pseudo code. Especially when it goes beyond 'something like this' to tried, tested and working.

Bottom line is you always have to take whatever you can get, and something is usually far better than nothing.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 09, 2019 8:35 pm

Here is the final ScriptBasic native DHT11 script.

Thanks @hippy for your code contribution to the final solution. I learnt a lot about GPIO building the WiringPi extension module and your guidance.

I think I'm going to rewrite the pulse detection as I know from my WPI::BitStreamRead function what my zero and one pulse widths are. I may take a stab at data correction if known patterns of it are repeatable.

Code: Select all

IMPORT wpi.bas

WPI::wiringPiSetup()
WPI::piHiPri(99)

valid = 0
WHILE valid = 0
  bits = ""
  WPI::pinMode(7, 1)
  WPI::digitalWrite(7, 0)
  WPI::delay(18)
  WPI::digitalWrite(7, 1)
  WPI::delayMicroseconds(40)
  WPI::pinMode(7, 0)
  bits = WPI::BitStreamRead(7)
  splita bits by "" to samples

' **** Extract the pulse lengths ****                                             

  pulseSize = 0
  last = 0
  for index = 2 to ubound(samples)
    if samples[index] = 1 then
      if last = 0 then
        pulseSize += 1
      end if
      if pulseSize <= 40 then
        pulses[pulseSize-1] += 1
      end if
    end if
    last = samples[index]
  next
  undef samples

' **** Determine minimum and maximum pulse sizes, and mid-way between ****

  minimum = pulses[0]
  maximum = pulses[0]

  for index = 0 to 40
    if pulses[index] < minimum then minimum = pulses[index]
    if pulses[index] > maximum then maximum = pulses[index]
  next
  middle = ((maximum - minimum) \ 2) + minimum

' **** Determine outbits values ****

  outbits = ""
  for index = 0 to 40
    if pulses[index] > middle then
      outbits &= 1
    else
      outbits &= 0
    end if
  next
  undef pulses

' **** Form the bytes ****

  index = 0
  for idx = 2 TO LEN(outbits) STEP 8
    byte[index] = WPI::bin2int(mid(outbits,idx,8))
    index += 1
  next 
  outbits = "" 

' **** Print results ****

  if byte[0] + byte[1] + byte[2] + byte[3] = byte[4] then
    print format("Humidity: %d.%d %%  -  Temperature: %d.%d C\n", byte[0], byte[1], byte[2], byte[3])
  else
    print "<Checksum Error>\n"
  end if
  undef byte
  SLEEP(2)
WEND

WPI::piHiPri(0)  

end
Output

Code: Select all

[email protected]:~/sbrpi/examples $ scriba allbits.sb
<Checksum Error>
<Checksum Error>
Humidity: 55.0 %  -  Temperature: 21.6 C
<Checksum Error>
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
<Checksum Error>
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
<Checksum Error>
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
Humidity: 55.0 %  -  Temperature: 21.6 C
<Checksum Error>
Last edited by ScriptBasic on Mon Jun 10, 2019 1:12 am, edited 2 times in total.

User avatar
ScriptBasic
Posts: 1254
Joined: Wed Apr 03, 2019 5:53 pm
Location: Anacortes, WA USA
Contact: Website Twitter

Re: ScriptBasic

Sun Jun 09, 2019 10:03 pm

I'm done with the DHT11 but feel free to post your creation when you have it working.

I'm going to be taking the ScriptBasic sensor project to the RaspberryBASIC.org forum. I don't want to get in trouble for flooding the forum with a language only I'm using. I gave it my best effort but ScriptBasic must not be worthwhile enough to spend any time trying to use / learn it. I apologize to the Raspberry Pi foundation for misusing their resouces.

BBC BASIC is more appropriate for most of the members on this forum. ScriptBasic is focused on providing solutions for embedded language developers.

hippy
Posts: 5363
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: ScriptBasic

Mon Jun 10, 2019 1:45 pm

ScriptBasic wrote:
Sun Jun 09, 2019 10:03 pm
I don't want to get in trouble for flooding the forum with a language only I'm using.
I don't see why that should happen. It seems no different to any project which any user may be wanting help with. Most topics raised have only a limited audience which is interested in them, even the longer ones.
ScriptBasic wrote:
Sun Jun 09, 2019 10:03 pm
I gave it my best effort but ScriptBasic must not be worthwhile enough to spend any time trying to use / learn it.
If you had an agenda to make ScriptBasic 'the one true basic' for a Pi, have people flocking to use it and deserting everything else, then, no, you haven't succeeded in that, though I have no idea who may have taken an interest in it or how they feel about it.

But I wouldn't let anyone's lack of interest in ScriptBasic itself put you off leveraging people's willingness to help with whatever you may need help with.

Return to “Other programming languages”