diff --git a/tools/power_monitor/i2c.py b/tools/power_monitor/i2c.py index 78925b171..4d5ac1061 100644 --- a/tools/power_monitor/i2c.py +++ b/tools/power_monitor/i2c.py @@ -5,7 +5,7 @@ # May be combined with pyftdi at a later date # - please do not mix Tintin code in -from pyftdi.pyftdi import ftdi +from pyftdi import ftdi from array import array as Array class MPSSE(ftdi.Ftdi): @@ -152,7 +152,7 @@ def SendNacks(self): # Close the I2C when done def Close(self): - self.set_bitmode(0, self.BITMODE_RESET) + self.set_bitmode(0, self.BitMode.RESET) self.close() # Set the low bit pins high/low @@ -246,7 +246,7 @@ def _build_block_buffer(self, cmd, data, size): # append data input only if write if cmd == self.tx or cmd == self.txrx: - buf.append(ord(data[k])) + buf.append(data[k] if isinstance(data[k], int) else ord(data[k])) k += 1 # In I2C mode clock one ACK bit @@ -273,7 +273,7 @@ def _internal_read(self, size): buf = Array('B') while n < size: rxsize = size - n - rxsize - min(self.I2C_TRANSFER_SIZE, rxsize) + rxsize = min(self.I2C_TRANSFER_SIZE, rxsize) # buf not used by build_block when reading data = self._build_block_buffer(self.rx, buf, rxsize) @@ -301,7 +301,7 @@ def _ftdi_raw_read(self, buf, size): while n < size: str_buf = self.read_data(size) for s in str_buf: - buf.append(ord(s)) + buf.append(s if isinstance(s, int) else ord(s)) r = len(buf) if r < 0: break diff --git a/tools/power_monitor/ina226.py b/tools/power_monitor/ina226.py index 021b4b1dc..d79f368a7 100644 --- a/tools/power_monitor/ina226.py +++ b/tools/power_monitor/ina226.py @@ -40,7 +40,7 @@ def __init__(self, name, address, max_i, shunt_r): max_i /= self.UNITS_SCALING self.calibration_value = 0.00512 / ((max_i / (1 << 15)) * shunt_r) - self.calibration_value = min(self.calibration_value, 0xFFFF) + self.calibration_value = int(min(self.calibration_value, 0xFFFF)) self.current_lsb = 0.00512 / (shunt_r * self.calibration_value) self.current_lsb *= self.UNITS_SCALING diff --git a/tools/power_monitor/mcp23009.py b/tools/power_monitor/mcp23009.py index 163a05806..08ca00c2d 100644 --- a/tools/power_monitor/mcp23009.py +++ b/tools/power_monitor/mcp23009.py @@ -44,7 +44,7 @@ def _i2cWrite8BitReg(self, regAddress, regValue): writeString = pack('>BBB', self.i2cAddress, regAddress, regValue) self.i2cBus.Write(writeString) if self.i2cBus.GetAck() != ACK: - print "NO ACK RECEIVED w0" + print("NO ACK RECEIVED w0") #self.i2cBus.Stop() #raise Exception("No ack received for command string %s" % writeString) @@ -56,13 +56,13 @@ def _i2cRead8BitReg(self, regAddress): writeString = pack('>BB', self.i2cAddress, regAddress) self.i2cBus.Write(writeString) if self.i2cBus.GetAck() != ACK: - print "NO ACK RECEIVED r1" + print("NO ACK RECEIVED r1") self.i2cBus.Start() writeString = pack('B', (self.i2cAddress + 0x01)) self.i2cBus.Write(writeString) if self.i2cBus.GetAck() != ACK: - print "NO ACK RECEIVED r3" + print("NO ACK RECEIVED r3") self.i2cBus.SendNacks() data = self.i2cBus.Read(1) @@ -74,7 +74,7 @@ def _i2cRead8BitReg(self, regAddress): def setButtons(self, back=False, up=False, down=False, select=False): # read the current GPIO register and mask out the buttons curGPIO = unpack('>B',self._i2cRead8BitReg(REG_GPIO))[0] | 0x0f - print "Before - setButton: 0x%x" % curGPIO + print("Before - setButton: 0x%x" % curGPIO) if back: curGPIO &= 0xf7 if up: @@ -83,7 +83,7 @@ def setButtons(self, back=False, up=False, down=False, select=False): curGPIO &= 0xfd if down: curGPIO &= 0xfe - print "After - setButton: 0x%x" % curGPIO + print("After - setButton: 0x%x" % curGPIO) self._i2cWrite8BitReg(REG_GPIO, curGPIO) def configureGPIODirection(self, gpio_mask, as_output=True): @@ -98,7 +98,7 @@ def configureGPIODirection(self, gpio_mask, as_output=True): self._i2cWrite8BitReg(REG_IODIR, new_gpiodir) gpiodir = unpack('>B', self._i2cRead8BitReg(REG_IODIR))[0] - print "REG_IODIR = 0x%x" % gpiodir + print("REG_IODIR = 0x%x" % gpiodir) def setUsbChargeEn(self, chargeEnable=False): usb_en_mask = 0x10 @@ -107,12 +107,12 @@ def setUsbChargeEn(self, chargeEnable=False): # read the current GPIO register and mask out the USB V+ En curGPIO = unpack('>B', self._i2cRead8BitReg(REG_GPIO))[0] & 0xEF - print "Before - setUsbChargeEn: 0x%x" % curGPIO + print("Before - setUsbChargeEn: 0x%x" % curGPIO) if chargeEnable: curGPIO |= usb_en_mask - print "After - setUsbChargeEn 0x%x" % curGPIO + print("After - setUsbChargeEn 0x%x" % curGPIO) self._i2cWrite8BitReg(REG_GPIO, curGPIO) @@ -132,7 +132,7 @@ def setAccessoryPullup(self, pullupEnable=False): self._i2cWrite8BitReg(REG_GPIO, curGPIO) curGPIO = unpack('>B', self._i2cRead8BitReg(REG_GPIO))[0] - print "REG_GPIO = 0x%x" % curGPIO + print("REG_GPIO = 0x%x" % curGPIO) def reset(self): # this is the reset sequence @@ -150,4 +150,4 @@ def reset(self): def readRegs(self): for reg in range(0,11): - print "%x: %x" %(reg, unpack('>B',self._i2cRead8BitReg(reg))[0]) + print("%x: %x" %(reg, unpack('>B',self._i2cRead8BitReg(reg))[0])) diff --git a/tools/power_monitor/power_monitor.py b/tools/power_monitor/power_monitor.py index be018c5b4..23d468b58 100755 --- a/tools/power_monitor/power_monitor.py +++ b/tools/power_monitor/power_monitor.py @@ -94,8 +94,8 @@ def graph_function(self, read_input, count): self.graph_data = self.np.roll(self.graph_data, -1) self.graph_data[self.roll_at - 1] = read_input[1] if count % self.samples_to_batch == 0: - print "%.3f secs to collect last %d samples" % \ - (time.time() - self.start_time, self.samples_to_batch) + print("%.3f secs to collect last %d samples" % \ + (time.time() - self.start_time, self.samples_to_batch)) self.plt.clf() self.plt.plot(list(self.graph_data), 'b') avg_all = "Avg: %.3f mA" % (self.np.average(self.graph_data) / 1000) @@ -151,8 +151,8 @@ def graph_function(self, read_input, count): self.graph_data = self.np.roll(self.graph_data, -1) self.graph_data[self.roll_at - 1] = read_input[1] if count % self.samples_to_batch == 0: - print "%.3f secs to collect last %d samples" % \ - (time.time() - self.start_time, self.samples_to_batch) + print("%.3f secs to collect last %d samples" % \ + (time.time() - self.start_time, self.samples_to_batch)) avg_all = "Avg: %.3f ma" % (self.np.average(self.graph_data) / 1000) avg_last_set = "Last Collection Avg: %.3f mA %s" % \ (self.np.average(self.graph_data[-100:])/1000, avg_all) @@ -216,8 +216,8 @@ def enabled_bool(str): if args.rails: for r in args.rails: if r not in RailNames: - print 'Rail "{}" not valid for platform "{}"'.format(r, args.platform) - print 'Valid rails are: {}'.format(RailNames) + print('Rail "{}" not valid for platform "{}"'.format(r, args.platform)) + print('Valid rails are: {}'.format(RailNames)) sys.exit(1) # local graphing setup @@ -287,14 +287,14 @@ def read_currents(): return read_tuple if args.outfile: - with open(args.outfile, 'wb') as csvfile: + with open(args.outfile, 'w', newline='') as csvfile: powercsv = csv.writer(csvfile) powercsv.writerow(args.rails) powercsv.writerow(read_currents()) while args.rails and args.continuous: powercsv.writerow(read_currents()) else: - print args.rails + print(args.rails) while args.continuous: readings = read_currents() count += 1 @@ -304,6 +304,6 @@ def read_currents(): for rail in args.rails: totals[rail] += readings[args.rails.index(rail)+1] averages[rail] = totals[rail]/count - print averages + print(averages) else: - print readings + print(readings)