Converting a byte or hex value from a single character sliced from a numeric string #15717
-
Folks, string = "745261300000"
for a in range(0, len(string)+1): # these two lines slices each digit and prints them one by one OK
print(string[a:a+1])
a=0 # sliced digit position pointer
for y in range(32, 40): # Select Relay Matrix board (PCF addresses are 32 to 39)
pcf = pcf8574.PCF8574(i2c, y) # set this address to use
b=(string[a:a+1]) # slice one character at a time from the string
print(b) # print it
pcf.pin(b, 1) # set this bit of this PCF board (this line isnt working as I hoped)
# pcf.pin(7, 1) # this works OK
a=a+1 # increment character pointer pcf.pin(b, 1) is the problem line. The format of b is not valid. I saw this on a thread and thought that this would convert b into a hex value.......but it isnt seeing b as a single character string I think I need to convert the single sliced character from the string to a byte in an array. I have tried various ways to do this, but none work to provide a valid 'b' for the I2C pcf.pin(b, 1) command syntax. What is the correct way to do this conversion? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
You are rather overcomplicating things. I think you will find |
Beta Was this translation helpful? Give feedback.
-
I would make a class. To save lines, inheritance could be used or to make it easier to understand, use composition. from machine import I2C
from pcf8574 import PCF8574
# with composition
class PcfSender:
def __init__(self, i2c, address=0x20):
self.pcf = PCF8574(i2c, address)
def set_pins(digits: str):
pin_numbers = tuple(map(int, digits))
if any(not pin < 8 for pin in pin_numbers):
raise RuntimeError("Pin number too high")
for pin in pin_numbers:
self.pcf.pin(pin, 1)
i2c = I2C(0)
# 0x20 seems to be the default address of PCF if
# A0-2 is not set
pcf_sender = PcfSender(i2c, 0x20)
# with inheritance
class PcfSender(PCF8574):
def set_pins(digits: str):
pin_numbers = tuple(map(int, digits))
if any(not pin < 8 for pin in pin_numbers):
raise RuntimeError("Pin number too high")
for pin in pin_numbers:
self.pcf.pin(pin, 1)
i2c = I2C(0)
pcf_sender = PcfSender(i2c, 0x20) This code applies for each single character the pin_numbers = tuple(map(int, digits)) |
Beta Was this translation helpful? Give feedback.
-
Btw. there is a flaw. For each PCF you only have 8 ports. This means if you want to set the Pins of one PCF, then you could only have a maximum of 8 digits. But your example holds 12 digits. Do you have more than one PCF and want to set them? |
Beta Was this translation helpful? Give feedback.
-
This is working for me. Perhaps not optimal, but working. I will clean it up as I learn more. Thanks. string = "745261380000"
a=0
for y in range(32, 40, 2): # Select Relay Matrix board (PCF address)
b1=(string[a:a+1])
l = ord(b1)
b = bytearray(l)
b[0]=ord(b1)
if b[0]-48 > 7:
pcf = pcf8574.PCF8574(i2c, y)
pcf.pin(b[0]-49, 1)
elif b[0]-48 > 0:
pcf = pcf8574.PCF8574(i2c, y)
pcf.pin(b[0]-49, 1)
elif b[0]-48 <= 0:
y += 2
pcf = pcf8574.PCF8574(i2c, y)
pcf.pin(b[0]-42, 1)
if w == 1 and y >= 35:
break
a+=1
''' |
Beta Was this translation helpful? Give feedback.
-
I finished up needing to utilise some other digits as well as 0-9 in my string. |
Beta Was this translation helpful? Give feedback.
You are rather overcomplicating things. I think you will find
int(b)
will fix your problem.