Skip to content

Commit

Permalink
control.gpio.pca953x: mask bits directly instead of using bitarray
Browse files Browse the repository at this point in the history
Use of bitarray is deprecated in Glasgow due to endian issues and doubts about the quality of it's C implementation.
  • Loading branch information
electroniceel committed Oct 3, 2019
1 parent 37c2b4e commit b5a7867
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions software/glasgow/applet/control/gpio/pca953x/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging
import asyncio
import argparse
from bitarray import bitarray

from .... import *
from ....interface.i2c_master import I2CMasterApplet
Expand Down Expand Up @@ -52,9 +51,10 @@ async def get_input_word(self):

async def get_input_bit(self, bitno):
inputval = await self.get_input_word()
ba = bitarray(endian='little')
ba.frombytes(bytes([inputval]))
return ba[bitno]
if inputval & (1 << bitno):
return 1
else:
return 0

async def get_dir_word(self):
"""a set bit means input"""
Expand All @@ -64,9 +64,10 @@ async def get_dir_word(self):
async def get_dir_bit(self, bitno):
"""a set bit means input"""
dirval = await self.get_dir_word()
ba = bitarray(endian='little')
ba.frombytes(bytes([dirval]))
return ba[bitno]
if dirval & (1 << bitno):
return 1
else:
return 0

async def set_dir_word(self, direction):
"""a set bit means input"""
Expand All @@ -75,30 +76,33 @@ async def set_dir_word(self, direction):
async def set_dir_bit(self, bitno, direction):
"""direction 1 means input"""
dirval = await self.get_dir_word()
ba = bitarray(endian='little')
ba.frombytes(bytes([dirval]))
ba[bitno] = direction
await self.set_dir_word(ba.tobytes()[0])
if direction:
dirval |= (1 << bitno)
else:
dirval &= ((1 << bitno) ^ 0xFF)
await self.set_dir_word(dirval)

async def get_output_word(self):
outval = await self._read_reg(REG_OUTPUT)
return outval

async def get_output_bit(self, bitno):
outval = await self.get_output_word()
ba = bitarray(endian='little')
ba.frombytes(bytes([outval]))
return ba[bitno]
if outval & (1 << bitno):
return 1
else:
return 0

async def set_output_word(self, output):
await self._write_reg(REG_OUTPUT, output)

async def set_output_bit(self, bitno, output):
outval = await self.get_output_word()
ba = bitarray(endian='little')
ba.frombytes(bytes([outval]))
ba[bitno] = output
await self.set_output_word(ba.tobytes()[0])
if output:
outval |= (1 << bitno)
else:
outval &= ((1 << bitno) ^ 0xFF)
await self.set_output_word(outval)


class ControlGPIOPCA953xApplet(I2CMasterApplet, name="control-gpio-pca953x"):
Expand Down

0 comments on commit b5a7867

Please sign in to comment.