This is what I'm doing:
import spidev
import onionGpio
...
def __init__(self):
try:
self.spi = spidev.SpiDev(32766,1)
self.spi.max_speed_hz=100000;
self.spi.mode = 1;
self.ready = onionGpio.OnionGpio(0,1);
r = self.ready.setInputDirection();
...
def _drdy(self):
try:
while self.ready.getValue()[0] == '1':
continue;
except:
self.ready._freeGpio();
print(traceback.format_exc());
def getReadings(self):
self._drdy();
v1 = self.spi.xfer3([self.CMD_WMUX,0x00,self.MUX_AN0_AN1,self.CMD_SYNC,self.CMD_WAKEUP,self.CMD_RDATA],3);
self._drdy();
v2 = self.spi.xfer3([self.CMD_WMUX,0x00,self.MUX_AN2_AN3,self.CMD_SYNC,self.CMD_WAKEUP,self.CMD_RDATA],3);
self._drdy();
v3 = self.spi.xfer3([self.CMD_WMUX,0x00,self.MUX_AN4_AN5,self.CMD_SYNC,self.CMD_WAKEUP,self.CMD_RDATA],3);
self._drdy();
v4 = self.spi.xfer3([self.CMD_WMUX,0x00,self.MUX_AN6_AN7,self.CMD_SYNC,self.CMD_WAKEUP,self.CMD_RDATA],3);
return [self._getVal(v1),self._getVal(v2),self._getVal(v3),self._getVal(v4)];
...
#### THE LOOP ####
....
while True:
try:
if not lock:
time.sleep(0.005);
weights = adc.getReadings();
display_count += 1;
if display_count > 20:
display_count = 0;
client.publish(MQTT_TOPIC,"{\"weight\":%6.2f,\"adc1\":%d,\"adc2\":%d,\"adc3\":%d,\"adc4\":%d}" % (weight,weights[0],weights[1],weights[2],weights[3]));
except KeyboardInterrupt:
print(stamp()+" :Exiting, interrupt");
sys.exit(1);
except:
print(traceback.format_exc());