mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-08 08:53:40 +00:00
first implementation of 1-wire protocol (#505)
onewire: initial implementation for protocol and ds18b20 device
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/onewire"
|
||||
|
||||
"tinygo.org/x/drivers/ds18b20"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Define pin for DS18B20
|
||||
pin := machine.D2
|
||||
|
||||
ow := onewire.New(pin)
|
||||
romIDs, err := ow.Search(onewire.SEARCH_ROM)
|
||||
if err != nil {
|
||||
println(err)
|
||||
}
|
||||
sensor := ds18b20.New(ow)
|
||||
|
||||
for {
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
println()
|
||||
println("Device:", machine.Device)
|
||||
|
||||
println()
|
||||
println("Request Temperature.")
|
||||
for _, romid := range romIDs {
|
||||
println("Sensor RomID: ", hex.EncodeToString(romid))
|
||||
sensor.RequestTemperature(romid)
|
||||
}
|
||||
|
||||
// wait 750ms or more for DS18B20 convert T
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
println()
|
||||
println("Read Temperature")
|
||||
for _, romid := range romIDs {
|
||||
raw, err := sensor.ReadTemperatureRaw(romid)
|
||||
if err != nil {
|
||||
println(err)
|
||||
}
|
||||
println()
|
||||
println("Sensor RomID: ", hex.EncodeToString(romid))
|
||||
println("Temperature Raw value: ", hex.EncodeToString(raw))
|
||||
|
||||
t, err := sensor.ReadTemperature(romid)
|
||||
if err != nil {
|
||||
println(err)
|
||||
}
|
||||
println("Temperature in celsius milli degrees (°C/1000): ", t)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"time"
|
||||
"tinygo.org/x/drivers/onewire"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
pin := machine.D2
|
||||
|
||||
ow := onewire.New(pin)
|
||||
|
||||
for {
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
println()
|
||||
println("Device:", machine.Device)
|
||||
|
||||
romIDs, err := ow.Search(onewire.SEARCH)
|
||||
if err != nil {
|
||||
println(err)
|
||||
}
|
||||
for _, romid := range romIDs {
|
||||
println(hex.EncodeToString(romid))
|
||||
}
|
||||
|
||||
if len(romIDs) == 1 {
|
||||
// only 1 device on bus
|
||||
r, err := ow.ReadAddress()
|
||||
if err != nil {
|
||||
println(err)
|
||||
}
|
||||
println(hex.EncodeToString(r))
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user