tinygo-org / drivers

TinyGo drivers for sensors, displays, wireless adaptors, and other devices that use I2C, SPI, GPIO, ADC, and UART interfaces.
https://tinygo.org
BSD 3-Clause "New" or "Revised" License
616 stars 195 forks source link

DHTXX driver does not work on Arduino Uno #255

Open adrianbn opened 3 years ago

adrianbn commented 3 years ago

When trying to build the example for the dht driver (https://github.com/tinygo-org/drivers/blob/release/examples/dht/main.go) on Windows I get the following error:

> tinygo flash -target arduino -port COM3 main.go
# tinygo.org/x/drivers/dht
..\..\go\pkg\mod\tinygo.org\x\drivers@v0.15.1\dht\thermometer.go:160:10: UART1 not declared by package machine
..\..\go\pkg\mod\tinygo.org\x\drivers@v0.15.1\dht\thermometer.go:161:16: UART1 not declared by package machine

Taking a quick look at the code for the driver, it tries to access UART1 https://github.com/tinygo-org/drivers/blob/release/dht/thermometer.go#L160, which according to the machine package for arduino is not defined (https://tinygo.org/microcontrollers/machine/arduino/).

I tried the dirty hack of using UART or UART0 as defined in the machine package for arduino, but they don't have a method Interrupt as used by the driver.

Cheers!

sago35 commented 3 years ago

It's not a good idea to use machine.UART1 in dht/thermometer.go. It is better to pass it from dht.New(), for example, so that it does not depend on a specific UART.

adrianbn commented 3 years ago

Not sure I follow. Do you mean pass the UART from the device into dht.New() and then trickle it down to receiveSignals? Even then, it doesn't seem like the arduino UART in the machine package supports Interrupt(), right?

mikespook commented 3 years ago

To the boards without UART, it causes a problem to build. This is issue.

aykevl commented 3 years ago

The correct fix would be to use the runtime/interrupt package:

mask := interrupt.Disable()
// do something with interrupts disabled
interrupt.Restore(mask)

That seems reasonable for the DHTXX driver as it depends on precise timings.