def get_adc_devices(config, bus): '''Return MCP342x devices based on config settings''' r = {} sec = 'daemon' for comp in ('x', 'y', 'z', 'sensor_temperature'): if config.has_option(sec, comp + '_address'): if not config.has_option(sec, comp + '_channel'): raise Exception('Option ' + comp + '_channel is required in section ' + sec) address = awn.safe_eval(config.get(sec, comp + '_address')) channel = awn.safe_eval(config.get(sec, comp + '_channel')) # Some devices were constructed with single-ended ADC # boards. Enable a pseudo double-ended mode where IN+ and # IN- are measured by independed single-ended ADC # channels. pseudo_de = False if hasattr(address, '__iter__') or hasattr(channel, '__iter__'): if (not hasattr(address, '__iter__') or not hasattr(channel, '__iter__') or len(address) != 2 or len(channel) != 2): raise Exception('Pseudo double-ended configuration ' + 'requires two values for both ' + 'address and channe') if comp == 'sensor_temperature': raise Exception('Pseudo double-ended configuration ' + ' for sensor temperature is ' + 'not supported') pseudo_de = True adc = MCP342x(bus, address=0, channel=0, resolution=18, gain=1) for opt in ('resolution', 'gain', 'scale_factor', 'offset'): if config.has_option(sec, comp + '_' + opt): v = awn.safe_eval(config.get(sec, comp + '_' + opt)) getattr(adc, 'set_' + opt)(v) if pseudo_de: adc.set_address(address[0]) adc.set_channel(channel[0]) adc2 = copy.copy(adc) adc2.set_address(address[1]) adc2.set_channel(channel[1]) # Offset must be zero otherwise it will be removed later adc2.set_offset(0) r[comp + '_ref'] = adc2 else: adc.set_address(address) adc.set_channel(channel) r[comp] = adc return r
def safe_eval_mull_100(s): return 100 * awn.safe_eval(s)
# If the argparse type conversion was employed then convert # back to a string s = str(s) logger.debug("value: " + s) # Parse struct format string into order, quantity, type pfmt = awn.eeprom.parse_unpack_format(eeprom[k]['format']) if pfmt[2] == 's' and len(s) > pfmt[1]: raise Exception('Argument for ' + k + ' too long') if pfmt[2] in ('c', 's', 'p'): # String data data = s else: # Convert into numeric data data = awn.safe_eval(s) if pfmt[1] > 1 and pfmt[2] != 's': # Multiple values required struct.pack_into(eeprom[k]['format'], eeprom_data, eeprom[k]['address'], *data) else: struct.pack_into(eeprom[k]['format'], eeprom_data, eeprom[k]['address'], data) eeprom_image_filename = args.file + '.bin' fh = open(eeprom_image_filename, 'wb') fh.write(eeprom_data) fh.close() print('Wrote ' + str(eeprom_size) + ' bytes to ' + eeprom_image_filename)
# back to a string s = str(s) m = re.match('^eeprom_(.*)', k) if m: cmd['name'] = 'write_eeprom' eeprom_setting = m.group(1) # Parse struct format string into order, quantity, type pfmt = eeprom.parse_unpack_format( \ eeprom.eeprom[eeprom_setting]['format']) if pfmt[2] in ('c', 's', 'p'): # String data data = s else: # Convert into numeric data data = awn.safe_eval(s) # Pack data into suitable bytearrays matching the EEPROM layout if pfmt[1] > 1 and pfmt[2] not in ('s', 'p'): # Multiple values required eeprom_data = struct.pack(eeprom.eeprom[eeprom_setting]['format'], *data) else: eeprom_data = struct.pack(eeprom.eeprom[eeprom_setting]['format'], data) # Convert the bytewise values into a string, remembering to # prepend the address cmd['value'] = str(eeprom.eeprom[eeprom_setting]['address']) + \ ',' + ','.join([str(ord(x)) for x in eeprom_data]) else: