Skip to content

Commit

Permalink
Start onewire
Browse files Browse the repository at this point in the history
  • Loading branch information
tannewt committed May 30, 2023
1 parent 410e29e commit 0dec240
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
6 changes: 5 additions & 1 deletion adafruit_circuitpyrate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Unsupported: b, $"""

modes = (
# ("1-WIRE", "onewire"),
("1-WIRE", "onewire"),
# ("UART", "uart"),
("I2C", "i2c"),
("SPI", "spi"),
Expand Down Expand Up @@ -89,6 +89,7 @@ class Mode:
def __init__(self, input, output):
self._input = input
self._output = output
self.macros = {}

self.pull_ok = False

Expand Down Expand Up @@ -127,6 +128,9 @@ def run_macro(self, number):
name, _ = self.macros[num]
self._print(f"{num}. {name}")
else:
if number not in self.macros:
self._print("Unknown macro, try ? or (0) for help")
return
_, func = self.macros[number]
func()

Expand Down
91 changes: 91 additions & 0 deletions adafruit_circuitpyrate/onewire.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from adafruit_circuitpyrate import Mode, BusWrite, BusRead
import adafruit_prompt_toolkit as prompt_toolkit

import array
import adafruit_onewire.bus

KNOWN_DEVICES = {
0x10: "DS18S20 High Prec Dig Therm",
0x28: "DS18B20 Prog Res Dig Therm",
0x22: "DS1822 Econo Dig Therm",
0x04: "DS2404 EconoRAM time Chip",
0x2D: "DS2431 1K EEPROM"
}

class OneWire(Mode):
name = "1-WIRE"

def __init__(self, pins, input, output):
super().__init__(input, output)

self.onewire = adafruit_onewire.bus.OneWireBus(pins["mosi"])

self.macros = {
51 : ("READ ROM (0x33) *for single device bus", self.read_rom),
# 85 : ("MATCH ROM (0x55) *followed by 64bit address", self.match_rom),
204: ("SKIP ROM (0xCC) *followed by command", self.skip_rom),
# 236: ("ALARM SEARCH (0xEC)", self.alarm_search),
240: ("SEARCH ROM (0xF0)", self.search_rom),
}

self.pull_ok = True

def deinit(self):
self.onewire.deinit()

def print_pin_functions(self):
self._print("- OWD - -")

def print_pin_directions(self):
self._print("I I I I")

def read_rom(self):
self.onewire.reset()
self._print("BUS RESET OK")
self.onewire.write(b"\x33")
buf = bytearray(8)
self.onewire.readinto(buf)
self._print("READ ROM (0x33):", end="")
for b in buf:
self._print(f" 0x{b:02X}", end="")
self._print()
if buf[0] in KNOWN_DEVICES:
self._print(KNOWN_DEVICES[buf[0]])
else:
self._print("Unknown device")

def skip_rom(self):
pass

def search_rom(self):
self._print("Macro 1WIRE address")
for i, device in enumerate(self.onewire.scan()):
self._print(f" {i}.", end="")
for b in device.rom:
self._print(f" 0x{b:02X}", end="")
self._print()
if device.rom[0] in KNOWN_DEVICES:
self._print(KNOWN_DEVICES[device.rom[0]])
else:
self._print("Unknown device")

def run_sequence(self, sequence):
for action in sequence:
if action == "START":
self.onewire.reset()
self._print("BUS RESET OK")
elif isinstance(action, BusWrite):
buf = bytearray(action.repeat)
self._print(f"WRITE:", end="")
for i in range(action.repeat):
buf[i] = action.value
self._print(f" 0x{buf[i]:02X}", end="")
self._print()
self.onewire.write(buf)
elif isinstance(action, BusRead):
buf = bytearray(action.repeat)
self._print("READ:", end="")
self.onewire.readinto(buf)
for i in range(action.repeat):
self._print(f" 0x{buf[i]:02X}", end="")
self._print()

0 comments on commit 0dec240

Please sign in to comment.