Skip to content

Commit

Permalink
Keyboard: Improvement of crkbd communication functions (based on heli…
Browse files Browse the repository at this point in the history
…x-keyboard) (qmk#3798)

* improvement of crkbd communication functions (based on  helix-keyboard)

* Removed unnecessary code.

* Changed read restriction from #define to #pragma once.

* Changed from sizeof to defined size.

* moved lib folder to crkbdroot.
removed warning of ws2812.d
  • Loading branch information
marksard authored and drashna committed Sep 3, 2018
1 parent fa1ee47 commit 35efcc9
Show file tree
Hide file tree
Showing 31 changed files with 580 additions and 233 deletions.
13 changes: 10 additions & 3 deletions keyboards/crkbd/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ 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"
#include <serial_config.h>

#endif
#ifdef USE_Link_Time_Optimization
// LTO has issues with macros (action_get_macro) and "functions" (fn_actions),
// so just disable them
#define NO_ACTION_MACRO
#define NO_ACTION_FUNCTION

#define DISABLE_LEADER
#endif // USE_Link_Time_Optimization
5 changes: 1 addition & 4 deletions keyboards/crkbd/crkbd.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#ifndef CRKBD_H
#define CRKBD_H
#pragma once

#ifdef KEYBOARD_crkbd_rev1
#include "rev1.h"
#endif

#endif
2 changes: 1 addition & 1 deletion keyboards/crkbd/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void i2c_delay(void) {
// _delay_us(100);
}

// Setup twi to run at 100kHz
// Setup twi to run at 100kHz or 400kHz (see ./i2c.h SCL_CLOCK)
void i2c_master_init(void) {
// no prescaler
TWSR = 0;
Expand Down
7 changes: 2 additions & 5 deletions keyboards/crkbd/i2c.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef I2C_H
#define I2C_H
#pragma once

#include <stdint.h>

Expand All @@ -15,7 +14,7 @@

#define SLAVE_BUFFER_SIZE 0x10

// i2c SCL clock frequency
// i2c SCL clock frequency 400kHz
#define SCL_CLOCK 400000L

extern volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
Expand Down Expand Up @@ -45,5 +44,3 @@ extern unsigned char i2c_readNak(void);
extern unsigned char i2c_read(unsigned char ack);

#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();

#endif
2 changes: 0 additions & 2 deletions keyboards/crkbd/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "split_util.h"
#endif
#ifdef SSD1306OLED
#include "LUFA/Drivers/Peripheral/TWI.h"
#include "ssd1306.h"
#endif

Expand Down Expand Up @@ -130,7 +129,6 @@ void matrix_init_user(void) {
#endif
//SSD1306 OLED init, make sure to add #define SSD1306OLED in config.h
#ifdef SSD1306OLED
TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 800000));
iota_gfx_init(!has_usb()); // turns on the display
#endif
}
Expand Down
14 changes: 7 additions & 7 deletions keyboards/crkbd/keymaps/default/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ SWAP_HANDS_ENABLE = no # Enable one-hand typing
SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend

# If you want to change the display of OLED, you need to change here
SRC += ../lib/rgb_state_reader.c \
../lib/layer_state_reader.c \
../lib/logo_reader.c \
../lib/keylogger.c \
# ../lib/mode_icon_reader.c \
# ../lib/host_led_state_reader.c \
# ../lib/timelogger.c \
SRC += ./lib/rgb_state_reader.c \
./lib/layer_state_reader.c \
./lib/logo_reader.c \
./lib/keylogger.c \
# ./lib/mode_icon_reader.c \
# ./lib/host_led_state_reader.c \
# ./lib/timelogger.c \
2 changes: 0 additions & 2 deletions keyboards/crkbd/keymaps/like_jis/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "split_util.h"
#endif
#ifdef SSD1306OLED
#include "LUFA/Drivers/Peripheral/TWI.h"
#include "ssd1306.h"
#endif

