|
| 1 | +/* Copyright 2021 Gompa (@Gompa) |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU General Public License as published by |
| 5 | + * the Free Software Foundation, either version 2 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License |
| 14 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +// https://github.com/shinoaliceKabocha/choco60_track/tree/master/keymaps/default |
| 18 | + |
| 19 | +#include "paw3204.h" |
| 20 | +#include "wait.h" |
| 21 | +#include "debug.h" |
| 22 | +#include "gpio.h" |
| 23 | + |
| 24 | +#define REG_PID1 0x00 |
| 25 | +#define REG_PID2 0x01 |
| 26 | +#define REG_STAT 0x02 |
| 27 | +#define REG_X 0x03 |
| 28 | +#define REG_Y 0x04 |
| 29 | + |
| 30 | +#define REG_SETUP 0x06 |
| 31 | +#define REG_IMGQUAL 0x07 |
| 32 | +#define REG_IMGREC 0x0E |
| 33 | +#define REG_IMGTRASH 0x0D |
| 34 | + |
| 35 | +#define constrain(amt, low, high) ((amt) < (low) ? (low) : ((amt) > (high) ? (high) : (amt))) |
| 36 | + |
| 37 | +// CPI values |
| 38 | +enum cpi_values { |
| 39 | + CPI400, // 0b000 |
| 40 | + CPI500, // 0b001 |
| 41 | + CPI600, // 0b010 |
| 42 | + CPI800, // 0b011 |
| 43 | + CPI1000, // 0b100 |
| 44 | + CPI1200, // 0b101 |
| 45 | + CPI1600, // 0b110 |
| 46 | +}; |
| 47 | + |
| 48 | +uint8_t paw3204_serial_read(void); |
| 49 | +void paw3204_serial_write(uint8_t reg_addr); |
| 50 | +uint8_t paw3204_read_reg(uint8_t reg_addr); |
| 51 | +void paw3204_write_reg(uint8_t reg_addr, uint8_t data); |
| 52 | + |
| 53 | +void paw3204_init(void) { |
| 54 | + setPinOutput(PAW3204_SCLK_PIN); // setclockpin to output |
| 55 | + setPinInputHigh(PAW3204_SDIO_PIN); // set datapin input high |
| 56 | + |
| 57 | + paw3204_write_reg(REG_SETUP, 0x86); // reset sensor and set 1600cpi |
| 58 | + wait_us(5); |
| 59 | + |
| 60 | + paw3204_read_reg(0x00); // read id |
| 61 | + paw3204_read_reg(0x01); // read id2 |
| 62 | + // PAW3204_write_reg(REG_SETUP,0x06); // dont reset sensor and set cpi 1600 |
| 63 | + paw3204_write_reg(REG_IMGTRASH, 0x32); // write image trashhold |
| 64 | +} |
| 65 | + |
| 66 | +uint8_t paw3204_serial_read(void) { |
| 67 | + setPinInput(PAW3204_SDIO_PIN); |
| 68 | + uint8_t byte = 0; |
| 69 | + |
| 70 | + for (uint8_t i = 0; i < 8; ++i) { |
| 71 | + writePinLow(PAW3204_SCLK_PIN); |
| 72 | + wait_us(1); |
| 73 | + |
| 74 | + byte = (byte << 1) | readPin(PAW3204_SDIO_PIN); |
| 75 | + |
| 76 | + writePinHigh(PAW3204_SCLK_PIN); |
| 77 | + wait_us(1); |
| 78 | + } |
| 79 | + |
| 80 | + return byte; |
| 81 | +} |
| 82 | + |
| 83 | +void paw3204_serial_write(uint8_t data) { |
| 84 | + writePinLow(PAW3204_SDIO_PIN); |
| 85 | + setPinOutput(PAW3204_SDIO_PIN); |
| 86 | + |
| 87 | + for (int8_t b = 7; b >= 0; b--) { |
| 88 | + writePinLow(PAW3204_SCLK_PIN); |
| 89 | + if (data & (1 << b)) { |
| 90 | + writePinHigh(PAW3204_SDIO_PIN); |
| 91 | + } else { |
| 92 | + writePinLow(PAW3204_SDIO_PIN); |
| 93 | + } |
| 94 | + writePinHigh(PAW3204_SCLK_PIN); |
| 95 | + } |
| 96 | + |
| 97 | + wait_us(4); |
| 98 | +} |
| 99 | + |
| 100 | +report_paw3204_t paw3204_read(void) { |
| 101 | + report_paw3204_t data = {0}; |
| 102 | + |
| 103 | + data.isMotion = paw3204_read_reg(REG_STAT) & (1 << 7); // check for motion only (bit 7 in field) |
| 104 | + data.x = (int8_t)paw3204_read_reg(REG_X); |
| 105 | + data.y = (int8_t)paw3204_read_reg(REG_Y); |
| 106 | + |
| 107 | + return data; |
| 108 | +} |
| 109 | + |
| 110 | +void paw3204_write_reg(uint8_t reg_addr, uint8_t data) { |
| 111 | + paw3204_serial_write(0b10000000 | reg_addr); |
| 112 | + paw3204_serial_write(data); |
| 113 | +} |
| 114 | + |
| 115 | +uint8_t paw3204_read_reg(uint8_t reg_addr) { |
| 116 | + paw3204_serial_write(reg_addr); |
| 117 | + wait_us(5); |
| 118 | + return paw3204_serial_read(); |
| 119 | +} |
| 120 | + |
| 121 | +void paw3204_set_cpi(uint16_t cpi) { |
| 122 | + uint8_t cpival = CPI1000; |
| 123 | + if (cpi <= 450) { |
| 124 | + cpival = CPI400; |
| 125 | + } else if (cpi <= 550) { |
| 126 | + cpival = CPI500; |
| 127 | + } else if (cpi <= 700) { |
| 128 | + cpival = CPI600; |
| 129 | + } else if (cpi <= 900) { |
| 130 | + cpival = CPI800; |
| 131 | + } else if (cpi <= 1100) { |
| 132 | + cpival = CPI1000; |
| 133 | + } else if (cpi <= 1400) { |
| 134 | + cpival = CPI1200; |
| 135 | + } else if (cpi > 1400) { |
| 136 | + cpival = CPI1600; |
| 137 | + } |
| 138 | + paw3204_write_reg(REG_SETUP, cpival); |
| 139 | +} |
| 140 | + |
| 141 | +uint16_t paw3204_get_cpi(void) { |
| 142 | + uint16_t cpival = 1000; |
| 143 | + |
| 144 | + switch (paw3204_read_reg(REG_SETUP) & 0b111) { |
| 145 | + case CPI400: |
| 146 | + cpival = 400; |
| 147 | + break; |
| 148 | + case CPI500: |
| 149 | + cpival = 500; |
| 150 | + break; |
| 151 | + case CPI600: |
| 152 | + cpival = 600; |
| 153 | + break; |
| 154 | + case CPI800: |
| 155 | + cpival = 800; |
| 156 | + break; |
| 157 | + case CPI1000: |
| 158 | + cpival = 1000; |
| 159 | + break; |
| 160 | + case CPI1200: |
| 161 | + cpival = 1200; |
| 162 | + break; |
| 163 | + case CPI1600: |
| 164 | + cpival = 1600; |
| 165 | + break; |
| 166 | + } |
| 167 | + return cpival; |
| 168 | +} |
| 169 | + |
| 170 | +uint8_t read_pid_paw3204(void) { |
| 171 | + return paw3204_read_reg(REG_PID1); |
| 172 | +} |
0 commit comments