-
Notifications
You must be signed in to change notification settings - Fork 2
/
CatchExceptions4.py
44 lines (35 loc) · 969 Bytes
/
CatchExceptions4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ar - 18-05-2021
# import os
import time
# import smbus2
from smbus2 import SMBus
# Define I2C bus
DEVICE_BUS = 1
# Define device I2C slave address.
DEVICE_ADDR = 0x17
def settle():
time.sleep(0)
# Write byte to specified I2C register address
def putByte(RA, wbyte):
while True:
try:
with SMBus(DEVICE_BUS) as pbus:
pbus.write_byte_data(DEVICE_ADDR, RA, wbyte)
with SMBus(DEVICE_BUS) as gbus:
rbyte = gbus.read_byte_data(DEVICE_ADDR, RA)
if (wbyte) <= rbyte <= (wbyte):
print("OK ", wbyte, rbyte)
break
else:
# if rbyte < max((wbyte - 2),0):
raise ValueError
except ValueError:
print("Write:", wbyte, "!= Read:", rbyte, " Trying again")
while True:
putByte(0x18, 180)
# settle()
putByte(0x18, 0)
time.sleep(1)
# EOF