Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions firmware/cap1296.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,53 @@
#include "cap1296.h"

#define CAP1296_ADDR 0x28

#define REG_SENSITIVITY 0x1F // Sensitivity Control
#define REG_INPUTEN 0x21 // Sensor Input Enable
#define REG_CAL 0x26 // Calibration Activate and Status
#define REG_INTEN 0x27 // Interrupt Enable
#define REG_REPEAERATEEN 0x28 // Repeat Rate Enable
#define REG_CONF2 0x44 // Configuration 2
#define REG_MAINCONTROL 0x00 // Main Control
#define REG_INPUTSTATUS 0x03 // Sensor Input Status

void cap1296_init()
{
// Sensitivity Control Register
// Set sensitivity multiplier to x4 and data scaling factor to x256
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){0x1F, 0x5F}, 2, false);
// Sensor Input Enable Register
// Do monitor touch inputs CS1, CS2, CS3, CS4 and don't monitor touch inputs CS5, CS6 in the active state
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){0x21, 0x0F}, 2, false);
// Calibration Activate and Status Register
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){REG_SENSITIVITY, 0x5F}, 2, false);

// Monitor touch inputs CS1, CS2, CS3, CS4 and don't monitor touch inputs CS5, CS6 in the active state
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){REG_INPUTEN, 0x0F}, 2, false);

// Force recalibration of the touch inputs CS1, CS2, CS3, CS4
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){0x26, 0x0F}, 2, false);
// Interrupt Enable Register
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){REG_CAL, 0x0F}, 2, false);

// Enable interrupts for touch inputs CS1, CS2, CS3, CS4 and disable interrupts for touch inputs CS5, CS6
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){0x27, 0x0F}, 2, false);
// Repeat Rate Enable Register
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){REG_INTEN, 0x0F}, 2, false);

// Disable repeat interrupts during button holds for all touch inputs
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){0x28, 0x00}, 2, false);
// Configuration 2 Register
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){REG_REPEAERATEEN, 0x00}, 2, false);

// Set interrupts to trigger when a button is released and recalibrate a touch input if its base count is out of limit
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){0x44, 0x40}, 2, false);
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[2]){REG_CONF2, 0x40}, 2, false);
}

void cap1296_clear_int_bit_in_main_control_register()
{
uint8_t main_control_register_value;
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[1]){0}, 1, true);
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[1]){REG_MAINCONTROL}, 1, true);
i2c_read_blocking(I2C_PORT, CAP1296_ADDR, &main_control_register_value, 1, false);

// Main Control Register
// Clear the int bit while maintaining previous values of other bits
const uint8_t main_control_register[2] = {0x00, (uint8_t)(main_control_register_value & 0b11111110)};
const uint8_t main_control_register[2] = {REG_MAINCONTROL, (uint8_t)(main_control_register_value & 0b11111110)};
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, main_control_register, 2, false);
}

uint8_t cap1296_read_sensor_input_status_register()
{
uint8_t touch_status_buffer;
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[1]){0x03}, 1, true);
i2c_write_blocking(I2C_PORT, CAP1296_ADDR, (const uint8_t[1]){REG_INPUTSTATUS}, 1, true);
i2c_read_blocking(I2C_PORT, CAP1296_ADDR, &touch_status_buffer, 1, false);
return touch_status_buffer;
}
6 changes: 1 addition & 5 deletions firmware/cap1296.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
#include "hardware/i2c.h"
#include "pindefs.h"

// CAP1296 datasheet:
// https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/00001569B.pdf

#define CAP1296_ADDR 0x28
#define BUTTON_RELEASE 0
#define CW_BUTTON_PRESS (1 << 0)
#define ENABLE_BUTTON_PRESS (1 << 1)
Expand All @@ -15,4 +11,4 @@

void cap1296_init();
void cap1296_clear_int_bit_in_main_control_register();
uint8_t cap1296_read_sensor_input_status_register();
uint8_t cap1296_read_sensor_input_status_register();
27 changes: 18 additions & 9 deletions firmware/is32fl3193.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,42 @@
#include "hardware/i2c.h"
#include "is32fl3193.h"

#define IS31_ADDR 0x68

#define REG_SHDN 0x00
#define REG_LEDMODE 0x01 // NB: Switched on datasheet
#define REG_BREATHCTRL 0x02 // NB: Switched on datasheet
#define REG_CURRSET 0x03
#define REG_PWM0 0x04
#define REG_PWM1 0x05
#define REG_PWM2 0x06
#define REG_DATAUPDATE 0x07

void rgb_set_rgb(uint8_t r, uint8_t g, uint8_t b)
{
uint8_t rgb[4] = {0x04, r, g, b};
uint8_t rgb[4] = {REG_PWM0, r, g, b};
i2c_write_blocking(I2C_PORT, IS31_ADDR, rgb, 4, false);
i2c_write_blocking(I2C_PORT, IS31_ADDR, (const uint8_t[]){0x07, 0x00}, 2, false); // pwm data
i2c_write_blocking(I2C_PORT, IS31_ADDR, (const uint8_t[]){REG_DATAUPDATE, 0x00}, 2, false); // pwm data
}

void rgb_set_auto(bool enable, bool led)
{
const uint8_t current_driver_settings[2] = {0x00, 0x20}; // {disable current driver, enable current driver}
const uint8_t current_driver_address_value[2] = {0x00, current_driver_settings[led]}; // {enable current register address, enable current register value}
i2c_write_blocking(I2C_PORT, IS31_ADDR, current_driver_address_value, 2, false);
const uint8_t cmd[2] = {REG_SHDN, led ? (uint8_t)0x20 : (uint8_t)0x00};
i2c_write_blocking(I2C_PORT, IS31_ADDR, cmd, 2, false);

const uint8_t rgb_colors[3][3] = {{255, 0, 0}, {1, 20, 7}, {0, 0, 255}}; // r, g, b
rgb_set_rgb(rgb_colors[enable][0], rgb_colors[enable][1], rgb_colors[enable][2]);
}

void rgb_set_breathing(bool breathing)
{
const uint8_t breathing_settings[2] = {0x00, 0x20}; // {no breathing, yes breathing}
const uint8_t breathing_address_value[2] = {0x02, breathing_settings[breathing]}; // {breathing register address, breathing register value}
i2c_write_blocking(I2C_PORT, IS31_ADDR, breathing_address_value, 2, false);
const uint8_t cmd[2] = {REG_BREATHCTRL, breathing ? (uint8_t)0x20 : (uint8_t)0x00};
i2c_write_blocking(I2C_PORT, IS31_ADDR, cmd, 2, false);
}

void rgb_init()
{
gpio_put(IS31_POW_EN, 1);
i2c_write_blocking(I2C_PORT, IS31_ADDR, (const uint8_t[2]){0x03, 0x08}, 2, false); // Set max current to 5 mA
i2c_write_blocking(I2C_PORT, IS31_ADDR, (const uint8_t[2]){REG_CURRSET, 0x08}, 2, false); // Set max current to 5 mA
}

4 changes: 1 addition & 3 deletions firmware/is32fl3193.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#define IS31_ADDR 0x68

void rgb_init();
void rgb_set_breathing(bool breathing);
void rgb_set_breathing(bool breathing);
void rgb_set_auto(bool enable, bool led);
Loading