Skip to content

Instantly share code, notes, and snippets.

@apparentlymart
Created November 2, 2014 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apparentlymart/633b7700105bc2ac1bfa to your computer and use it in GitHub Desktop.
Save apparentlymart/633b7700105bc2ac1bfa to your computer and use it in GitHub Desktop.
Prototype Alamatic Driver for BMP183

Prototype Alamatic Driver for BMP183

This is just an exploration of what a real-world (if incomplete) BMP183 driver might look like with the current iteration of Alamatic's featureset.

It makes extensive use of the worker concept, which effectively creates a protothread that processes a queue of requests one at a time. A worker can await a promise, just like a Task can, but unlike a normal function.

This example seems to demonstrate that even this relatively simple example leads to a proliferation of workers, suggesting that if this concept is to work the protothreads must be very lightweight or else the program's memory and CPU usage could easily become dwarfed by protothread bookkeeping.

enum Register as UInt8:
CONTROL = 0xf4
TEMP_DATA = 0xf6
enum ControlCommand as UInt8:
READ_TEMP = 0x2e
READ_PRESSURE = 2x34
class BMP183(spi_channel, delay):
worker begin(mode):
spi_channel.begin()
spi_channel.set_data_mode(MODE0)
await spi_channel.write("hello world")
worker _read_uint8_reg(reg as Register):
await spi_channel.write([0x80 | reg.value])
buf = Buffer(UInt8, 1)
await spi_channel.read(buf)
return buf[0]
worker _read_uint16_reg(reg as Register):
await spi_channel.write([0x80 | reg.value])
buf = Buffer(UInt8, 2)
await spi_channel.read(buf)
return buf[0] << 8 | buf[1]
worker _write_uint8_reg(reg as Register, value as UInt8):
await spi_channel.write([0x7f & reg.value, value])
func _write_control(cmd as ControlCommand):
return _write_uint8_reg(CONTROL, cmd.value)
worker get_temperature():
...
func get_pressure():
...
worker get_altitude(sea_level_pressure=101325):
pressure = await get_pressure();
# FIXME: This needs a conversion to float somewhere.
altitude = 44330 * (1 - (pressure / sea_level_pressure) ** 0.1903)
worker get_raw_temperature():
await spi_channel.select()
_write_control(READ_TEMP)
await delay.ms(5)
await _read_uint16_reg(TEMP_DATA)
await spi_channel.deselect()
func get_raw_pressure():
pass
import device/bmp183
import lpc1114
import format_writer
var spi_channel = lpc1114.spi_bus.create_channel(lpc1114.D1)
var sensor = bmp183.BMP183(spi_channel)
var format_writer = format_writer.FormatWriter(lpc1114.UART0)
loop:
var temp = await sensor.get_temperature()
var altitude = await sensor.get_altitude()
await lpc1114.format_writer.write("Temperature: ", temp, "\n")
await lpc1114.format_writer.write("Altitude: ", altitude, "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment