From 1345bc21614e465c7ce2945e0ac623ec58f186a1 Mon Sep 17 00:00:00 2001 From: Quentin Smith Date: Thu, 7 Jan 2021 02:48:35 -0500 Subject: [PATCH] p1am: documentation and example program --- Makefile | 2 ++ README.md | 1 + examples/p1am/main.go | 74 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 examples/p1am/main.go diff --git a/Makefile b/Makefile index 6a69e1f..0417ab5 100644 --- a/Makefile +++ b/Makefile @@ -95,6 +95,8 @@ smoke-test: @md5sum ./build/test.hex tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mpu6050/main.go @md5sum ./build/test.hex + tinygo build -size short -o ./build/test.hex -target=p1am-100 ./examples/p1am/main.go + @md5sum ./build/test.hex tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setbuffer/main.go @md5sum ./build/test.hex tinygo build -size short -o ./build/test.hex -target=microbit ./examples/pcd8544/setpixel/main.go diff --git a/README.md b/README.md index 34398ff..a2da2a9 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ The following 58 devices are supported. | [Microphone - PDM](https://cdn-learn.adafruit.com/assets/assets/000/049/977/original/MP34DT01-M.pdf) | I2S/PDM | | [MMA8653 accelerometer](https://www.nxp.com/docs/en/data-sheet/MMA8653FC.pdf) | I2C | | [MPU6050 accelerometer/gyroscope](https://store.invensense.com/datasheets/invensense/MPU-6050_DataSheet_V3%204.pdf) | I2C | +| [P1AM-100 Base Controller](https://facts-engineering.github.io/modules/P1AM-100/P1AM-100.html) | SPI | | [PCD8544 display](http://eia.udg.edu/~forest/PCD8544_1.pdf) | SPI | | [Resistive Touchscreen (4-wire)](http://ww1.microchip.com/downloads/en/Appnotes/doc8091.pdf) | GPIO | | [Semihosting](https://wiki.segger.com/Semihosting) | Debug | diff --git a/examples/p1am/main.go b/examples/p1am/main.go new file mode 100644 index 0000000..6fe18ae --- /dev/null +++ b/examples/p1am/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "fmt" + "machine" + "time" + + "tinygo.org/x/drivers/p1am" +) + +func main() { + for { + if err := loop(); err != nil { + fmt.Printf("loop failed, retrying: %v\n", err) + time.Sleep(500 * time.Millisecond) + } + } +} +func loop() error { + led := machine.LED + led.Configure(machine.PinConfig{Mode: machine.PinOutput}) + sw := machine.SWITCH + sw.Configure(machine.PinConfig{Mode: machine.PinInput}) + + if err := p1am.Controller.Initialize(); err != nil { + return fmt.Errorf("initializing controller: %w", err) + } + + version, err := p1am.Controller.Version() + if err != nil { + return fmt.Errorf("fetching base controller version: %w", err) + } + fmt.Printf("Base controller version: %d.%d.%d\n", version[0], version[1], version[2]) + + for i := 1; i <= p1am.Controller.Slots; i++ { + slot := p1am.Controller.Slot(i) + fmt.Printf("Slot %d: ID 0x%08x, Props %+v\n", i, slot.ID, slot.Props) + } + + slot1 := p1am.Controller.Slot(1) + var lastInput uint32 + state := sw.Get() + for { + if active, err := p1am.Controller.Active(); err != nil || !active { + return fmt.Errorf("controller active %v: %v", active, err) + } + if state != sw.Get() { + state = sw.Get() + fmt.Printf("New switch state: %v\n", state) + if slot1.Props.DO > 0 { + if err := slot1.Channel(1).WriteDiscrete(state); err != nil { + return err + } + } + } + if slot1.Props.DI > 0 { + sstate, err := slot1.ReadDiscrete() + if err != nil { + return fmt.Errorf("reading slot: %w", err) + } + if sstate != lastInput { + lastInput = sstate + fmt.Printf("new DI state: %#b\n", sstate) + } + } + if state { + led.High() + } else { + led.Low() + } + time.Sleep(time.Millisecond * 10) + } + return nil +}