Expand Down Expand Up @@ -124,7 +123,6 @@ void matrix_init_user(void) {
#endif
//SSD1306 OLED init, make sure to add #define SSD1306OLED in config.h
#ifdef SSD1306OLED
TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 800000));
iota_gfx_init(!has_usb()); // turns on the display
#endif
}
Expand Down
14 changes: 7 additions & 7 deletions keyboards/crkbd/keymaps/like_jis/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ SWAP_HANDS_ENABLE = no # Enable one-hand typing
SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend

# If you want to change the display of OLED, you need to change here
SRC += ../lib/rgb_state_reader.c \
../lib/layer_state_reader.c \
../lib/logo_reader.c \
../lib/keylogger.c \
# ../lib/mode_icon_reader.c \
# ../lib/host_led_state_reader.c \
# ../lib/timelogger.c \
SRC += ./lib/rgb_state_reader.c \
./lib/layer_state_reader.c \
./lib/logo_reader.c \
./lib/keylogger.c \
# ./lib/mode_icon_reader.c \
# ./lib/host_led_state_reader.c \
# ./lib/timelogger.c \
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 1 addition & 5 deletions keyboards/crkbd/pro_micro.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
$Id: wiring.h 249 2007-02-03 16:52:51Z mellis $
*/

#ifndef Pins_Arduino_h
#define Pins_Arduino_h
#pragma once

#include <avr/pgmspace.h>

Expand Down Expand Up @@ -358,5 +356,3 @@ const uint8_t PROGMEM analog_pin_to_channel_PGM[] = {
#define SERIAL_PORT_USBVIRTUAL Serial
#define SERIAL_PORT_HARDWARE Serial1
#define SERIAL_PORT_HARDWARE_OPEN Serial1

#endif /* Pins_Arduino_h */
8 changes: 1 addition & 7 deletions keyboards/crkbd/rev1/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,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 REV1_CONFIG_H
#define REV1_CONFIG_H

#include "../config.h"
#pragma once

/* USB Device descriptor parameter */
#define VENDOR_ID 0xFEED
Expand Down Expand Up @@ -82,6 +79,3 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//#define NO_ACTION_ONESHOT
//#define NO_ACTION_MACRO
//#define NO_ACTION_FUNCTION


#endif
59 changes: 34 additions & 25 deletions keyboards/crkbd/rev1/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
Expand All @@ -30,12 +31,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "matrix.h"
#include "split_util.h"
#include "pro_micro.h"
#include "config.h"

#ifdef USE_MATRIX_I2C
# include "i2c.h"
#else // USE_SERIAL
# include "serial.h"
# include "split_scomm.h"
#endif

#ifndef DEBOUNCE
Expand Down Expand Up @@ -103,6 +103,8 @@ void matrix_init(void)
init_cols();

TX_RX_LED_INIT;
TXLED0;
RXLED0;

// initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
Expand Down Expand Up @@ -179,17 +181,20 @@ int i2c_transaction(void) {

#else // USE_SERIAL

int serial_transaction(void) {
int serial_transaction(int master_changed) {
int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
#ifdef SERIAL_USE_MULTI_TRANSACTION
int ret=serial_update_buffers(master_changed);
#else
int ret=serial_update_buffers();
#endif
if (ret ) {
if(ret==2)RXLED1;
if(ret==2) RXLED1;
return 1;
}
RXLED0;
for (int i = 0; i < ROWS_PER_HAND; ++i) {
matrix[slaveOffset+i] = serial_slave_buffer[i];
}
RXLED0;
memcpy(&matrix[slaveOffset],
(void *)serial_slave_buffer, SERIAL_SLAVE_BUFFER_LENGTH);
return 0;
}
#endif
Expand All @@ -200,19 +205,9 @@ uint8_t matrix_scan(void)
matrix_master_scan();
}else{
matrix_slave_scan();

// if(serial_slave_DATA_CORRUPT()){
// TXLED0;
int offset = (isLeftHand) ? ROWS_PER_HAND : 0;

for (int i = 0; i < ROWS_PER_HAND; ++i) {
matrix[offset+i] = serial_master_buffer[i];
}

// }else{
// TXLED1;
// }

int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
memcpy(&matrix[offset],
(void *)serial_master_buffer, SERIAL_MASTER_BUFFER_LENGTH);
matrix_scan_quantum();
}
return 1;
Expand All @@ -222,6 +217,7 @@ uint8_t matrix_scan(void)
uint8_t matrix_master_scan(void) {

int ret = _matrix_scan();
int mchanged = 1;

int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;

Expand All @@ -231,15 +227,18 @@ uint8_t matrix_master_scan(void) {
// i2c_slave_buffer[i] = matrix[offset+i];
// }
#else // USE_SERIAL
for (int i = 0; i < ROWS_PER_HAND; ++i) {
serial_master_buffer[i] = matrix[offset+i];
}
#ifdef SERIAL_USE_MULTI_TRANSACTION
mchanged = memcmp((void *)serial_master_buffer,
&matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
#endif
memcpy((void *)serial_master_buffer,
&matrix[offset], SERIAL_MASTER_BUFFER_LENGTH);
#endif

#ifdef USE_MATRIX_I2C
if( i2c_transaction() ) {
#else // USE_SERIAL
if( serial_transaction() ) {
if( serial_transaction(mchanged) ) {
#endif
// turn on the indicator led when halves are disconnected
TXLED1;
Expand Down Expand Up @@ -273,9 +272,19 @@ void matrix_slave_scan(void) {
i2c_slave_buffer[i] = matrix[offset+i];
}
#else // USE_SERIAL
#ifdef SERIAL_USE_MULTI_TRANSACTION
int change = 0;
#endif
for (int i = 0; i < ROWS_PER_HAND; ++i) {
#ifdef SERIAL_USE_MULTI_TRANSACTION
if( serial_slave_buffer[i] != matrix[offset+i] )
change = 1;
#endif
serial_slave_buffer[i] = matrix[offset+i];
}
#ifdef SERIAL_USE_MULTI_TRANSACTION
slave_buffer_change_count += change;
#endif
#endif
}

Expand Down
5 changes: 1 addition & 4 deletions keyboards/crkbd/rev1/rev1.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef REV1_H
#define REV1_CONFIG_H
#pragma once

#include "../crkbd.h"

Expand Down Expand Up @@ -49,5 +48,3 @@
KC_##L20, KC_##L21, KC_##L22, KC_##L23, KC_##L24, KC_##L25, KC_##R20, KC_##R21, KC_##R22, KC_##R23, KC_##R24, KC_##R25, \
KC_##L30, KC_##L31, KC_##L32, KC_##R30, KC_##R31, KC_##R32 \
)

#endif
5 changes: 3 additions & 2 deletions keyboards/crkbd/rev1/rules.mk
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
SRC += rev1/matrix.c \
ws2812.c
SRC += rev1/matrix.c
SRC += rev1/split_util.c
SRC += rev1/split_scomm.c
10 changes: 10 additions & 0 deletions keyboards/crkbd/rev1/serial_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

/* Soft Serial defines */
#define SERIAL_PIN_DDR DDRD
#define SERIAL_PIN_PORT PORTD
#define SERIAL_PIN_INPUT PIND
#define SERIAL_PIN_MASK _BV(PD2)
#define SERIAL_PIN_INTERRUPT INT2_vect

#define SERIAL_USE_MULTI_TRANSACTION
5 changes: 5 additions & 0 deletions keyboards/crkbd/rev1/serial_config_simpleapi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#undef SERIAL_USE_MULTI_TRANSACTION
#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
#define SERIAL_MASTER_BUFFER_LENGTH MATRIX_ROWS/2
Loading

0 comments on commit 35efcc9

Please sign in to comment.