Skip to content

Commit

Permalink
Add RP2040 SCRAMBLE v2 (qmk#19489)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaygreco authored Jan 27, 2023
1 parent 6e42b58 commit b3dca4b
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 51 deletions.
21 changes: 3 additions & 18 deletions keyboards/nullbitsco/scramble/rules.mk
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
# MCU name
MCU = atmega328p
# NOTE: This file is shared and only exists to set the default build
# The real build rules are set in the v1/v2 directories

# Bootloader selection
BOOTLOADER = usbasploader

# Build Options
# change yes to no to disable
#
BOOTMAGIC_ENABLE = no # Enable Bootmagic Lite
MOUSEKEY_ENABLE = yes # Mouse keys
EXTRAKEY_ENABLE = yes # Audio control and System control
CONSOLE_ENABLE = no # Console for debug
COMMAND_ENABLE = no # Commands for debug and configuration
NKRO_ENABLE = no # Enable N-Key Rollover
BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality
RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow
AUDIO_ENABLE = no # Audio output
ENCODER_ENABLE = yes # Use rotary encoder
DEFAULT_FOLDER = nullbitsco/scramble/v2
22 changes: 0 additions & 22 deletions keyboards/nullbitsco/scramble/scramble.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,3 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include QMK_KEYBOARD_H

// place overrides here
void set_scramble_LED(uint8_t mode) {
switch(mode) {
case LED_ON:
setPinOutput(PIN_LED);
writePin(PIN_LED, GPIO_STATE_HIGH);
break;

case LED_DIM:
setPinInput(PIN_LED);
break;

case LED_OFF:
setPinOutput(PIN_LED);
writePin(PIN_LED, GPIO_STATE_LOW);
break;

default:
break;
}
}
17 changes: 6 additions & 11 deletions keyboards/nullbitsco/scramble/scramble.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,13 @@

#include "quantum.h"

// Indication LED settings
#define LED_ON 2
#define LED_DIM 1
#define LED_OFF 0

#define GPIO_STATE_LOW 0
#define GPIO_STATE_HIGH 1

#define PIN_LED B2

void set_scramble_LED(uint8_t mode);
#if defined(KEYBOARD_nullbitsco_scramble_v1)
#include "v1.h"
#elif defined(KEYBOARD_nullbitsco_scramble_v2)
#include "v2.h"
#endif

// Layout is the same in all revisions
#define LAYOUT( \
K01, K02, K03, \
K11, K12, K13 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* NOTE: This config file is specific to AVR builds. */

#pragma once

#include "config_common.h"
Expand Down
21 changes: 21 additions & 0 deletions keyboards/nullbitsco/scramble/v1/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# NOTE: This file is specific to AVR builds.

# MCU name
MCU = atmega328p

# Bootloader selection
BOOTLOADER = usbasploader

# Build Options
# change yes to no to disable
#
BOOTMAGIC_ENABLE = no # Enable Bootmagic Lite
MOUSEKEY_ENABLE = yes # Mouse keys
EXTRAKEY_ENABLE = yes # Audio control and System control
CONSOLE_ENABLE = no # Console for debug
COMMAND_ENABLE = no # Commands for debug and configuration
NKRO_ENABLE = no # Enable N-Key Rollover
BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality
RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow
AUDIO_ENABLE = no # Audio output
ENCODER_ENABLE = yes # Use rotary encoder
25 changes: 25 additions & 0 deletions keyboards/nullbitsco/scramble/v1/v1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2022 Jay Greco
// SPDX-License-Identifier: GPL-2.0-or-later

#include "v1.h"

void set_scramble_LED(uint8_t mode) {
switch(mode) {
case LED_ON:
setPinOutput(PIN_LED);
writePin(PIN_LED, GPIO_STATE_HIGH);
break;

case LED_DIM:
setPinInput(PIN_LED);
break;

case LED_OFF:
setPinOutput(PIN_LED);
writePin(PIN_LED, GPIO_STATE_LOW);
break;

default:
break;
}
}
18 changes: 18 additions & 0 deletions keyboards/nullbitsco/scramble/v1/v1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2022 Jay Greco
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "scramble.h"

// Indication LED settings
#define LED_ON 2
#define LED_DIM 1
#define LED_OFF 0

#define GPIO_STATE_LOW 0
#define GPIO_STATE_HIGH 1

#define PIN_LED B2

void set_scramble_LED(uint8_t mode);
42 changes: 42 additions & 0 deletions keyboards/nullbitsco/scramble/v2/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright 2021 Jay Greco
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/>.
*/

/* NOTE: This config file is specific to RP2040 builds. */

#pragma once

#include "config_common.h"

/* key matrix size */
#define MATRIX_ROWS 2
#define MATRIX_COLS 3

#define DIRECT_PINS {{GP6,GP8,GP10}, {GP29,GP28,GP22}}

/* Set 0 if debouncing isn't needed */
#define DEBOUNCE 10

/* Optional encoder pins */
#define ENCODERS_PAD_A { GP24 }
#define ENCODERS_PAD_B { GP25 }
#define TAP_CODE_DELAY 10

/* RP2040-specific defines*/
#define RP2040_FLASH_GENERIC_03H
#define I2C1_SDA_PIN GP26
#define I2C1_SCL_PIN GP27
#define I2C_DRIVER I2CD2
9 changes: 9 additions & 0 deletions keyboards/nullbitsco/scramble/v2/halconf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2022 Jay Greco
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#define HAL_USE_I2C TRUE
#define HAL_USE_PWM TRUE

#include_next <halconf.h>
14 changes: 14 additions & 0 deletions keyboards/nullbitsco/scramble/v2/mcuconf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2022 Jay Greco
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include_next <mcuconf.h>

#undef RP_I2C_USE_I2C1
#define RP_I2C_USE_I2C1 TRUE

#undef RP_PWM_USE_PWM1
#define RP_PWM_USE_PWM1 TRUE
#undef RP_PWM_USE_PWM2
#define RP_PWM_USE_PWM2 TRUE
21 changes: 21 additions & 0 deletions keyboards/nullbitsco/scramble/v2/rules.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# NOTE: This file is is specific to RP2040 builds.

# MCU name
MCU = RP2040

# Bootloader selection
BOOTLOADER = rp2040

# Build Options
# change yes to no to disable
#
BOOTMAGIC_ENABLE = no # Enable Bootmagic Lite
MOUSEKEY_ENABLE = yes # Mouse keys
EXTRAKEY_ENABLE = yes # Audio control and System control
CONSOLE_ENABLE = no # Console for debug
COMMAND_ENABLE = no # Commands for debug and configuration
NKRO_ENABLE = no # Enable N-Key Rollover
BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality
RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow
AUDIO_ENABLE = no # Audio output
ENCODER_ENABLE = yes # Use rotary encoder
84 changes: 84 additions & 0 deletions keyboards/nullbitsco/scramble/v2/v2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2022 Jay Greco
// SPDX-License-Identifier: GPL-2.0-or-later

#include "v2.h"

// SCRAMBLE PWM LED config
pwm_led_t scramble_rgb = {
{GP18, GP19, GP20},
{&PWMD1, &PWMD1, &PWMD2},
{0, 1, 0},
PWM_OUTPUT_ACTIVE_LOW,
0
};

// Internal PWM init
// only runs once per PWM LED
void _init_pwm(pwm_led_t* led) {
if (!led->init_complete) {
for (int i=0; i<NUM_RGB_IDX; i++) {
palSetPadMode(PAL_PORT(led->pin[i]), PAL_PAD(led->pin[i]), PWM_PAL_MODE);

static PWMConfig pwmCFG = {
.frequency = PWM_PWM_COUNTER_FREQUENCY,
.period = PWM_PWM_PERIOD,
};

// Channels are always configured as active low
// If active high is needed, pwm is inverted in _set_led_pwm()
pwmCFG.channels[0].mode = PWM_OUTPUT_ACTIVE_LOW;
pwmCFG.channels[1].mode = PWM_OUTPUT_ACTIVE_LOW;
pwmStart(led->driver[i], &pwmCFG);

// Start LEDs in the OFF state
uint8_t pwm = led->mode == PWM_OUTPUT_ACTIVE_HIGH ? 100 : 0;
pwmEnableChannel(led->driver[i], led->channel[i], PWM_FRACTION_TO_WIDTH(led->driver[i], 99, pwm));
}

led->init_complete = 1;
}
}

// Internal generic PWM setter
void _set_led_pwm(uint8_t pwm, pwm_led_t* led, uint8_t channel) {
if (pwm > 100) pwm = 100;
if (led->mode == PWM_OUTPUT_ACTIVE_HIGH) pwm = (100 - pwm);

_init_pwm(led);
pwmEnableChannel(led->driver[channel], led->channel[channel], PWM_FRACTION_TO_WIDTH(led->driver[channel], 99, pwm));
}

// SCRAMBLE
void set_scramble_LED(uint8_t mode) {
switch(mode) {
case LED_ON:
set_scramble_LED_rgb_pwm(65, 100, 95);
break;

case LED_DIM:
set_scramble_LED_rgb_pwm(3, 9, 3);
break;

default: //LED_OFF
set_scramble_LED_rgb_pwm(0, 0, 0);
break;
}
}

void set_scramble_LED_rgb_pwm(uint8_t r_pwm, uint8_t g_pwm, uint8_t b_pwm) {
_set_led_pwm(r_pwm, &scramble_rgb, RED);
_set_led_pwm(g_pwm, &scramble_rgb, GREEN);
_set_led_pwm(b_pwm, &scramble_rgb, BLUE);
}

void set_scramble_LED_r_pwm(uint8_t pwm) {
_set_led_pwm(pwm, &scramble_rgb, RED);
}

void set_scramble_LED_g_pwm(uint8_t pwm) {
_set_led_pwm(pwm, &scramble_rgb, GREEN);
}

void set_scramble_LED_b_pwm(uint8_t pwm) {
_set_led_pwm(pwm, &scramble_rgb, BLUE);
}
43 changes: 43 additions & 0 deletions keyboards/nullbitsco/scramble/v2/v2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2022 Jay Greco
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "scramble.h"
#include <hal.h>

enum led_mode {
LED_OFF = 0,
LED_DIM,
LED_ON,
NUM_LED_MODE
};

enum rgb_idx {
RED = 0,
GREEN,
BLUE,
NUM_RGB_IDX
};

typedef struct pwm_led_t {
uint32_t pin[3];
PWMDriver* driver[3];
uint8_t channel[3];
uint8_t mode;
uint8_t init_complete;
} pwm_led_t;

#define PWM_PAL_MODE (PAL_MODE_ALTERNATE_PWM | PAL_RP_PAD_DRIVE12 | PAL_RP_GPIO_OE)
#define PWM_PWM_COUNTER_FREQUENCY 1000000
#define PWM_PWM_PERIOD PWM_PWM_COUNTER_FREQUENCY / 1000

// RP2040 adds HW PWM control!
// PWM values are in percent, 0-100
void
set_scramble_LED(uint8_t mode),
set_scramble_LED_rgb_pwm(uint8_t r_pwm, uint8_t g_pwm, uint8_t b_pwm),
set_scramble_LED_r_pwm(uint8_t pwm),
set_scramble_LED_g_pwm(uint8_t pwm),
set_scramble_LED_b_pwm(uint8_t pwm);

0 comments on commit b3dca4b

Please sign in to comment.