예제 #1
0
    def initialise(self, ledmode=MODE_BLINK):
        with self.lock:
            if ledmode == "BLINK": ledmode = MODE_BLINK
            elif ledmode == "ROTATE": ledmode = MODE_ROTATE
            elif ledmode == "DEBUG": ledmode = MODE_DEBUG
            elif ledmode == "OFF": ledmode = MODE_OFF
            elif ledmode == "DUMMY": pass

            if not ledmode in [
                    MODE_OFF, MODE_BLINK, MODE_ROTATE, MODE_DEBUG, "DUMMY"
            ]:
                raise Exception("An invalid mode was selected for LED_Control")
            self.ledmode = ledmode
            if not self.ledmode == "DUMMY":
                try:
                    self.i2cbus = serbus.I2CDev(I2C_DEV_NUM)
                    self.i2cbus.open()
                    self.i2cbus.write(I2C_ADDRESS,
                                      [self.ledmode, 0x00, 0x00, 0x00])
                except IOError:
                    logging.exception(
                        "Exception when trying to change LED colors")
            if ledmode in [MODE_BLINK, MODE_ROTATE]:
                self.siren.setmode("ON")
            else:
                self.siren.setmode("OFF")
            self.initialised = True
예제 #2
0
# i2c_htu21d.py
# Example of reading data from the HTU21D I2C temp/humidity sensor
import serbus, time

htu21d_bus = 1  # Connected to /dev/i2c-1
htu21d_addr = 0x40  # HTU21D slave address
htu21d_cmd_temp = 0xe3  # Command to read temperature
htu21d_cmd_rh = 0xe5  # Command to read relative humidity

bus = serbus.I2CDev(1)
bus.open()


def getTemp():
    # Read the 3 bytes of data:
    msb, lsb, crc = bus.readTransaction(htu21d_addr, htu21d_cmd_temp, 3)
    # The crc (cyclic redundancy check) can be used to verify the data was
    # received without error - ignore it here
    # Combine the high and low bytes:
    raw_value = (msb << 8) | lsb
    # Clear the two status bits (see datasheet):
    raw_value &= ~0b11
    # Convert to Celsius and return (conversion from datasheet):
    return -46.85 + 175.72 * (raw_value / 65536.0)


def getRH():
    msb, lsb, crc = bus.readTransaction(htu21d_addr, htu21d_cmd_rh, 3)
    raw_value = (msb << 8) | lsb
    raw_value &= ~0b11
    # Convert to %RH and return (conversion from datasheet):
예제 #3
0
# i2c_read.py
# Write data to an I2C EEPROM (e.g. 24LC256) then reading it back
import serbus, time

eeprom_addr = 0x50  # I2C slave address of EEPROM
start_msb = 0x00  # High byte of location in EEPROM to write/read
start_lsb = 0x00  # Low byte of location in EEPROM to write/read

data_to_write = range(10)

# Create an I2CDev instance for interfacing to /dev/i2c-2:
bus = serbus.I2CDev(2)
bus.open()

print "Writing data: {}".format(data_to_write)
# Write the data to the EEPROM:
bus.write(eeprom_addr, [start_msb, start_lsb] + data_to_write)
# The I2C write is asynchronous - give it a bit of time to complete:
time.sleep(0.01)  # 10ms should be more than enough

# Read the data from the EEPROM:
bus.write(eeprom_addr, [start_msb, start_lsb])
read_data = bus.read(0x50, len(data_to_write))
print "Data read: {}".format(read_data)

if read_data == data_to_write:
    print "EEPROM write successful!"
else:
    print "EEPROM write failed, is WP enabled?"

