From 71fa131ef9a9fcafdc456bef2fb1a68d40254d59 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 11 May 2019 20:21:57 +0200 Subject: [PATCH] machine/dummy: add hooks to override peripheral methods --- src/machine/machine_dummy.go | 72 ++++++++++++++++++++++++++++++------ src/machine/spi.go | 2 +- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/machine/machine_dummy.go b/src/machine/machine_dummy.go index 6d180a594..5b70b5532 100644 --- a/src/machine/machine_dummy.go +++ b/src/machine/machine_dummy.go @@ -12,29 +12,77 @@ const ( ) // Fake LED numbers, for testing. -const ( - LED = LED1 - LED1 = 0 - LED2 = 0 - LED3 = 0 - LED4 = 0 +var ( + LED uint8 = LED1 + LED1 uint8 = 0 + LED2 uint8 = 0 + LED3 uint8 = 0 + LED4 uint8 = 0 ) // Fake button numbers, for testing. -const ( - BUTTON = BUTTON1 - BUTTON1 = 0 - BUTTON2 = 0 - BUTTON3 = 0 - BUTTON4 = 0 +var ( + BUTTON uint8 = BUTTON1 + BUTTON1 uint8 = 5 + BUTTON2 uint8 = 6 + BUTTON3 uint8 = 7 + BUTTON4 uint8 = 8 +) + +// Fake SPI interfaces, for testing. +var ( + SPI0 = SPI{0} +) + +var ( + GPIOConfigure func(pin uint8, config GPIOConfig) + GPIOSet func(pin uint8, value bool) + GPIOGet func(pin uint8) bool + SPIConfigure func(bus uint8, sck uint8, mosi uint8, miso uint8) + SPITransfer func(bus uint8, w uint8) uint8 ) func (p GPIO) Configure(config GPIOConfig) { + if GPIOConfigure != nil { + GPIOConfigure(p.Pin, config) + } } func (p GPIO) Set(value bool) { + if GPIOSet != nil { + GPIOSet(p.Pin, value) + } } func (p GPIO) Get() bool { + if GPIOGet != nil { + return GPIOGet(p.Pin) + } return false } + +type SPI struct { + Bus uint8 +} + +type SPIConfig struct { + Frequency uint32 + SCK uint8 + MOSI uint8 + MISO uint8 + Mode uint8 +} + +func (spi SPI) Configure(config SPIConfig) { + if SPIConfigure != nil { + SPIConfigure(spi.Bus, config.SCK, config.MOSI, config.MISO) + } +} + +// Transfer writes/reads a single byte using the SPI interface. +func (spi SPI) Transfer(w byte) (byte, error) { + if SPITransfer != nil { + return SPITransfer(spi.Bus, w), nil + } + return 0, nil +} diff --git a/src/machine/spi.go b/src/machine/spi.go index 83f16d5ed..bdbd87e42 100644 --- a/src/machine/spi.go +++ b/src/machine/spi.go @@ -1,4 +1,4 @@ -// +build nrf stm32f103xx atsamd21g18a +// +build js,wasm nrf stm32f103xx atsamd21g18a package machine