qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

pimoroni_trackball.c (6036B)


      1 /* Copyright 2020 Christopher Courtney, aka Drashna Jael're  (@drashna) <drashna@live.com>
      2  * Copyright 2021 Dasky (@daskygit)
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #include "pointing_device_internal.h"
     19 #include "pimoroni_trackball.h"
     20 #include "i2c_master.h"
     21 #include "timer.h"
     22 
     23 // clang-format off
     24 #define PIMORONI_TRACKBALL_REG_LED_RED 0x00
     25 #define PIMORONI_TRACKBALL_REG_LED_GRN 0x01
     26 #define PIMORONI_TRACKBALL_REG_LED_BLU 0x02
     27 #define PIMORONI_TRACKBALL_REG_LED_WHT 0x03
     28 #define PIMORONI_TRACKBALL_REG_LEFT    0x04
     29 #define PIMORONI_TRACKBALL_REG_RIGHT   0x05
     30 #define PIMORONI_TRACKBALL_REG_UP      0x06
     31 #define PIMORONI_TRACKBALL_REG_DOWN    0x07
     32 // clang-format on
     33 
     34 static uint16_t precision = 128;
     35 
     36 const pointing_device_driver_t pimoroni_trackball_pointing_device_driver = {
     37     .init       = pimoroni_trackball_device_init,
     38     .get_report = pimoroni_trackball_get_report,
     39     .set_cpi    = pimoroni_trackball_set_cpi,
     40     .get_cpi    = pimoroni_trackball_get_cpi,
     41 };
     42 
     43 uint16_t pimoroni_trackball_get_cpi(void) {
     44     return (precision * 125);
     45 }
     46 /**
     47  * @brief Sets the scaling value for pimoroni trackball
     48  *
     49  * Sets a scaling value for pimoroni trackball to allow runtime adjustment. This isn't used by the sensor and is an
     50  * approximation so the functions are consistent across drivers.
     51  *
     52  * NOTE: This rounds down to the nearest number divisable by 125 that's a positive integer, values below 125 are clamped to 125.
     53  *
     54  * @param cpi uint16_t
     55  */
     56 void pimoroni_trackball_set_cpi(uint16_t cpi) {
     57     if (cpi < 249) {
     58         precision = 1;
     59     } else {
     60         precision = (cpi - (cpi % 125)) / 125;
     61     }
     62 }
     63 
     64 void pimoroni_trackball_set_rgbw(uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
     65     uint8_t                              data[4] = {r, g, b, w};
     66     __attribute__((unused)) i2c_status_t status  = i2c_write_register(PIMORONI_TRACKBALL_ADDRESS << 1, PIMORONI_TRACKBALL_REG_LED_RED, data, sizeof(data), PIMORONI_TRACKBALL_TIMEOUT);
     67 
     68     pd_dprintf("Trackball RGBW i2c_status_t: %d\n", status);
     69 }
     70 
     71 i2c_status_t read_pimoroni_trackball(pimoroni_data_t *data) {
     72     i2c_status_t status = i2c_read_register(PIMORONI_TRACKBALL_ADDRESS << 1, PIMORONI_TRACKBALL_REG_LEFT, (uint8_t *)data, sizeof(*data), PIMORONI_TRACKBALL_TIMEOUT);
     73 
     74 #ifdef POINTING_DEVICE_DEBUG
     75     static uint16_t d_timer;
     76     if (timer_elapsed(d_timer) > PIMORONI_TRACKBALL_DEBUG_INTERVAL) {
     77         pd_dprintf("Trackball READ i2c_status_t: %d L: %d R: %d Up: %d D: %d SW: %d\n", status, data->left, data->right, data->up, data->down, data->click);
     78         d_timer = timer_read();
     79     }
     80 #endif
     81 
     82     return status;
     83 }
     84 
     85 __attribute__((weak)) bool pimoroni_trackball_device_init(void) {
     86     i2c_init();
     87     uint8_t      rgbw_data[4] = {0};
     88     i2c_status_t status       = i2c_write_register(PIMORONI_TRACKBALL_ADDRESS << 1, PIMORONI_TRACKBALL_REG_LED_RED, rgbw_data, sizeof(rgbw_data), PIMORONI_TRACKBALL_TIMEOUT);
     89 
     90     return (status == I2C_STATUS_SUCCESS);
     91 }
     92 
     93 int16_t pimoroni_trackball_get_offsets(uint8_t negative_dir, uint8_t positive_dir, uint8_t scale) {
     94     uint8_t offset     = 0;
     95     bool    isnegative = false;
     96     if (negative_dir > positive_dir) {
     97         offset     = negative_dir - positive_dir;
     98         isnegative = true;
     99     } else {
    100         offset = positive_dir - negative_dir;
    101     }
    102     uint16_t magnitude = (scale * offset * offset * precision) >> 7;
    103     return isnegative ? -(int16_t)(magnitude) : (int16_t)(magnitude);
    104 }
    105 
    106 mouse_xy_report_t pimoroni_trackball_adapt_values(xy_clamp_range_t *offset) {
    107     if (*offset > MOUSE_REPORT_XY_MAX) {
    108         *offset -= MOUSE_REPORT_XY_MAX;
    109         return (mouse_xy_report_t)MOUSE_REPORT_XY_MAX;
    110     } else if (*offset < MOUSE_REPORT_XY_MIN) {
    111         *offset += MOUSE_REPORT_XY_MAX;
    112         return (mouse_xy_report_t)MOUSE_REPORT_XY_MIN;
    113     } else {
    114         mouse_xy_report_t temp_return = *offset;
    115         *offset                       = 0;
    116         return temp_return;
    117     }
    118 }
    119 
    120 report_mouse_t pimoroni_trackball_get_report(report_mouse_t mouse_report) {
    121     static uint16_t         debounce      = 0;
    122     static uint8_t          error_count   = 0;
    123     pimoroni_data_t         pimoroni_data = {0};
    124     static xy_clamp_range_t x_offset = 0, y_offset = 0;
    125 
    126     if (error_count < PIMORONI_TRACKBALL_ERROR_COUNT) {
    127         i2c_status_t status = read_pimoroni_trackball(&pimoroni_data);
    128 
    129         if (status == I2C_STATUS_SUCCESS) {
    130             error_count = 0;
    131 
    132             if (!(pimoroni_data.click & 128)) {
    133                 mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, false, POINTING_DEVICE_BUTTON1);
    134                 if (!debounce) {
    135                     x_offset += pimoroni_trackball_get_offsets(pimoroni_data.right, pimoroni_data.left, PIMORONI_TRACKBALL_SCALE);
    136                     y_offset += pimoroni_trackball_get_offsets(pimoroni_data.down, pimoroni_data.up, PIMORONI_TRACKBALL_SCALE);
    137                     mouse_report.x = pimoroni_trackball_adapt_values(&x_offset);
    138                     mouse_report.y = pimoroni_trackball_adapt_values(&y_offset);
    139                 } else {
    140                     debounce--;
    141                 }
    142             } else {
    143                 mouse_report.buttons = pointing_device_handle_buttons(mouse_report.buttons, true, POINTING_DEVICE_BUTTON1);
    144                 debounce             = PIMORONI_TRACKBALL_DEBOUNCE_CYCLES;
    145             }
    146         } else {
    147             error_count++;
    148         }
    149     }
    150     return mouse_report;
    151 }