33#define I2C_SMBUS_WRITE 0
34#define I2C_SMBUS_READ 1
35#define I2C_SMBUS_BYTE_DATA 2
45template <
typename T> T
clamp(T value, T min_val, T max_val) {
46 return (value < min_val) ? min_val : ((value > max_val) ? max_val : value);
66 throw std::runtime_error(
"Failed to open I2C device");
73 throw std::runtime_error(
"Failed to set I2C address");
104 struct i2c_smbus_ioctl_data args;
106 args.command = command;
110 return ioctl(file, I2C_SMBUS, &args);
123 struct i2c_smbus_ioctl_data args;
125 args.command = command;
129 if (ioctl(file, I2C_SMBUS, &args) < 0) {
145 throw std::runtime_error(
"I2C write failed");
161 throw std::runtime_error(
"I2C read failed");
179 int base_reg = 0x06 + (channel * 4);
196 value =
clamp(value, 0, 4095);
239 int prescale =
static_cast<int>(std::floor(25000000.0 / 4096.0 / 100 - 1));
241 int newmode = (oldmode & 0x7F) | 0x10;
T clamp(T value, T min_val, T max_val)
Clamps a value to a given range.
#define I2C_SMBUS_BYTE_DATA
T clamp(T value, T min_val, T max_val)
Clamps a value to a given range.
File containing the PeripheralController class.
void init_motors() override
Initializes the motor controllers.
void init_servo() override
Initializes the servo controller.
~PeripheralController() override
Destructor for the PeripheralController class.
void set_servo_pwm(int channel, int on_value, int off_value) override
Sets the PWM of a servo motor.
int i2c_smbus_read_byte_data(int file, uint8_t command) override
Reads a byte of data from a specific register.
void set_motor_pwm(int channel, int value) override
Sets the PWM value for a motor.
virtual void write_byte_data(int fd, int reg, int value) override
Writes a byte of data to a specific register.
virtual int read_byte_data(int fd, int reg) override
Reads a byte of data from a specific register.
PeripheralController(int servo_addr, int motor_addr)
Constructor for the PeripheralController class.
int i2c_smbus_write_byte_data(int file, uint8_t command, uint8_t value) override
Writes a byte of data to a specific register.
Represents data formats for I2C SMBus communication.