You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					51 lines
				
				1.4 KiB
			
		
		
			
		
	
	
					51 lines
				
				1.4 KiB
			| 
											5 years ago
										 | #pragma once
 | ||
|  | 
 | ||
|  | #include <cstdint>
 | ||
| 
											3 years ago
										 | #include <unistd.h>
 | ||
| 
											5 years ago
										 | #include "cereal/gen/cpp/log.capnp.h"
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											4 years ago
										 | #include "common/i2c.h"
 | ||
| 
											3 years ago
										 | #include "common/gpio.h"
 | ||
|  | 
 | ||
| 
											3 years ago
										 | #include "common/swaglog.h"
 | ||
| 
											3 years ago
										 | #include "system/sensord/sensors/constants.h"
 | ||
|  | #include "system/sensord/sensors/sensor.h"
 | ||
| 
											5 years ago
										 | 
 | ||
|  | int16_t read_12_bit(uint8_t lsb, uint8_t msb);
 | ||
|  | int16_t read_16_bit(uint8_t lsb, uint8_t msb);
 | ||
| 
											4 years ago
										 | int32_t read_20_bit(uint8_t b2, uint8_t b1, uint8_t b0);
 | ||
| 
											5 years ago
										 | 
 | ||
|  | 
 | ||
| 
											5 years ago
										 | class I2CSensor : public Sensor {
 | ||
| 
											5 years ago
										 | private:
 | ||
|  |   I2CBus *bus;
 | ||
| 
											3 years ago
										 |   int gpio_nr;
 | ||
|  |   bool shared_gpio;
 | ||
| 
											5 years ago
										 |   virtual uint8_t get_device_address() = 0;
 | ||
|  | 
 | ||
|  | public:
 | ||
| 
											3 years ago
										 |   I2CSensor(I2CBus *bus, int gpio_nr = 0, bool shared_gpio = false);
 | ||
|  |   ~I2CSensor();
 | ||
| 
											5 years ago
										 |   int read_register(uint register_address, uint8_t *buffer, uint8_t len);
 | ||
|  |   int set_register(uint register_address, uint8_t data);
 | ||
| 
											3 years ago
										 |   int init_gpio();
 | ||
|  |   bool has_interrupt_enabled();
 | ||
| 
											5 years ago
										 |   virtual int init() = 0;
 | ||
| 
											3 years ago
										 |   virtual bool get_event(MessageBuilder &msg, uint64_t ts = 0) = 0;
 | ||
| 
											3 years ago
										 |   virtual int shutdown() = 0;
 | ||
| 
											3 years ago
										 | 
 | ||
|  |   int verify_chip_id(uint8_t address, const std::vector<uint8_t> &expected_ids) {
 | ||
|  |     uint8_t chip_id = 0;
 | ||
|  |     int ret = read_register(address, &chip_id, 1);
 | ||
|  |     if (ret < 0) {
 | ||
|  |       LOGE("Reading chip ID failed: %d", ret);
 | ||
|  |       return -1;
 | ||
|  |     }
 | ||
|  |     for (int i = 0; i < expected_ids.size(); ++i) {
 | ||
|  |       if (chip_id == expected_ids[i]) return chip_id;
 | ||
|  |     }
 | ||
|  |     LOGE("Chip ID wrong. Got: %d, Expected %d", chip_id, expected_ids[0]);
 | ||
|  |     return -1;
 | ||
|  |   }
 | ||
| 
											5 years ago
										 | };
 |