Skip to content

Commit

Permalink
modify paw3204
Browse files Browse the repository at this point in the history
  • Loading branch information
daskygit committed Apr 8, 2023
1 parent 40728df commit bd9d1f3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 75 deletions.
163 changes: 92 additions & 71 deletions drivers/sensors/paw3204.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,153 +20,174 @@
#include "wait.h"
#include "debug.h"
#include "gpio.h"
#include "pointing_device.h"

#define REG_PID1 0x00
#define REG_PID2 0x01
#define REG_STAT 0x02
#define REG_X 0x03
#define REG_Y 0x04
#define PAW3204_REG_PID1 0x00
#define PAW3204_REG_PID2 0x01
#define PAW3204_REG_STAT 0x02
#define PAW3204_REG_X 0x03
#define PAW3204_REG_Y 0x04

#define REG_SETUP 0x06
#define REG_IMGQUAL 0x07
#define REG_IMGREC 0x0E
#define REG_IMGTRASH 0x0D
#define PAW3204_REG_SETUP 0x06
#define PAW3204_REG_IMGQUAL 0x07
#define PAW3204_REG_IMGREC 0x0E
#define PAW3204_REG_IMGTRASH 0x0D

#define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt)))

// CPI values
enum cpi_values {
CPI400, // 0b000
CPI500, // 0b001
CPI600, // 0b010
CPI800, // 0b011
CPI1000, // 0b100
CPI1200, // 0b101
CPI1600, // 0b110
enum paw3204_cpi_values {
PAW3204_CPI400, // 0b000
PAW3204_CPI500, // 0b001
PAW3204_CPI600, // 0b010
PAW3204_CPI800, // 0b011
PAW3204_CPI1000, // 0b100
PAW3204_CPI1200, // 0b101
PAW3204_CPI1600, // 0b110
};

uint8_t paw3204_serial_read(void);
void paw3204_serial_write(uint8_t reg_addr);
uint8_t paw3204_read_reg(uint8_t reg_addr);
void paw3204_write_reg(uint8_t reg_addr, uint8_t data);
uint8_t paw3204_serial_read(paw3204_config_t* paw3204_config);
void paw3204_serial_write(paw3204_config_t* paw3204_config, uint8_t reg_addr);
uint8_t paw3204_read_reg(paw3204_config_t* paw3204_config, uint8_t reg_addr);
void paw3204_write_reg(paw3204_config_t* paw3204_config, uint8_t reg_addr, uint8_t data);

void paw3204_init(void) {
setPinOutput(PAW3204_SCLK_PIN); // setclockpin to output
setPinInputHigh(PAW3204_SDIO_PIN); // set datapin input high
void paw3204_init(const void* config) {
paw3204_config_t* paw3204_config = (paw3204_config_t*)config;

paw3204_write_reg(REG_SETUP, 0x86); // reset sensor and set 1600cpi
setPinOutput(paw3204_config->sclk); // setclockpin to output
setPinInputHigh(paw3204_config->sdio); // set datapin input high

paw3204_write_reg(paw3204_config, PAW3204_REG_SETUP, 0x86); // reset sensor and set 1600cpi
wait_us(5);

paw3204_read_reg(0x00); // read id
paw3204_read_reg(0x01); // read id2
paw3204_read_reg(paw3204_config,0x00); // read id
paw3204_read_reg(paw3204_config, 0x01); // read id2
// PAW3204_write_reg(REG_SETUP,0x06); // dont reset sensor and set cpi 1600
paw3204_write_reg(REG_IMGTRASH, 0x32); // write image trashhold
paw3204_write_reg(paw3204_config, PAW3204_REG_IMGTRASH, 0x32); // write image trashhold
}

uint8_t paw3204_serial_read(void) {
setPinInput(PAW3204_SDIO_PIN);
uint8_t paw3204_serial_read(paw3204_config_t* paw3204_config) {
setPinInput(paw3204_config->sdio);
uint8_t byte = 0;

for (uint8_t i = 0; i < 8; ++i) {
writePinLow(PAW3204_SCLK_PIN);
writePinLow(paw3204_config->sclk);
wait_us(1);

byte = (byte << 1) | readPin(PAW3204_SDIO_PIN);
byte = (byte << 1) | readPin(paw3204_config->sdio);

writePinHigh(PAW3204_SCLK_PIN);
writePinHigh(paw3204_config->sclk);
wait_us(1);
}

return byte;
}

void paw3204_serial_write(uint8_t data) {
writePinLow(PAW3204_SDIO_PIN);
setPinOutput(PAW3204_SDIO_PIN);
void paw3204_serial_write(paw3204_config_t* paw3204_config, uint8_t data) {
writePinLow(paw3204_config->sdio);
setPinOutput(paw3204_config->sdio);

for (int8_t b = 7; b >= 0; b--) {
writePinLow(PAW3204_SCLK_PIN);
writePinLow(paw3204_config->sclk);
if (data & (1 << b)) {
writePinHigh(PAW3204_SDIO_PIN);
writePinHigh(paw3204_config->sdio);
} else {
writePinLow(PAW3204_SDIO_PIN);
writePinLow(paw3204_config->sdio);
}
writePinHigh(PAW3204_SCLK_PIN);
writePinHigh(paw3204_config->sclk);
}

wait_us(4);
}

report_paw3204_t paw3204_read(void) {
report_paw3204_t paw3204_read(paw3204_config_t* paw3204_config) {
report_paw3204_t data = {0};

data.isMotion = paw3204_read_reg(REG_STAT) & (1 << 7); // check for motion only (bit 7 in field)
data.x = (int8_t)paw3204_read_reg(REG_X);
data.y = (int8_t)paw3204_read_reg(REG_Y);
data.isMotion = paw3204_read_reg(paw3204_config, PAW3204_REG_STAT) & (1 << 7); // check for motion only (bit 7 in field)
data.x = (int8_t)paw3204_read_reg(paw3204_config, PAW3204_REG_X);
data.y = (int8_t)paw3204_read_reg(paw3204_config, PAW3204_REG_Y);

return data;
}

void paw3204_write_reg(uint8_t reg_addr, uint8_t data) {
paw3204_serial_write(0b10000000 | reg_addr);
paw3204_serial_write(data);
void paw3204_write_reg(paw3204_config_t* paw3204_config, uint8_t reg_addr, uint8_t data) {
paw3204_serial_write(paw3204_config, 0b10000000 | reg_addr);
paw3204_serial_write(paw3204_config, data);
}

uint8_t paw3204_read_reg(uint8_t reg_addr) {
paw3204_serial_write(reg_addr);
uint8_t paw3204_read_reg(paw3204_config_t* paw3204_config, uint8_t reg_addr) {
paw3204_serial_write(paw3204_config, reg_addr);
wait_us(5);
return paw3204_serial_read();
return paw3204_serial_read(paw3204_config);
}

void paw3204_set_cpi(uint16_t cpi) {
uint8_t cpival = CPI1000;
void paw3204_set_cpi(const void* config, uint16_t cpi) {
paw3204_config_t* paw3204_config = (paw3204_config_t*)config;

uint8_t cpival = PAW3204_CPI1000;
if (cpi <= 450) {
cpival = CPI400;
cpival = PAW3204_CPI400;
} else if (cpi <= 550) {
cpival = CPI500;
cpival = PAW3204_CPI500;
} else if (cpi <= 700) {
cpival = CPI600;
cpival = PAW3204_CPI600;
} else if (cpi <= 900) {
cpival = CPI800;
cpival = PAW3204_CPI800;
} else if (cpi <= 1100) {
cpival = CPI1000;
cpival = PAW3204_CPI1000;
} else if (cpi <= 1400) {
cpival = CPI1200;
cpival = PAW3204_CPI1200;
} else if (cpi > 1400) {
cpival = CPI1600;
cpival = PAW3204_CPI1600;
}
paw3204_write_reg(REG_SETUP, cpival);
paw3204_write_reg(paw3204_config, PAW3204_REG_SETUP, cpival);
}

uint16_t paw3204_get_cpi(void) {
uint16_t paw3204_get_cpi(const void* config) {
paw3204_config_t* paw3204_config = (paw3204_config_t*)config;

uint16_t cpival = 1000;

switch (paw3204_read_reg(REG_SETUP) & 0b111) {
case CPI400:
switch (paw3204_read_reg(paw3204_config, PAW3204_REG_SETUP) & 0b111) {
case PAW3204_CPI400:
cpival = 400;
break;
case CPI500:
case PAW3204_CPI500:
cpival = 500;
break;
case CPI600:
case PAW3204_CPI600:
cpival = 600;
break;
case CPI800:
case PAW3204_CPI800:
cpival = 800;
break;
case CPI1000:
case PAW3204_CPI1000:
cpival = 1000;
break;
case CPI1200:
case PAW3204_CPI1200:
cpival = 1200;
break;
case CPI1600:
case PAW3204_CPI1600:
cpival = 1600;
break;
}
return cpival;
}

uint8_t read_pid_paw3204(void) {
return paw3204_read_reg(REG_PID1);
uint8_t read_pid_paw3204(paw3204_config_t* paw3204_config) {
return paw3204_read_reg(paw3204_config, PAW3204_REG_PID1);
}

report_mouse_t paw3204_get_report(const void* config) {
paw3204_config_t* paw3204_config = (paw3204_config_t*)config;
report_paw3204_t data = paw3204_read(paw3204_config);
report_mouse_t mouse_report = {0};
if (data.isMotion) {
pd_dprintf("Raw ] X: %d, Y: %d\n", data.x, data.y);

mouse_report.x = data.x;
mouse_report.y = data.y;
}

return mouse_report;
}
20 changes: 16 additions & 4 deletions drivers/sensors/paw3204.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <stdint.h>
#include <stdbool.h>
#include "pointing_device.h"

#ifndef PAW3204_SCLK_PIN
# ifdef POINTING_DEVICE_SCLK_PIN
Expand All @@ -40,14 +41,25 @@ typedef struct {
bool isMotion;
} report_paw3204_t;

typedef struct {
pin_t sdio;
pin_t sclk;
} paw3204_config_t;

const pointing_device_driver_t paw3204_driver_default;

#if defined(PAW3204_SCLK_PIN) & defined(PAW3204_SDIO_PIN)
const paw3204_config_t paw3204_config_default;
#endif

/**
* @brief Initializes the sensor so it is in a working state and ready to
* be polled for data.
*
* @return true Initialization was a success
* @return false Initialization failed, do not proceed operation
*/
void paw3204_init(void);
void paw3204_init(const void* config);

/**
* @brief Reads and clears the current delta, and motion register values on the
Expand All @@ -57,20 +69,20 @@ void paw3204_init(void);
* fields are set to zero
*/

report_paw3204_t paw3204_read(void);
report_paw3204_t paw3204_read(paw3204_config_t* config);
/**
* @brief Sets the given CPI value the sensor. CPI is often refereed to
* as the sensors sensitivity. Values outside of the allowed range are
* constrained into legal values.
*
* @param cpi CPI value to set
*/
void paw3204_set_cpi(uint16_t cpi);
void paw3204_set_cpi(const void* config, uint16_t cpi);

/**
* @brief Gets the currently set CPI value from the sensor. CPI is often
* refereed to as the sensors sensitivity.
*
* @return uint16_t Current CPI value of the sensor
*/
uint16_t paw3204_get_cpi(void);
uint16_t paw3204_get_cpi(const void* config);
4 changes: 4 additions & 0 deletions quantum/pointing_device/pointing_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "host.h"
#include "report.h"
#include "gpio.h"
#include "pointing_device_internal.h"

#ifdef POINTING_DEVICE_AUTO_MOUSE_ENABLE
# include "pointing_device_auto_mouse.h"
Expand Down Expand Up @@ -117,6 +118,9 @@ typedef struct {
#if defined(POINTING_DEVICE_DRIVER_CIRQUE_PINNACLE_I2C) || defined(POINTING_DEVICE_DRIVER_CIRQUE_PINNACLE_SPI)
# include "cirque_pinnacle.h"
#endif
#if defined(POINTING_DEVICE_DRIVER_PAW3204)
# include "paw3204.h"
#endif
#if defined(POINTING_DEVICE_DRIVER_PIMORONI_TRACKBALL)
# include "pimoroni_trackball.h"
#endif
Expand Down

0 comments on commit bd9d1f3

Please sign in to comment.