def test_ValidAccuraciesAreAccepted(self): try: PiADCInput([0], 12) PiADCInput([0], 14) PiADCInput([0], 16) PiADCInput([0], 18) except ValueError: self.fail("Valid Accuracies should be allowed")
def test_ADCConfigMethodReturnsCorrectConfigMask(self): lookup_table = [(0, 12, 0x90), (0, 14, 0x94), (0, 16, 0x98), (0, 18, 0x9C), (1, 12, 0xB0), (1, 14, 0xB4), (1, 16, 0xB8), (1, 18, 0xBC), (2, 12, 0xD0), (2, 14, 0xD4), (2, 16, 0xD8), (2, 18, 0xDC), (3, 12, 0xF0), (3, 14, 0xF4), (3, 16, 0xF8), (3, 18, 0xFC)] for i in range(0,2): for j in lookup_table: adc = PiADCInput([0], j[1]) self.assertEqual(j[2], adc._adcConfig(4*i + j[0])[1])
def _createADCSampler(self): # Need to prepare the config self.logger.info("Creating the ADC sampler") accuracy = self._config['input_accuracy'] channels = [] for input_config in self._config['input']: channels.append(input_config['pin']) self.logger.info(('Channel list', channels)) self._piADCInput = PiADCInput(channels, accuracy)
def test_ADCConfigMethodReturnsCorrectConfigMask(self): lookup_table = [(0, 12, 0x90), (0, 14, 0x94), (0, 16, 0x98), (0, 18, 0x9C), (1, 12, 0xB0), (1, 14, 0xB4), (1, 16, 0xB8), (1, 18, 0xBC), (2, 12, 0xD0), (2, 14, 0xD4), (2, 16, 0xD8), (2, 18, 0xDC), (3, 12, 0xF0), (3, 14, 0xF4), (3, 16, 0xF8), (3, 18, 0xFC)] for i in range(0, 2): for j in lookup_table: adc = PiADCInput([0], j[1]) self.assertEqual(j[2], adc._adcConfig(4 * i + j[0])[1])
class GPIODataSampleReader(SampleReader): ''' This implementation will read samples from the GPIO for the Pi-ADC ''' def __init__(self, configuration): self.logger = logging.getLogger(__name__) self._config = configuration self._createADCSampler() def _createADCSampler(self): # Need to prepare the config self.logger.info("Creating the ADC sampler") accuracy = self._config['input_accuracy'] channels = [] for input_config in self._config['input']: channels.append(input_config['pin']) self.logger.info(('Channel list', channels)) self._piADCInput = PiADCInput(channels, accuracy) def read_sample(self): samples = self._piADCInput.getSamples() self.logger.info(("Samples: ", samples)) # Need to create data points of the correct form datapoints = list() for channel in samples: # Need to find the relevant line in the config cfg = self._input_config_for_channel(channel) # And now create the data point dp = dict() dp['timestamp'] = time.time() dp['raw_value'] = samples[channel] dp['sensor_id'] = cfg['id'] dp['type'] = cfg['type'] datapoints.append(dp) return datapoints def _input_config_for_channel(self, channel): pins = map(lambda x: x['pin'], self._config['input']) try: idx = list(pins).index(channel) except: print("problem") return self._config['input'][idx]
class GPIODataSampleReader(SampleReader): ''' This implementation will read samples from the GPIO for the Pi-ADC ''' def __init__(self, configuration): self.logger = logging.getLogger(__name__) self._config = configuration self._createADCSampler() def _createADCSampler(self): # Need to prepare the config self.logger.info("Creating the ADC sampler") accuracy = self._config['input_accuracy'] channels = [] for input_config in self._config['input']: channels.append(input_config['pin']) self.logger.info(('Channel list', channels)) self._piADCInput = PiADCInput(channels, accuracy) def read_sample(self): samples = self._piADCInput.getSamples() self.logger.info(("Samples: ", samples)) # Need to create data points of the correct form datapoints = list() for channel in samples: # Need to find the relevant line in the config cfg = self._input_config_for_channel(channel) # And now create the data point dp = dict() dp['timestamp'] = time.time() dp['raw_value'] = samples[channel] dp['sensor_id'] = cfg['id'] dp['type'] = cfg['type'] datapoints.append(dp) return datapoints def _input_config_for_channel(self, channel): pins = map(lambda x : x['pin'], self._config['input']) try: idx = list(pins).index(channel) except: print("problem") return self._config['input'][idx]
def test_ADCConfigMethodReturnsCorrectAddresses(self): lookup_table = [0x68, 0x68, 0x68, 0x68, 0x69, 0x69, 0x69, 0x69] adc = PiADCInput([0], 12) for i in range(0, len(lookup_table)): self.assertEqual(lookup_table[i], adc._adcConfig(i)[0])
def test_DivisorMethodWorksCorrectly(self): lookup_table = [(12, 1), (14, 4), (16, 16), (18, 64)] for kv in lookup_table: adc = PiADCInput([0], kv[0]) self.assertEqual(kv[1], adc._divisor())
def test_ValidChannelsAreAccepted(self): try: PiADCInput([0, 1, 2, 3, 4, 5, 6, 7], 12) PiADCInput([3], 12) except ValueError: self.fail("Valid channels should be accepted")