Skip to content

Commit

Permalink
ADNS-5050 / Ploopy Nano / Ploopy Mini Trackballs (qmk#11994)
Browse files Browse the repository at this point in the history
* added adns5050 sensor code, as well as implementations for the Ploopy Mini and the Ploopy Nano

* fixed spurious scrolling issue

* recommended fixes for pr linting and cleanup
  • Loading branch information
ploopyco authored and violet-fish committed Mar 28, 2021
1 parent 0d2db0b commit 88fb9b7
Show file tree
Hide file tree
Showing 31 changed files with 1,464 additions and 0 deletions.
197 changes: 197 additions & 0 deletions keyboards/ploopyco/adns5050.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/* Copyright 2021 Colin Lam (Ploopy Corporation)
* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
* Copyright 2019 Sunjun Kim
* Copyright 2019 Hiroyuki Okada
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#include "adns5050.h"
#include "quantum.h"
#include "wait.h"

#ifdef CONSOLE_ENABLE
# include "print.h"
#endif

#ifndef OPTIC_ROTATED
# define OPTIC_ROTATED false
#endif

// Definitions for the ADNS serial line.
// These really ought to be defined in your config.h, but defaults are
// here if you're really lazy.
#ifndef ADNS_SCLK_PIN
# define ADNS_SCLK_PIN B7
#endif

#ifndef ADNS_SDIO_PIN
# define ADNS_SDIO_PIN C6
#endif

#ifndef ADNS_CS_PIN
# define ADNS_CS_PIN B4
#endif

#ifdef CONSOLE_ENABLE
void print_byte(uint8_t byte) { dprintf("%c%c%c%c%c%c%c%c|", (byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), (byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), (byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), (byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')); }
#endif

// Initialize the ADNS serial pins.
void adns_init(void) {
setPinOutput(ADNS_SCLK_PIN);
setPinOutput(ADNS_SDIO_PIN);
setPinOutput(ADNS_CS_PIN);
}

// Perform a synchronization with the ADNS.
// Just as with the serial protocol, this is used by the slave to send a
// synchronization signal to the master.
void adns_sync(void) {
writePinLow(ADNS_CS_PIN);
wait_us(1);
writePinHigh(ADNS_CS_PIN);
}

void adns_cs_select(void) {
writePinLow(ADNS_CS_PIN);
}

void adns_cs_deselect(void) {
writePinHigh(ADNS_CS_PIN);
}

uint8_t adns_serial_read(void) {
setPinInput(ADNS_SDIO_PIN);
uint8_t byte = 0;

for (uint8_t i = 0; i < 8; ++i) {
writePinLow(ADNS_SCLK_PIN);
wait_us(1);

byte = (byte << 1) | readPin(ADNS_SDIO_PIN);

writePinHigh(ADNS_SCLK_PIN);
wait_us(1);
}

return byte;
}

void adns_serial_write(uint8_t data) {
setPinOutput(ADNS_SDIO_PIN);

for (int8_t b = 7; b >= 0; b--) {
writePinLow(ADNS_SCLK_PIN);

if (data & (1 << b))
writePinHigh(ADNS_SDIO_PIN);
else
writePinLow(ADNS_SDIO_PIN);

wait_us(2);

writePinHigh(ADNS_SCLK_PIN);
}

// tSWR. See page 15 of the ADNS spec sheet.
// Technically, this is only necessary if the next operation is an SDIO
// read. This is not guaranteed to be the case, but we're being lazy.
wait_us(4);

// Note that tSWW is never necessary. All write operations require at
// least 32us, which exceeds tSWW, so there's never a need to wait for it.
}

// Read a byte of data from a register on the ADNS.
// Don't forget to use the register map (as defined in the header file).
uint8_t adns_read_reg(uint8_t reg_addr) {
adns_cs_select();

adns_serial_write(reg_addr);

// We don't need a minimum tSRAD here. That's because a 4ms wait time is
// already included in adns_serial_write(), so we're good.
// See page 10 and 15 of the ADNS spec sheet.
//wait_us(4);

uint8_t byte = adns_serial_read();

// tSRW & tSRR. See page 15 of the ADNS spec sheet.
// Technically, this is only necessary if the next operation is an SDIO
// read or write. This is not guaranteed to be the case.
// Honestly, this wait could probably be removed.
wait_us(1);

adns_cs_deselect();

return byte;
}

void adns_write_reg(uint8_t reg_addr, uint8_t data) {
adns_cs_select();
adns_serial_write(reg_addr);
adns_serial_write(data);
adns_cs_deselect();
}

report_adns_t adns_read_burst(void) {
adns_cs_select();

report_adns_t data;
data.dx = 0;
data.dy = 0;

adns_serial_write(REG_MOTION_BURST);

// We don't need a minimum tSRAD here. That's because a 4ms wait time is
// already included in adns_serial_write(), so we're good.
// See page 10 and 15 of the ADNS spec sheet.
//wait_us(4);

uint8_t x = adns_serial_read();
uint8_t y = adns_serial_read();

// Burst mode returns a bunch of other shit that we don't really need.
// Setting CS to high ends burst mode early.
adns_cs_deselect();

data.dx = convert_twoscomp(x);
data.dy = convert_twoscomp(y);

return data;
}

// Convert a two's complement byte from an unsigned data type into a signed
// data type.
int8_t convert_twoscomp(uint8_t data) {
if ((data & 0x80) == 0x80)
return -128 + (data & 0x7F);
else
return data;
}

// Don't forget to use the definitions for CPI in the header file.
void adns_set_cpi(uint8_t cpi) {
adns_write_reg(REG_MOUSE_CONTROL2, cpi);
}

bool adns_check_signature(void) {
uint8_t pid = adns_read_reg(REG_PRODUCT_ID);
uint8_t rid = adns_read_reg(REG_REVISION_ID);
uint8_t pid2 = adns_read_reg(REG_PRODUCT_ID2);

return (pid == 0x12 && rid == 0x01 && pid2 == 0x26);
}
79 changes: 79 additions & 0 deletions keyboards/ploopyco/adns5050.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Copyright 2021 Colin Lam (Ploopy Corporation)
* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
* Copyright 2019 Sunjun Kim
* Copyright 2019 Hiroyuki Okada
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <stdbool.h>

// Registers
#define REG_PRODUCT_ID 0x00
#define REG_REVISION_ID 0x01
#define REG_MOTION 0x02
#define REG_DELTA_X 0x03
#define REG_DELTA_Y 0x04
#define REG_SQUAL 0x05
#define REG_SHUTTER_UPPER 0x06
#define REG_SHUTTER_LOWER 0x07
#define REG_MAXIMUM_PIXEL 0x08
#define REG_PIXEL_SUM 0x09
#define REG_MINIMUM_PIXEL 0x0a
#define REG_PIXEL_GRAB 0x0b
#define REG_MOUSE_CONTROL 0x0d
#define REG_MOUSE_CONTROL2 0x19
#define REG_LED_DC_MODE 0x22
#define REG_CHIP_RESET 0x3a
#define REG_PRODUCT_ID2 0x3e
#define REG_INV_REV_ID 0x3f
#define REG_MOTION_BURST 0x63

// CPI values
#define CPI125 0x11
#define CPI250 0x12
#define CPI375 0x13
#define CPI500 0x14
#define CPI625 0x15
#define CPI750 0x16
#define CPI875 0x17
#define CPI1000 0x18
#define CPI1125 0x19
#define CPI1250 0x1a
#define CPI1375 0x1b

#ifdef CONSOLE_ENABLE
void print_byte(uint8_t byte);
#endif

typedef struct {
int8_t dx;
int8_t dy;
} report_adns_t;

// A bunch of functions to implement the ADNS5050-specific serial protocol.
// Note that the "serial.h" driver is insufficient, because it does not
// manually manipulate a serial clock signal.
void adns_init(void);
void adns_sync(void);
uint8_t adns_serial_read(void);
void adns_serial_write(uint8_t data);
uint8_t adns_read_reg(uint8_t reg_addr);
void adns_write_reg(uint8_t reg_addr, uint8_t data);
report_adns_t adns_read_burst(void);
int8_t convert_twoscomp(uint8_t data);
void adns_set_cpi(uint8_t cpi);
bool adns_check_signature(void);
59 changes: 59 additions & 0 deletions keyboards/ploopyco/trackball_mini/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright 2021 Colin Lam (Ploopy Corporation)
* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
* Copyright 2019 Sunjun Kim
* Copyright 2019 Hiroyuki Okada
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "config_common.h"

/* USB Device descriptor parameter */
#define VENDOR_ID 0x5043
#define PRODUCT_ID 0x1EAB
#define DEVICE_VER 0x0001
#define MANUFACTURER PloopyCo
#define PRODUCT Trackball Mini

/* key matrix size */
#define MATRIX_ROWS 1
#define MATRIX_COLS 5


/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
#define DEBOUNCE 5

/* define if matrix has ghost (lacks anti-ghosting diodes) */
//#define MATRIX_HAS_GHOST

/* disable action features */
//#define NO_ACTION_LAYER
//#define NO_ACTION_TAPPING
//#define NO_ACTION_ONESHOT
#define NO_ACTION_MACRO
#define NO_ACTION_FUNCTION

/* Much more so than a keyboard, speed matters for a mouse. So we'll go for as high
a polling rate as possible. */
#define USB_POLLING_INTERVAL_MS 1
#define USB_MAX_POWER_CONSUMPTION 100

/* Bootmagic Lite key configuration */
#define BOOTMAGIC_LITE_ROW 0
#define BOOTMAGIC_LITE_COLUMN 3

// If board has a debug LED, you can enable it by defining this
// #define DEBUG_LED_PIN F7
19 changes: 19 additions & 0 deletions keyboards/ploopyco/trackball_mini/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"keyboard_name": "Ploopy Trackball Mini",
"url": "www.ploopy.co",
"maintainer": "ploopyco",
"manufacturer": "Ploopy Corporation",
"width": 8,
"height": 3,
"layouts": {
"LAYOUT": {
"layout": [
{"x":0, "y":0, "h":2},
{"x":1, "y":0.25, "h":1.5},
{"x":2, "y":0, "h":2},
{"x":3.5, "y":0, "h":2},
{"x":4.5, "y":0, "h":2}
]
}
}
}
28 changes: 28 additions & 0 deletions keyboards/ploopyco/trackball_mini/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2021 Colin Lam (Ploopy Corporation)
* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
* Copyright 2019 Sunjun Kim
* Copyright 2019 Hiroyuki Okada
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include QMK_KEYBOARD_H

// safe range starts at `PLOOPY_SAFE_RANGE` instead.

const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
[0] = LAYOUT( /* Base */
KC_BTN1, KC_BTN3, KC_BTN2,
KC_BTN4, KC_BTN5
),
};
3 changes: 3 additions & 0 deletions keyboards/ploopyco/trackball_mini/keymaps/default/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The default keymap for the Ploopy Trackball Mini.

Note that kits bought from PloopyCo actually ship with the VIA keymap, not this one.
Loading

0 comments on commit 88fb9b7

Please sign in to comment.