BASIC User Group > NOT BASIC

RPI Sense HAT and 8th

(1/2) > >>

jalih:
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: ---
: 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 ;

--- End code ---

John:

--- Code: Bash ---pi@RPi3B:~/rpi8th/rpi32_shat $ ./testException: Error powering down the device.: : user:power-downpi@RPi3B:~/rpi8th/rpi32_shat $  
6.7 MB ?

jalih:

--- Quote from: John on April 27, 2019, 09:14:52 am ---6.7 MB ?

--- End quote ---

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.

jalih:

--- Quote from: John on April 27, 2019, 09:14:52 am ---
--- Code: Bash ---pi@RPi3B:~/rpi8th/rpi32_shat $ ./testException: Error powering down the device.: : user:power-downpi@RPi3B:~/rpi8th/rpi32_shat $  
--- End quote ---

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: ---

: 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 ;

--- End code ---

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

John:
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 $

Navigation

[0] Message Index

[#] Next page

Go to full version