Skip to content

Commit

Permalink
Kebabify. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Oct 4, 2024
1 parent 90d0012 commit 9484738
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 111 deletions.
20 changes: 10 additions & 10 deletions examples/main.toit
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ TX ::= 16
RTS ::= 18

main:
pin_rx := gpio.Pin RX
pin_tx := gpio.Pin TX
pin_rts := gpio.Pin RTS
pin-rx := gpio.Pin RX
pin-tx := gpio.Pin TX
pin-rts := gpio.Pin RTS

rs485_bus := rs485.Rs485
--rx=pin_rx
--tx=pin_tx
--rts=pin_rts
--baud_rate=r46ca01.R46ca01.DEFAULT_BAUD_RATE
bus := modbus.Modbus.rtu rs485_bus
rs485-bus := rs485.Rs485
--rx=pin-rx
--tx=pin-tx
--rts=pin-rts
--baud-rate=r46ca01.R46ca01.DEFAULT-BAUD-RATE
bus := modbus.Modbus.rtu rs485-bus

// Assume that the sensor is the only one on the bus.
sensor := r46ca01.R46ca01.detect bus

print sensor.read_temperature
print sensor.read-temperature
84 changes: 42 additions & 42 deletions src/r46ca01.toit
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import gpio
A driver for the R46CA01 temperature sensor.
*/
class R46ca01:
static DEFAULT_UNIT_ID ::= 1
static DEFAULT_BAUD_RATE ::= 9600
static DEFAULT-UNIT-ID ::= 1
static DEFAULT-BAUD-RATE ::= 9600

static TEMPERATURE_ADDRESS_ ::= 0x00
static UNIT_ID_ADDRESS_ ::= 0x02
static BAUD_RATE_ADDRESS_ ::= 0x03
static CORRECTION_ADDRESS_ ::= 0x04
static TEMPERATURE-ADDRESS_ ::= 0x00
static UNIT-ID-ADDRESS_ ::= 0x02
static BAUD-RATE-ADDRESS_ ::= 0x03
static CORRECTION-ADDRESS_ ::= 0x04

static BAUD_RATE_1200_ ::= 0
static BAUD_RATE_2400_ ::= 1
static BAUD_RATE_4800_ ::= 2
static BAUD_RATE_9600_ ::= 3
static BAUD_RATE_19200_ ::= 4
static BAUD-RATE-1200_ ::= 0
static BAUD-RATE-2400_ ::= 1
static BAUD-RATE-4800_ ::= 2
static BAUD-RATE-9600_ ::= 3
static BAUD-RATE-19200_ ::= 4

registers_/modbus.HoldingRegisters

Expand All @@ -33,34 +33,34 @@ class R46ca01:
The given Modbus $station must be an R46CA01 device.
*/
constructor station/modbus.Station:
registers_ = station.holding_registers
registers_ = station.holding-registers

/**
Creates a new R46CA01 driver.
Uses $detect_unit_id to find the unit id of the R46CA01 device.
Uses $detect-unit-id to find the unit id of the R46CA01 device.
The R46CA01 device must be the only device on the bus.
*/
constructor.detect bus/modbus.Modbus:
id := detect_unit_id bus
id := detect-unit-id bus
return R46ca01 (bus.station id)

/**
Reads the unit id (also known as "server address", or "station address") from the connected sensor.
Note that only one unit must be on the bus when performing this action.
*/
static detect_unit_id bus/modbus.Modbus -> int:
broadcast_station := bus.station 0xFF
return broadcast_station.holding_registers.read_single --address=UNIT_ID_ADDRESS_
static detect-unit-id bus/modbus.Modbus -> int:
broadcast-station := bus.station 0xFF
return broadcast-station.holding-registers.read-single --address=UNIT-ID-ADDRESS_

/**
Reads the temperature.
Returns the result in degrees Celsius.
*/
read_temperature -> float:
raw := read_temperature --raw
read-temperature -> float:
raw := read-temperature --raw
if raw == 0x8000: throw "NO_DS18B20_OR_ERROR"
return raw * 0.1

Expand All @@ -73,27 +73,27 @@ class R46ca01:
Each unit corresponds to 0.1 degrees Celsius.
*/
read_temperature --raw/bool -> int:
read-temperature --raw/bool -> int:
if not raw: throw "INVALID_ARGUMENT"
return read_ TEMPERATURE_ADDRESS_
return read_ TEMPERATURE-ADDRESS_

/**
Reads the correction value.
Returns the correction value in degrees Celsius.
*/
read_correction -> float:
return (read_correction --raw) * 0.1
read-correction -> float:
return (read-correction --raw) * 0.1

/**
Reads the correction value.
Returns the raw correction value as reported by the sensor. Each unit corresponds
to 0.1 degrees Celsius.
*/
read_correction --raw/bool -> int:
read-correction --raw/bool -> int:
if not raw: throw "INVALID_ARGUMENT"
return read_ CORRECTION_ADDRESS_
return read_ CORRECTION-ADDRESS_

/**
Sets the correction value.
Expand All @@ -103,8 +103,8 @@ class R46ca01:
The correction is stored in non-volatile memory.
*/
set_correction value/float:
set_correction --raw (value * 10).to_int
set-correction value/float:
set-correction --raw (value * 10).to-int

/**
Sets the correction value.
Expand All @@ -115,9 +115,9 @@ class R46ca01:
The correction is stored in non-volatile memory.
*/
set_correction --raw value/int:
set-correction --raw value/int:
if not raw: throw "INVALID_ARGUMENT"
write_ --address=CORRECTION_ADDRESS_ value
write_ --address=CORRECTION-ADDRESS_ value

/**
Changes the unit id (also known as "server address", or "station address") to the given $id.
Expand All @@ -127,36 +127,36 @@ class R46ca01:
The $id must be in range 1-247.
*/
set_unit_id id/int:
set-unit-id id/int:
if not 1 <= id <= 247: throw "INVALID_ARGUMENT"
write_ --address=UNIT_ID_ADDRESS_ id
write_ --address=UNIT-ID-ADDRESS_ id

/**
Sets the baud rate of the sensor.
The change will only take effect after a reboot of the sensor.
The $baud_rate must be one of:
The $baud-rate must be one of:
- 1200
- 2400
- 4800
- 9600 (default)
- 19200
*/
set_baud_rate baud_rate/int:
register_value /int := ?
if baud_rate == 1200: register_value = BAUD_RATE_1200_
else if baud_rate == 2400: register_value = BAUD_RATE_2400_
else if baud_rate == 4800: register_value = BAUD_RATE_4800_
else if baud_rate == 9600: register_value = BAUD_RATE_9600_
else if baud_rate == 19200: register_value = BAUD_RATE_19200_
set-baud-rate baud-rate/int:
register-value /int := ?
if baud-rate == 1200: register-value = BAUD-RATE-1200_
else if baud-rate == 2400: register-value = BAUD-RATE-2400_
else if baud-rate == 4800: register-value = BAUD-RATE-4800_
else if baud-rate == 9600: register-value = BAUD-RATE-9600_
else if baud-rate == 19200: register-value = BAUD-RATE-19200_
else: throw "INVALID_ARGUMENT"
write_ --address=BAUD_RATE_ADDRESS_ register_value
write_ --address=BAUD-RATE-ADDRESS_ register-value

read_ address/int -> int:
// Note that the R46CA01 must use function 0x03 (read holding registers) and 0x06 (write single holding register).
// Other functions are not officially supported.
return registers_.read_int16 --address=address
return registers_.read-int16 --address=address

write_ --address/int value/int:
registers_.write_single --address=address value
registers_.write-single --address=address value
30 changes: 15 additions & 15 deletions tests/test_baudrate_esp32.toit
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,32 @@ import rs485
import modbus
import log

FROM_BAUD_RATE ::= 9600
TO_BAUD_RATE ::= 9600
FROM-BAUD-RATE ::= 9600
TO-BAUD-RATE ::= 9600

RX ::= 17
TX ::= 16
RTS ::= 18

main:
log.set_default (log.default.with_level log.INFO_LEVEL)
log.set-default (log.default.with-level log.INFO-LEVEL)

from_baudrate := FROM_BAUD_RATE
to_baudrate := TO_BAUD_RATE
from-baudrate := FROM-BAUD-RATE
to-baudrate := TO-BAUD-RATE

pin_rx := gpio.Pin RX
pin_tx := gpio.Pin TX
pin_rts := gpio.Pin RTS
pin-rx := gpio.Pin RX
pin-tx := gpio.Pin TX
pin-rts := gpio.Pin RTS

rs485_bus := rs485.Rs485
--rx=pin_rx
--tx=pin_tx
--rts=pin_rts
--baud_rate=from_baudrate
bus := modbus.Modbus.rtu rs485_bus
rs485-bus := rs485.Rs485
--rx=pin-rx
--tx=pin-tx
--rts=pin-rts
--baud-rate=from-baudrate
bus := modbus.Modbus.rtu rs485-bus

// Assume that the sensor is the only one on the bus.
sensor := r46ca01.R46ca01.detect bus

sensor.set_baud_rate to_baudrate
sensor.set-baud-rate to-baudrate
print "done"
88 changes: 44 additions & 44 deletions tests/test_esp32.toit
Original file line number Diff line number Diff line change
Expand Up @@ -20,69 +20,69 @@ RTS ::= 18
main:
print "starting"

