Author Topic: RPI Sense HAT and 8th  (Read 551 times)

Offline jalih

  • Advocate
  • Posts: 52
RPI Sense HAT and 8th
« on: April 27, 2019, 01:02:38 AM »
Hi John,

I tried converting some code from C for 8th. I just converted the temperature part and left the pressure out. Could you try it on real hardware? I would like to know if it works, or not. Attached is a runnable binary.

Code: [Select]

: DEV_ID    0x5c ;
: WHO_AM_I  0x0f ;

: CTRL_REG1 0x20 ;
: CTRL_REG2 0x21 ;

: TEMP_OUT_L   0x2b ;
: TEMP_OUT_H   0x2c ;


var LPS25H


: init
  1 DEV_ID hw:i2c null? if
    drop
    DEV_ID "Cannot connect to the LPS25H on I2C address %02x" s:strfmt throw
  else
    WHO_AM_I true hw:i2c@reg null? not if
      0xbd n:= not if
        "who_am_i error" throw
      then
    else
      WHO_AM_I "error reading from %02x" s:strfmt throw
    then
  then

  LPS25H ! ;


: power-down
  LPS25H @ CTRL_REG1 0x00 true hw:i2c!reg nip if
   "Error powering down the device." throw
  then ;


: set-mode
  LPS25H @ CTRL_REG1 0x84 true hw:i2c!reg nip if
   "Error turning on the pressure sensor analog front end in single shot mode" throw
  then ;


: one-shot-measure
  LPS25H @ CTRL_REG2 0x01 true hw:i2c!reg nip if
   "Error running one-shot measurement (temperature and pressure)" throw
  then ;


: wait-results
  repeat
    0.25 sleep
    LPS25H @ CTRL_REG2 true hw:i2c@reg nip null? if
      drop
      "Error reading measurement status." throw
    then
    0 n:= not if
      break
    then
  again ;


: get-temp  \  -- temp
  LPS25H @ TEMP_OUT_L true hw:i2c@reg nip null? if
    drop
    "Error reading low byte of the temperature" throw
  then

  LPS25H @ TEMP_OUT_H true hw:i2c@reg nip null? if
    drop
    "Error reading high byte of the temperature" throw
  then

  8 n:shl n:bor 480 n:/ 42.5 n:+ ;


: app:main
  init
  power-down
  set-mode
  one-shot-measure
  wait-results
  get-temp . cr
  power-down
  bye ;
« Last Edit: April 27, 2019, 01:44:44 AM by jalih »

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: RPI Sense HAT and 8th
« Reply #1 on: April 27, 2019, 09:14:52 AM »
Code: Bash
  1. pi@RPi3B:~/rpi8th/rpi32_shat $ ./test
  2. Exception: Error powering down the device.: : user:power-down
  3. pi@RPi3B:~/rpi8th/rpi32_shat $
  4.  

6.7 MB ?
« Last Edit: April 27, 2019, 09:16:59 AM by John »

Offline jalih

  • Advocate
  • Posts: 52
Re: RPI Sense HAT and 8th
« Reply #2 on: April 27, 2019, 10:26:12 AM »
6.7 MB ?

8th packages everything into it's binary. So there is script file, interpreter that interpretes to native code, JUCE + bunch of other stuff. On the plus side, programs work and look the same on any platform without the need to install external depencies. I don't think that the binary size is too large, if you compare it to some QT application size for instance. Most of the time I use 8th to run my scripts directly from command-line. I also find REPL handy for testing some code or just to calculate some math expressions.

Offline jalih

  • Advocate
  • Posts: 52
Re: RPI Sense HAT and 8th
« Reply #3 on: April 27, 2019, 10:48:09 AM »
Code: Bash
  1. pi@RPi3B:~/rpi8th/rpi32_shat $ ./test
  2. Exception: Error powering down the device.: : user:power-down
  3. pi@RPi3B:~/rpi8th/rpi32_shat $
  4.  

Seems I made a stupid mistake on one word by throwing exception on success, not the other way around. Then I copy pasted it to other words as well!  ;D

Here is the corrected version:

Code: [Select]


: DEV_ID    0x5c ;
: WHO_AM_I  0x0f ;

: CTRL_REG1 0x20 ;
: CTRL_REG2 0x21 ;

: TEMP_OUT_L   0x2b ;
: TEMP_OUT_H   0x2c ;


var LPS25H


: init
  1 DEV_ID hw:i2c null? if
    drop
    DEV_ID "Cannot connect to the LPS25H on I2C address %02x" s:strfmt throw
  else
    WHO_AM_I true hw:i2c@reg null? not if
      0xbd n:= not if
        "who_am_i error" throw
      then
    else
      WHO_AM_I "error reading from %02x" s:strfmt throw
    then
  then

  LPS25H ! ;


: power-down
  LPS25H @ CTRL_REG1 0x00 true hw:i2c!reg nip not if
   "Error powering down the device" throw
  then ;


: set-mode
  LPS25H @ CTRL_REG1 0x84 true hw:i2c!reg nip not if
   "Error turning on the pressure sensor analog front end in single shot mode" throw
  then ;


: one-shot-measure
  LPS25H @ CTRL_REG2 0x01 true hw:i2c!reg nip not if
   "Error running one-shot measurement (temperature and pressure)" throw
  then ;


: wait-results
  repeat
    0.025 sleep
    LPS25H @ CTRL_REG2 true hw:i2c@reg nip null? if
      drop
      "Error reading measurement status" throw
    then
    0 n:= not if
      break
    then
  again ;


: get-temp  \  -- temp
  LPS25H @ TEMP_OUT_L true hw:i2c@reg nip null? if
    drop
    "Error reading low byte of the temperature" throw
  then

  LPS25H @ TEMP_OUT_H true hw:i2c@reg nip null? if
    drop
    "Error reading high byte of the temperature" throw
  then

  8 n:shl n:bor 480 n:/ 42.5 n:+ ;


: app:main
  init
  power-down
  set-mode
  one-shot-measure
  wait-results
  get-temp . cr
  power-down
  bye ;

John could you download fixed version and test it on real hardware? It should just output temperature value if it succeeds. If it fails, it's exception time...
« Last Edit: April 27, 2019, 11:05:23 AM by jalih »

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: RPI Sense HAT and 8th
« Reply #4 on: April 27, 2019, 11:38:26 AM »
Nope.

i@RPi3B:~/rpi8th/rpi32_shat/rpi32 $ ls -l
total 6896
-rwxr-xr-x 1 pi pi 7061277 Apr 27  2019 test
pi@RPi3B:~/rpi8th/rpi32_shat/rpi32 $ ./test
Exception: Error powering down the device.: : user:power-down
pi@RPi3B:~/rpi8th/rpi32_shat/rpi32 $


Offline jalih

  • Advocate
  • Posts: 52
Re: RPI Sense HAT and 8th
« Reply #5 on: April 27, 2019, 11:52:31 AM »
Nope.

I accidentally packaged the original erronous script and posted it to dropbox, not the corrected one. I could see it from the error message thrown. Corrected version doesn't have dot at the end of the message.

Now the dropbox link above should work poperly and have the corrected binary.

Offline John

  • Forum Support / SB Dev
  • Posts: 2752
    • ScriptBasic Open Source Project
Re: RPI Sense HAT and 8th
« Reply #6 on: April 27, 2019, 12:21:57 PM »
Success!


pi@RPi3B:~/rpi8th/rpi32_3 $ ./test
42.50000
pi@RPi3B:~/rpi8th/rpi32_3 $ scriba ~/sbrpi/examples/testhat.sb
Pressure: 756
P-Temp: 425
Humidity: 390
H-Temp: 255
Accel-x: 0
Accel-y: 0
Accel-z: 0
Mag-x: -1982
Mag-y: 1416
Mag-z: -386
Gyro-x: 1591
Gyro-y: 877
Gyro-z: 75
pi@RPi3B:~/rpi8th/rpi32_3 $



Offline jalih

  • Advocate
  • Posts: 52
Re: RPI Sense HAT and 8th
« Reply #7 on: April 27, 2019, 12:47:31 PM »
Success!

Thanks,

Seems like I messed up with "wait-results" word also! :D I did'nt wait correctly for measurement to be complete. I corrected my code and uploaded new binary to the dropbox. Good thing is that communicating with Sense HAT works properly from 8th! Thanks John for testing my code.

After I get my ROCK64 board, I will try attaching some sensors and interfacing them...

Code: [Select]

: DEV_ID    0x5c ;
: WHO_AM_I  0x0f ;

: CTRL_REG1 0x20 ;
: CTRL_REG2 0x21 ;

: TEMP_OUT_L   0x2b ;
: TEMP_OUT_H   0x2c ;


var LPS25H


: init
  1 DEV_ID hw:i2c null? if
    drop
    DEV_ID "Cannot connect to the LPS25H on I2C address %02x" s:strfmt throw
  else
    WHO_AM_I true hw:i2c@reg null? not if
      0xbd n:= not if
        "who_am_i error" throw
      then
    else
      WHO_AM_I "error reading from %02x" s:strfmt throw
    then
  then

  LPS25H ! ;


: power-down
  LPS25H @ CTRL_REG1 0x00 true hw:i2c!reg nip not if
   "Error powering down the device" throw
  then ;


: set-mode
  LPS25H @ CTRL_REG1 0x84 true hw:i2c!reg nip not if
   "Error turning on the pressure sensor analog front end in single shot mode" throw
  then ;


: one-shot-measure
  LPS25H @ CTRL_REG2 0x01 true hw:i2c!reg nip not if
   "Error running one-shot measurement (temperature and pressure)" throw
  then ;


: wait-results
  repeat
    0.025 sleep
    LPS25H @ CTRL_REG2 true hw:i2c@reg nip null? if
      drop
      "Error reading measurement status" throw
    then
    0 n:= if
      break
    then
  again ;


: get-temp  \  -- temp
  LPS25H @ TEMP_OUT_L true hw:i2c@reg nip null? if
    drop
    "Error reading low byte of the temperature" throw
  then

  LPS25H @ TEMP_OUT_H true hw:i2c@reg nip null? if
    drop
    "Error reading high byte of the temperature" throw
  then

  8 n:shl n:bor 480 n:/ 42.5 n:+ ;


: app:main
  init
  power-down
  set-mode
  one-shot-measure
  wait-results
  get-temp . cr
  power-down
  bye ;