~ruther/qmk_firmware

3e26886700a42fa6f66e934d4981b3e285784cd9 — MechMerlin 6 years ago 0456bd6
[Keyboard] Refactor Singa (#5579)

* pragma once it all

* put amnesia's name everywhere

* fixup readme

* remove custom matrix

* remove unneccessary configs

* disable bootmagic

* remove custom i2c in favor of i2c_master

* fix rgb led num

* add in rgb underglow support
M keyboards/singa/config.h => keyboards/singa/config.h +2 -7
@@ 15,8 15,7 @@ You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef CONFIG_H
#define CONFIG_H
#pragma once

#include "config_common.h"



@@ 25,7 24,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
#define MANUFACTURER    Singa Keyboards
#define PRODUCT         Singa

#define RGBLED_NUM 16
#define RGBLED_NUM 18

#define MATRIX_ROWS 7
#define MATRIX_COLS 15


@@ 40,7 39,3 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
#define NO_BACKLIGHT_CLOCK
#define BACKLIGHT_LEVELS 1
#define RGBLIGHT_ANIMATIONS

#define NO_UART 1

#endif

D keyboards/singa/i2c.c => keyboards/singa/i2c.c +0 -106
@@ 1,106 0,0 @@
/*
Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>

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/>.
*/

// Please do not modify this file

#include <avr/io.h>
#include <util/twi.h>

#include "i2c.h"

void i2c_set_bitrate(uint16_t bitrate_khz) {
    uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz);
    if (bitrate_div >= 16) {
        bitrate_div = (bitrate_div - 16) / 2;
    }
    TWBR = bitrate_div;
}

void i2c_init(void) {
    // set pull-up resistors on I2C bus pins
    PORTC |= 0b11;

    i2c_set_bitrate(400);

    // enable TWI (two-wire interface)
    TWCR |= (1 << TWEN);

    // enable TWI interrupt and slave address ACK
    TWCR |= (1 << TWIE);
    TWCR |= (1 << TWEA);
}

uint8_t i2c_start(uint8_t address) {
    // reset TWI control register
    TWCR = 0;

    // begin transmission and wait for it to end
    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));

    // check if the start condition was successfully transmitted
    if ((TWSR & 0xF8) != TW_START) {
        return 1;
    }

    // transmit address and wait
    TWDR = address;
    TWCR = (1<<TWINT) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));

    // check if the device has acknowledged the READ / WRITE mode
    uint8_t twst = TW_STATUS & 0xF8;
    if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
        return 1;
    }

    return 0;
}

void i2c_stop(void) {
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
}

uint8_t i2c_write(uint8_t data) {
    TWDR = data;

    // transmit data and wait
    TWCR = (1<<TWINT) | (1<<TWEN);
    while (!(TWCR & (1<<TWINT)));

    if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
        return 1;
    }

    return 0;
}

uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) {
    if (i2c_start(address)) {
        return 1;
    }

    for (uint16_t i = 0; i < length; i++) {
        if (i2c_write(data[i])) {
            return 1;
        }
    }

    i2c_stop();

    return 0;
}

D keyboards/singa/i2c.h => keyboards/singa/i2c.h +0 -27
@@ 1,27 0,0 @@
/*
Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>

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/>.
*/

// Please do not modify this file

#ifndef __I2C_H__
#define __I2C_H__

void i2c_init(void);
void i2c_set_bitrate(uint16_t bitrate_khz);
uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length);

#endif