log.set_default (log.default.with_level log.INFO_LEVEL)
pin_rx := gpio.Pin RX
pin_tx := gpio.Pin TX
pin_rts := gpio.Pin RTS

rs485_bus := rs485.Rs485
--rx=pin_rx
--tx=pin_tx
--rts=pin_rts
--baud_rate=r46ca01.R46ca01.DEFAULT_BAUD_RATE
bus := modbus.Modbus.rtu rs485_bus
log.set-default (log.default.with-level log.INFO-LEVEL)
pin-rx := gpio.Pin RX
pin-tx := gpio.Pin TX
pin-rts := gpio.Pin RTS

rs485-bus := rs485.Rs485
--rx=pin-rx
--tx=pin-tx
--rts=pin-rts
--baud-rate=r46ca01.R46ca01.DEFAULT-BAUD-RATE
bus := modbus.Modbus.rtu rs485-bus

// Assume that the sensor is the only one on the bus.
sensor := r46ca01.R46ca01.detect bus

sensor.set_correction 0.0
temperature := sensor.read_temperature
expect_equals 0.0 sensor.read_correction
expect_equals 0 (sensor.read_correction --raw)
sensor.set-correction 0.0
temperature := sensor.read-temperature
expect-equals 0.0 sensor.read-correction
expect-equals 0 (sensor.read-correction --raw)

sensor.set_correction 20.0
expect_equals 20.0 sensor.read_correction
expect_equals 200 (sensor.read_correction --raw)
changed_temperature := sensor.read_temperature
sensor.set-correction 20.0
expect-equals 20.0 sensor.read-correction
expect-equals 200 (sensor.read-correction --raw)
changed-temperature := sensor.read-temperature
// Allow for 0.5 degrees change in the short time.
expect 19.5 < changed_temperature - temperature < 20.5
expect 19.5 < changed-temperature - temperature < 20.5

sensor.set_correction -20.0
expect_equals -20.0 sensor.read_correction
expect_equals -200 (sensor.read_correction --raw)
changed_temperature = sensor.read_temperature
sensor.set-correction -20.0
expect-equals -20.0 sensor.read-correction
expect-equals -200 (sensor.read-correction --raw)
changed-temperature = sensor.read-temperature
// Allow for 0.5 degrees change in the short time.
expect -20.5 < changed_temperature - temperature < -19.5
expect -20.5 < changed-temperature - temperature < -19.5

sensor.set_correction --raw 10
expect_equals 1.0 sensor.read_correction
expect_equals 10 (sensor.read_correction --raw)
sensor.set-correction --raw 10
expect-equals 1.0 sensor.read-correction
expect-equals 10 (sensor.read-correction --raw)

sensor.set_correction --raw -10
expect_equals -1.0 sensor.read_correction
expect_equals -10 (sensor.read_correction --raw)
sensor.set-correction --raw -10
expect-equals -1.0 sensor.read-correction
expect-equals -10 (sensor.read-correction --raw)

sensor.set_correction --raw 0
sensor.set-correction --raw 0

old_id := r46ca01.R46ca01.detect_unit_id bus
print "current unit id: $old_id"
old-id := r46ca01.R46ca01.detect-unit-id bus
print "current unit id: $old-id"

sensor.set_unit_id 5
expect_equals 5 (r46ca01.R46ca01.detect_unit_id bus)
sensor.set-unit-id 5
expect-equals 5 (r46ca01.R46ca01.detect-unit-id bus)
sensor5 := r46ca01.R46ca01 (bus.station 5)
expect (sensor5.read_temperature - temperature).abs < 0.5
expect (sensor5.read-temperature - temperature).abs < 0.5

sensor5.set_unit_id 6
expect_equals 6 (r46ca01.R46ca01.detect_unit_id bus)
sensor5.set-unit-id 6
expect-equals 6 (r46ca01.R46ca01.detect-unit-id bus)
sensor6 := r46ca01.R46ca01 (bus.station 6)
expect (sensor6.read_temperature - temperature).abs < 0.5
expect (sensor6.read-temperature - temperature).abs < 0.5

print "Switching back to old unit id"
sensor6.set_unit_id old_id
sensor6.set-unit-id old-id

if old_id != 5:
expect_throw DEADLINE_EXCEEDED_ERROR: sensor5.read_temperature
if old-id != 5:
expect-throw DEADLINE-EXCEEDED-ERROR: sensor5.read-temperature
else:
expect_throw DEADLINE_EXCEEDED_ERROR: sensor6.read_temperature
expect-throw DEADLINE-EXCEEDED-ERROR: sensor6.read-temperature

print "done"

0 comments on commit 9484738

Please sign in to comment.