bus.close()
예제 #4
0
    def __init__(self):
        '''
        AFE4404(i2c_no)
        Creates an instance of the class AFE4404
        i2c_no can be 1 or 2 based on the i2c bus used
        '''
        self.i2cdev = serbus.I2CDev(1)
        self.i2cdev.open()

        # Software reset
        DATA_BYTES = self.convert2bytes(self.DIAGNOSIS_DATA
                                        | self.DIAGNOSIS_SW_RST)
        self.i2cdev.write(self.AFE4404_ADDR, [self.DIAGNOSIS] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED2 Start to 0
        DATA_BYTES = self.convert2bytes(self.LED2_ST_DATA)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED2_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED2 end to 399
        DATA_BYTES = self.convert2bytes(self.LED2_END_DATA | 0x18f)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED2_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED2 sample start to 80
        DATA_BYTES = self.convert2bytes(self.SMPL_LED2_ST_DATA | 0x50)
        self.i2cdev.write(self.AFE4404_ADDR, [self.SMPL_LED2_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED2 sample end to 399
        DATA_BYTES = self.convert2bytes(self.SMPL_LED2_END_DATA | 0x18f)
        self.i2cdev.write(self.AFE4404_ADDR, [self.SMPL_LED2_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set ADC reset 0 stat to 401
        DATA_BYTES = self.convert2bytes(self.ADC_RST_P0_ST_DATA | 0x191)
        self.i2cdev.write(self.AFE4404_ADDR, [self.ADC_RST_P0_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set ADC reset 0 end to 407
        DATA_BYTES = self.convert2bytes(self.ADC_RST_P0_END_DATA | 0x197)
        self.i2cdev.write(self.AFE4404_ADDR,
                          [self.ADC_RST_P0_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED2 convert start to 408
        DATA_BYTES = self.convert2bytes(self.LED2_CONV_ST_DATA | 0x198)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED2_CONV_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED2 convert end to 1467
        DATA_BYTES = self.convert2bytes(self.LED2_CONV_END_DATA | 0x5bb)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED2_CONV_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED3 start to 400
        DATA_BYTES = self.convert2bytes(self.LED3LEDSTC_DATA | 0x190)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED3LEDSTC] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED3 end to 799
        DATA_BYTES = self.convert2bytes(self.LED3LEDENDC_DATA | 0x31f)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED3LEDENDC] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED3 sample start to 480
        DATA_BYTES = self.convert2bytes(self.SMPL_LED3_ST_DATA | 0x1e0)
        self.i2cdev.write(self.AFE4404_ADDR, [self.SMPL_LED3_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED3 sample end to 799
        DATA_BYTES = self.convert2bytes(self.SMPL_LED3_END_DATA | 0x31f)
        self.i2cdev.write(self.AFE4404_ADDR, [self.SMPL_LED3_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set ADC reset 1 start to 1469
        DATA_BYTES = self.convert2bytes(self.ADC_RST_P1_ST_DATA | 0x5bd)
        self.i2cdev.write(self.AFE4404_ADDR, [self.ADC_RST_P1_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set ADC reset 1 end to 1469
        DATA_BYTES = self.convert2bytes(self.ADC_RST_P1_END_DATA | 0x5c3)
        self.i2cdev.write(self.AFE4404_ADDR,
                          [self.ADC_RST_P1_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED3 convert start to 1476
        DATA_BYTES = self.convert2bytes(self.LED3_CONV_ST_DATA | 0x5c4)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED3_CONV_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set ADC convert end to 2535
        DATA_BYTES = self.convert2bytes(self.LED3_CONV_END_DATA | 0x9e7)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED3_CONV_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED1 start to 1469
        DATA_BYTES = self.convert2bytes(self.LED1_ST_DATA | 0x320)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED1_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED1 end to 1199
        DATA_BYTES = self.convert2bytes(self.LED1_END_DATA | 0x4af)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED1_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED1 sample start to 880
        DATA_BYTES = self.convert2bytes(self.SMPL_LED1_ST_DATA | 0x370)
        self.i2cdev.write(self.AFE4404_ADDR, [self.SMPL_LED1_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED1 sample end to 1199
        DATA_BYTES = self.convert2bytes(self.SMPL_LED1_END_DATA | 0x4af)
        self.i2cdev.write(self.AFE4404_ADDR, [self.SMPL_LED1_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set ADC reset 2 start to 2537
        DATA_BYTES = self.convert2bytes(self.ADC_RST_P2_ST_DATA | 0x9e9)
        self.i2cdev.write(self.AFE4404_ADDR, [self.ADC_RST_P2_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set ADC reset 2 end to 2543
        DATA_BYTES = self.convert2bytes(self.ADC_RST_P2_END_DATA | 0x9ef)
        self.i2cdev.write(self.AFE4404_ADDR,
                          [self.ADC_RST_P2_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED1 convert start to 2544
        DATA_BYTES = self.convert2bytes(self.LED1_CONV_ST_DATA | 0x9f0)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED1_CONV_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED1 convert end to 3603
        DATA_BYTES = self.convert2bytes(self.LED1_CONV_END_DATA | 0xe13)
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED1_CONV_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set Ambient1 sample start to 1279
        DATA_BYTES = self.convert2bytes(self.SMPL_AMB1_ST_DATA | 0x4ff)
        self.i2cdev.write(self.AFE4404_ADDR, [self.SMPL_AMB1_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set Ambient1 smaple end to 1598
        DATA_BYTES = self.convert2bytes(self.SMPL_AMB1_END_DATA | 0x63e)
        self.i2cdev.write(self.AFE4404_ADDR, [self.SMPL_AMB1_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set ADC reset 3 start to 3605
        DATA_BYTES = self.convert2bytes(self.ADC_RST_P3_ST_DATA | 0xe15)
        self.i2cdev.write(self.AFE4404_ADDR, [self.ADC_RST_P3_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set ADC reset 3 end to 3611
        DATA_BYTES = self.convert2bytes(self.ADC_RST_P3_END_DATA | 0xe1b)
        self.i2cdev.write(self.AFE4404_ADDR,
                          [self.ADC_RST_P3_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set Ambient1 convert start to 3612
        DATA_BYTES = self.convert2bytes(self.AMB1_CONV_ST_DATA | 0xe1c)
        self.i2cdev.write(self.AFE4404_ADDR, [self.AMB1_CONV_ST] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set Ambient convert end to 4671
        DATA_BYTES = self.convert2bytes(self.AMB1_CONV_END_DATA | 0x123f)
        self.i2cdev.write(self.AFE4404_ADDR, [self.AMB1_CONV_END] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set powerdown start to 5471
        DATA_BYTES = self.convert2bytes(self.PDNCYCLESTC_DATA | 0x155f)
        self.i2cdev.write(self.AFE4404_ADDR, [self.PDNCYCLESTC] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set powerdown end to 39199
        DATA_BYTES = self.convert2bytes(self.PDNCYCLEENDC_DATA | 0x991f)
        self.i2cdev.write(self.AFE4404_ADDR, [self.PDNCYCLEENDC] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set prpct to 39999
        DATA_BYTES = self.convert2bytes(self.PRPCT_DATA | 0x9c3f)
        self.i2cdev.write(self.AFE4404_ADDR, [self.PRPCT] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set timer
        DATA_BYTES = self.convert2bytes(self.TIM_NUMAV_DATA | self.TIMEREN
                                        | 0x3)
        self.i2cdev.write(self.AFE4404_ADDR, [self.TIM_NUMAV] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set TIA gain
        DATA_BYTES = self.convert2bytes(self.TIA_GAINS2_DATA
                                        | self.TIA_ENSEPGAIN | 0x4)
        self.i2cdev.write(self.AFE4404_ADDR, [self.TIA_GAINS2] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set TIA gain
        DATA_BYTES = self.convert2bytes(self.TIA_GAINS1_DATA | 0x3)
        self.i2cdev.write(self.AFE4404_ADDR, [self.TIA_GAINS1] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set LED current
        DATA_BYTES = self.convert2bytes(self.LED_CONFIG_DATA | 0xf | (0x3 << 6)
                                        | (0x3 << 12))
        self.i2cdev.write(self.AFE4404_ADDR, [self.LED_CONFIG] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set SETTINGS register
        DATA_BYTES = self.convert2bytes(self.SETTINGS_DATA | (1 << 17)
                                        | (1 << 14) | (1 << 9) | (1 << 4))
        self.i2cdev.write(self.AFE4404_ADDR, [self.SETTINGS] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set CLKOUT register
        DATA_BYTES = self.convert2bytes(self.CLKOUT_DATA | (0x2 << 1))
        self.i2cdev.write(self.AFE4404_ADDR, [self.CLKOUT] + DATA_BYTES)
        self.i2c_comm_delay()

        # Set CLKDIV_PRF register
        DATA_BYTES = self.convert2bytes(self.CLKDIV_PRF_DATA | 0x1)
        self.i2cdev.write(self.AFE4404_ADDR, [self.CLKDIV_PRF] + DATA_BYTES)
        self.i2c_comm_delay()
        DATA_BYTES = self.convert2bytes(self.DIAGNOSIS_DATA | 0x1)
        self.i2cdev.write(self.AFE4404_ADDR, [self.DIAGNOSIS] + DATA_BYTES)
        self.i2c_comm_delay()

        print("done.")