M keyboards/singa/keymaps/amnesia0287/config.h => keyboards/singa/keymaps/amnesia0287/config.h +1 -1
@@ 1,4 1,4 @@
/* Copyright 2018 REPLACE_WITH_YOUR_NAME
/* Copyright 2018 amnesia0287
 *
 * 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

M keyboards/singa/keymaps/amnesia0287/keymap.c => keyboards/singa/keymaps/amnesia0287/keymap.c +1 -1
@@ 1,4 1,4 @@
/* Copyright 2018 REPLACE_WITH_YOUR_NAME
/* Copyright 2018 amnesia0287
 *
 * 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

M keyboards/singa/keymaps/default/config.h => keyboards/singa/keymaps/default/config.h +1 -1
@@ 1,4 1,4 @@
/* Copyright 2018 REPLACE_WITH_YOUR_NAME
/* Copyright 2018 amnesia0287
 *
 * 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

M keyboards/singa/keymaps/default/keymap.c => keyboards/singa/keymaps/default/keymap.c +1 -1
@@ 1,4 1,4 @@
/* Copyright 2018 REPLACE_WITH_YOUR_NAME
/* Copyright 2018 amnesia0287
 *
 * 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

M keyboards/singa/keymaps/test/config.h => keyboards/singa/keymaps/test/config.h +1 -1
@@ 1,4 1,4 @@
/* Copyright 2018 REPLACE_WITH_YOUR_NAME
/* Copyright 2018 amnesia0287
 *
 * 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

M keyboards/singa/keymaps/test/keymap.c => keyboards/singa/keymaps/test/keymap.c +1 -1
@@ 1,4 1,4 @@
/* Copyright 2018 REPLACE_WITH_YOUR_NAME
/* Copyright 2018 amnesia0287
 *
 * 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

D keyboards/singa/matrix.c => keyboards/singa/matrix.c +0 -112
@@ 1,112 0,0 @@
/*
Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>

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 <avr/io.h>
#include <util/delay.h>

#include "matrix.h"

#ifndef DEBOUNCE
#   define DEBOUNCE	5
#endif

static uint8_t debouncing = DEBOUNCE;

static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_debouncing[MATRIX_ROWS];

void matrix_set_row_status(uint8_t row);
uint8_t bit_reverse(uint8_t x);

void matrix_init(void) {
    // all outputs for rows high
    DDRB = 0xFF;
    PORTB = 0xFF;
    // all inputs for columns
    DDRA = 0x00;
    DDRC &= ~(0x111111<<2);
    DDRD &= ~(1<<PIND7);
    // all columns are pulled-up
    PORTA = 0xFF;
    PORTC |= (0b111111<<2);
    PORTD |= (1<<PIND7);

    // initialize matrix state: all keys off
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        matrix[row] = 0x00;
        matrix_debouncing[row] = 0x00;
    }

    matrix_init_quantum();
}

uint8_t matrix_scan(void) {
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        matrix_set_row_status(row);
        _delay_us(5);

        matrix_row_t cols = (
            // cols 0..7, PORTA 0 -> 7
            (~PINA) & 0xFF
        ) | (
            // cols 8..13, PORTC 7 -> 0
            bit_reverse((~PINC) & 0xFF) << 8
        ) | (
            // col 14, PORTD 7
            ((~PIND) & (1 << PIND7)) << 7
        );

        if (matrix_debouncing[row] != cols) {
            matrix_debouncing[row] = cols;
            debouncing = DEBOUNCE;
        }
    }

    if (debouncing) {
        if (--debouncing) {
            _delay_ms(1);
        } else {
            for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
                matrix[i] = matrix_debouncing[i];
            }
        }
    }

    matrix_scan_quantum();

    return 1;
}

// declarations
void matrix_set_row_status(uint8_t row) {
    DDRB = (1 << row);
    PORTB = ~(1 << row);
}

uint8_t bit_reverse(uint8_t x) {
    x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
    x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
    x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
    return x;
}

inline matrix_row_t matrix_get_row(uint8_t row) {
    return matrix[row];
}

void matrix_print(void) {
}

M keyboards/singa/readme.md => keyboards/singa/readme.md +4 -4
@@ 4,7 4,7 @@
![Singa](https://i.imgur.com/VVO27Tr.jpg)


A short description of the keyboard/project
75% Custom Keyboard. 


Keyboard Maintainer: [amnesia0287](https://github.com/amnesia0287)  


@@ 38,9 38,9 @@ macOS:
    ```
3. Install the following packages:
    ```
    brew install python
    brew install pyusb
    brew install --HEAD`https://raw.githubusercontent.com/robertgzr/homebrew-tap/master/bootloadhid.rb
    brew install python3
    pip3 install pyusb
    brew install --HEAD https://raw.githubusercontent.com/robertgzr/homebrew-tap/master/bootloadhid.rb

4. Place your keyboard into reset. 
5. Flash the board by typing `bootloadHID -r` followed by the path to your `.hex` file. 

M keyboards/singa/rules.mk => keyboards/singa/rules.mk +4 -6
@@ 31,20 31,18 @@ F_CPU = 12000000
BOOTLOADER = bootloadHID

# build options
BOOTMAGIC_ENABLE = yes
BOOTMAGIC_ENABLE = no
MOUSEKEY_ENABLE = yes
EXTRAKEY_ENABLE = yes
CONSOLE_ENABLE = yes
COMMAND_ENABLE = yes
BACKLIGHT_ENABLE = no
RGBLIGHT_ENABLE = no
BACKLIGHT_ENABLE = yes
RGBLIGHT_ENABLE = yes
RGBLIGHT_CUSTOM_DRIVER = yes

OPT_DEFS = -DDEBUG_LEVEL=0

# custom matrix setup
CUSTOM_MATRIX = yes
SRC = matrix.c i2c.c
SRC = i2c_master.c

# programming options
PROGRAM_CMD = ./util/atmega32a_program.py $(TARGET).hex

M keyboards/singa/singa.c => keyboards/singa/singa.c +44 -19
@@ 1,4 1,4 @@
/* Copyright 2018 REPLACE_WITH_YOUR_NAME
/* Copyright 2018 amnesia0287
 *
 * 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


@@ 13,32 13,57 @@
 * 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

#include <avr/pgmspace.h>
#include "action_layer.h"
#include "i2c.h"
#include "rgblight.h"
#include "i2c_master.h"
#include "quantum.h"

#ifdef RGBLIGHT_ENABLE
extern rgblight_config_t rgblight_config;

void rgblight_set(void) {
    if (!rgblight_config.enable) {
        for (uint8_t i = 0; i < RGBLED_NUM; i++) {
            led[i].r = 0;
            led[i].g = 0;
            led[i].b = 0;
        }
    }

// for keyboard subdirectory level init functions
// @Override
void matrix_init_kb(void) {
  // call user level keymaps, if any
  matrix_init_user();
    i2c_init();
    i2c_transmit(0xb0, (uint8_t*)led, 3 * RGBLED_NUM, 100);
}
#endif

void matrix_scan_kb(void) {
  matrix_scan_user();
  /* Nothing else for now. */
