bface refactor (#6333)
* remove custom matrix support * remove custom i2c and led driver * minor cleanups * update readmeplanck_thk^2
commit
7ba82cb5b7
@ -1,94 +0,0 @@ |
|||||||
/* Copyright 2017 Sebastian Kaim
|
|
||||||
* |
|
||||||
* 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/>.
|
|
||||||
*/ |
|
||||||
|
|
||||||
#ifdef BACKLIGHT_ENABLE |
|
||||||
|
|
||||||
#include "backlight_ps2avrGB.h" |
|
||||||
#define sbi(reg,bit) reg |= (_BV(bit)) |
|
||||||
#define cbi(reg,bit) reg &= (~_BV(bit)) |
|
||||||
#define PWM10 WGM10 |
|
||||||
#define PWM11 WGM11 |
|
||||||
#define COM1x1 COM1B1 |
|
||||||
#define OCR1x OCR1B |
|
||||||
|
|
||||||
void backlight_init_ports(void) |
|
||||||
{ |
|
||||||
#if BACKLIGHT_ON_STATE == 0 |
|
||||||
backlight_off(); |
|
||||||
#else |
|
||||||
backlight_on(); |
|
||||||
#endif |
|
||||||
|
|
||||||
// setup pwm
|
|
||||||
// this bitmagic is sourced from the original firmware
|
|
||||||
/*TCCR1B = ((TCCR1B & ~0x07) | 0x03);
|
|
||||||
TCNT1H = 0; |
|
||||||
TCNT1L = 0; |
|
||||||
sbi(TIMSK, TOIE1); |
|
||||||
OCR1BH = 0; |
|
||||||
OCR1BL = 0; |
|
||||||
cbi(TCCR1A,PWM11); |
|
||||||
sbi(TCCR1A,PWM10); |
|
||||||
sbi(TCCR1A,COM1B1); |
|
||||||
cbi(TCCR1A,COM1B0);*/ |
|
||||||
ICR1 = 0xFFFF; |
|
||||||
|
|
||||||
TCCR1A = _BV(COM1x1) | _BV(WGM11); // = 0b00001010;
|
|
||||||
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
|
|
||||||
|
|
||||||
backlight_init(); |
|
||||||
} |
|
||||||
|
|
||||||
void backlight_set(uint8_t level) |
|
||||||
{ |
|
||||||
if( level == 0 ) { |
|
||||||
backlight_off(); |
|
||||||
} |
|
||||||
else { |
|
||||||
backlight_on(); |
|
||||||
/*uint8_t pwm = get_pwm_for_brightness(level);
|
|
||||||
set_backlight_pwm(pwm); |
|
||||||
TCCR1A |= _BV(COM1x1); |
|
||||||
OCR1x = (level >= 2) ? 0xFFFF : 0x00FF;*/ |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
#define PWM_MAX 0xFF |
|
||||||
uint8_t get_pwm_for_brightness(uint8_t level) |
|
||||||
{ |
|
||||||
// we need to cast up here to allow multiplication with 0xFF. We could also use floats, but this is probably a lot faster.
|
|
||||||
uint16_t brightness = (uint16_t)level * (uint16_t)PWM_MAX / (uint16_t)BACKLIGHT_LEVELS; |
|
||||||
return (uint8_t)brightness; |
|
||||||
} |
|
||||||
|
|
||||||
void backlight_on(void) |
|
||||||
{ |
|
||||||
//_SFR_IO8(0x12) |= _BV(0x4);
|
|
||||||
LED_PIN |= BACKLIGHT_PORT_NUM; |
|
||||||
} |
|
||||||
|
|
||||||
void backlight_off(void) |
|
||||||
{ |
|
||||||
//_SFR_IO8(0x12) &= ~_BV(0x4);
|
|
||||||
LED_PIN &= ~BACKLIGHT_PORT_NUM; |
|
||||||
} |
|
||||||
|
|
||||||
void set_backlight_pwm(uint8_t level) { |
|
||||||
// this does not work (yet)
|
|
||||||
//OCR1B = level;
|
|
||||||
} |
|
||||||
|
|
||||||
#endif // BACKLIGHT_ENABLE
|
|
@ -1,35 +0,0 @@ |
|||||||
/* Copyright 2017 Sebastian Kaim
|
|
||||||
* |
|
||||||
* 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/>.
|
|
||||||
*/ |
|
||||||
|
|
||||||
|
|
||||||
#if defined(__AVR__) |
|
||||||
#include <avr/pgmspace.h> |
|
||||||
#include <avr/io.h> |
|
||||||
#include <avr/interrupt.h> |
|
||||||
#endif |
|
||||||
#include <stddef.h> |
|
||||||
#include <stdlib.h> |
|
||||||
#include "backlight.h" |
|
||||||
|
|
||||||
#ifndef PS2AVRGB_BACKLIGHT_H |
|
||||||
#define PS2AVRGB_BACKLIGHT_H |
|
||||||
|
|
||||||
uint8_t get_pwm_for_brightness(uint8_t level); |
|
||||||
void set_backlight_pwm(uint8_t level); |
|
||||||
void backlight_on(void); |
|
||||||
void backlight_off(void); |
|
||||||
|
|
||||||
#endif |
|
@ -1,104 +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/>.
|
|
||||||
*/ |
|
||||||
|
|
||||||
#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; |
|
||||||
} |
|
@ -1,25 +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/>.
|
|
||||||
*/ |
|
||||||
|
|
||||||
#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 |
|
@ -1,113 +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_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; |
|
||||||
} |
|
||||||
|
|
||||||
// activate backlight
|
|
||||||
//PORTD |= (1 << 4);
|
|
||||||
//_SFR_IO8(0x09) |= (1 << 4); //_BV(0x94 & 0xF);
|
|
||||||
//
|
|
||||||
// this is the code that *should* be executed in quantum.c
|
|
||||||
_SFR_IO8(0x12) |= _BV(0x4); |
|
||||||
} |
|
||||||
|
|
||||||
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; |
|
||||||
} |
|
||||||
|
|
||||||
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_user(); |
|
||||||
|
|
||||||
return 1; |
|
||||||
} |
|
||||||
|
|
||||||
inline matrix_row_t matrix_get_row(uint8_t row) { |
|
||||||
return matrix[row]; |
|
||||||
} |
|
||||||
|
|
||||||
void matrix_print(void) { |
|
||||||
} |
|
Loading…
Reference in new issue