Skip to content

Commit

Permalink
DM: added native neopix support
Browse files Browse the repository at this point in the history
  • Loading branch information
deanm1278 committed May 2, 2018
1 parent 3419df9 commit 3c079f0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
35 changes: 18 additions & 17 deletions adafruit_seesaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
from micropython import const
from adafruit_bus_device.i2c_device import I2CDevice
import digitalio
import struct

_STATUS_BASE = const(0x00)
_GPIO_BASE = const(0x01)
Expand Down Expand Up @@ -200,7 +201,8 @@ def direction(self, value):
def value(self):
if self._direction == digitalio.Direction.OUTPUT:
return self._value
return self._seesaw.digital_read(self._pin)
else:
return self._seesaw.digital_read(self._pin)

@value.setter
def value(self, val):
Expand Down Expand Up @@ -229,7 +231,7 @@ def pull(self, mode):
raise ValueError("Pull Down currently not supported")
elif mode == digitalio.Pull.UP:
self._seesaw.pin_mode(self._pin, self._seesaw.INPUT_PULLUP)
elif mode is None:
elif mode == None:
self._seesaw.pin_mode(self._pin, self._seesaw.INPUT)
else:
raise ValueError("Out of range")
Expand Down Expand Up @@ -321,13 +323,13 @@ def sw_reset(self):
def get_options(self):
buf = bytearray(4)
self.read(_STATUS_BASE, _STATUS_OPTIONS, buf, 4)
ret = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]
ret = struct.unpack(">I", buf)[0]
return ret

def get_version(self):
buf = bytearray(4)
self.read(_STATUS_BASE, _STATUS_VERSION, buf, 4)
ret = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]
ret = struct.unpack(">I", buf)[0]
return ret

def get_digitalio(self, pin):
Expand All @@ -348,25 +350,24 @@ def digital_write(self, pin, value):
def digital_read(self, pin):
if pin >= 32:
return self.digital_read_bulk_b((1 << (pin - 32))) != 0
return self.digital_read_bulk((1 << pin)) != 0
else:
return self.digital_read_bulk((1 << pin)) != 0

def digital_read_bulk(self, pins):
buf = bytearray(4)
self.read(_GPIO_BASE, _GPIO_BULK, buf)
#TODO: weird overflow error, fix
ret = ((buf[0] & 0xF) << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]
ret = struct.unpack(">I", buf)[0]
return ret & pins

def digital_read_bulk_b(self, pins):
buf = bytearray(8)
self.read(_GPIO_BASE, _GPIO_BULK, buf)
#TODO: weird overflow error, fix
ret = ((buf[4] & 0xF) << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]
ret = struct.unpack(">II", buf)[1]
return ret & pins


def set_GPIO_interrupts(self, pins, enabled):
cmd = bytearray([(pins >> 24), (pins >> 16), (pins >> 8), pins])
cmd = struct.pack(">I", pins)
if enabled:
self.write(_GPIO_BASE, _GPIO_INTENSET, cmd)
else:
Expand All @@ -390,7 +391,7 @@ def analog_read(self, pin):
raise ValueError("Invalid ADC pin")

self.read(_ADC_BASE, _ADC_CHANNEL_OFFSET + pin_mapping.index(pin), buf)
ret = (buf[0] << 8) | buf[1]
ret = struct.unpack(">H", buf)[0]
time.sleep(.001)
return ret

Expand All @@ -403,11 +404,11 @@ def touch_read(self, pin):
raise ValueError("Invalid touch pin")

self.read(_TOUCH_BASE, _TOUCH_CHANNEL_OFFSET + pin_mapping.index(pin), buf)
ret = (buf[0] << 8) | buf[1]
ret = struct.unpack(">H", buf)[0]
return ret

def pin_mode_bulk(self, pins, mode):
cmd = bytearray([(pins >> 24), (pins >> 16), (pins >> 8), pins])
cmd = struct.pack(">I", pins)
if mode == self.OUTPUT:
self.write(_GPIO_BASE, _GPIO_DIRSET_BULK, cmd)
elif mode == self.INPUT:
Expand All @@ -431,7 +432,7 @@ def pin_mode_bulk_b(self, pins, mode):
self.write(_GPIO_BASE, _GPIO_BULK_SET, cmd)

def digital_write_bulk(self, pins, value):
cmd = bytearray([(pins >> 24), (pins >> 16), (pins >> 8), pins])
cmd = struct.pack(">I", pins)
if value:
self.write(_GPIO_BASE, _GPIO_BULK_SET, cmd)
else:
Expand Down Expand Up @@ -511,7 +512,7 @@ def eeprom_read8(self, addr):
return self.read8(_EEPROM_BASE, addr)

def uart_set_baud(self, baud):
cmd = bytearray([(baud >> 24), (baud >> 16), (baud >> 8), baud])
cmd = struct.pack(">I", baud)
self.write(_SERCOM0_BASE, _SERCOM_BAUD, cmd)

def write8(self, reg_base, reg, value):
Expand All @@ -525,7 +526,7 @@ def read8(self, reg_base, reg):
def read(self, reg_base, reg, buf, delay=.001):
self.write(reg_base, reg)
if self._drdy != None:
while self._drdy.value is False:
while self._drdy.value == False:
pass
else:
time.sleep(delay)
Expand All @@ -538,7 +539,7 @@ def write(self, reg_base, reg, buf=None):
full_buffer += buf

if self._drdy != None:
while self._drdy.value is False:
while self._drdy.value == False:
pass
with self.i2c_device as i2c:
i2c.write(full_buffer)
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Uncomment the below if you use native CircuitPython modules such as
# digitalio, micropython and busio. List the modules you use. Without it, the
# autodoc module docs will fail to generate with a warning.
autodoc_mock_imports = ["adafruit_bus_device", "micropython", "digitalio"]
autodoc_mock_imports = ["adafruit_bus_device", "micropython", "digitalio", "struct"]

intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'Register': ('https://circuitpython.readthedocs.io/projects/register/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

Expand Down

0 comments on commit 3c079f0

Please sign in to comment.