__attribute__ ((weak))
void matrix_scan_user(void) {
}

__attribute__((weak)) // overridable
void matrix_init_user(void) {
void backlight_init_ports(void) {
    // initialize pins D0, D1, D4 and D6 as output
    setPinOutput(D0);
    setPinOutput(D1);
    setPinOutput(D4);
    setPinOutput(D6);

    // turn RGB LEDs on
    writePinHigh(D0);
    writePinHigh(D1);
    writePinHigh(D4);
    writePinHigh(D6);
}

__attribute__((weak)) // overridable
void matrix_scan_user(void) {

void backlight_set(uint8_t level) {
	if (level == 0) {
        // turn RGB LEDs off
        writePinLow(D0);
        writePinLow(D1);
        writePinLow(D4);
        writePinLow(D6);
	} else {
        // turn RGB LEDs on
        writePinHigh(D0);
        writePinHigh(D1);
        writePinHigh(D4);
        writePinHigh(D6);
	}
}

M keyboards/singa/singa.h => keyboards/singa/singa.h +2 -5
@@ 1,4 1,4 @@
/* Copyright 2018 REPLACE_WITH_YOUR_NAME
/* Copyright 2018 amnesia0287
 *
 * 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


@@ 13,8 13,7 @@
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef SINGA_H
#define SINGA_H
#pragma once

#include "quantum.h"



@@ 139,5 138,3 @@
  { k74,   k81,   k75,   KC_NO, KC_NO, k76,   KC_NO, KC_NO, k77,   KC_NO, k82,   KC_NO, k78,   k79, k80 }, \
  { KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, k15, k30 } \
}

#endif

M keyboards/singa/usbconfig.h => keyboards/singa/usbconfig.h +1 -4
@@ 8,8 8,7 @@
 * This Revision: $Id: usbconfig-prototype.h 785 2010-05-30 17:57:07Z cs $
 */

#ifndef __usbconfig_h_included__
#define __usbconfig_h_included__
#pragma once

#include "config.h"



@@ 392,5 391,3 @@ section at the end of this file).
/* #define USB_INTR_PENDING        EIFR */
#define USB_INTR_PENDING_BIT    INTF1
#define USB_INTR_VECTOR         INT1_vect

#endif /* __usbconfig_h_included__ */