aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--keyboards/lily58/config.h21
-rw-r--r--keyboards/lily58/i2c.c162
-rw-r--r--keyboards/lily58/i2c.h49
-rw-r--r--keyboards/lily58/keymaps/default/config.h41
-rw-r--r--keyboards/lily58/keymaps/default/keymap.c155
-rw-r--r--keyboards/lily58/keymaps/default/rules.mk22
-rw-r--r--keyboards/lily58/lily58.c1
-rw-r--r--keyboards/lily58/lily58.h28
-rw-r--r--keyboards/lily58/matrix.c459
-rw-r--r--keyboards/lily58/readme.md17
-rw-r--r--keyboards/lily58/rev1/config.h86
-rw-r--r--keyboards/lily58/rev1/rev1.c24
-rw-r--r--keyboards/lily58/rev1/rev1.h61
-rw-r--r--keyboards/lily58/rev1/rules.mk1
-rw-r--r--keyboards/lily58/rules.mk76
-rw-r--r--keyboards/lily58/serial.c445
-rw-r--r--keyboards/lily58/serial.h80
-rw-r--r--keyboards/lily58/serial_config.h8
-rw-r--r--keyboards/lily58/split_util.c86
-rw-r--r--keyboards/lily58/split_util.h20
-rw-r--r--keyboards/lily58/ssd1306.c330
-rw-r--r--keyboards/lily58/ssd1306.h94
22 files changed, 2266 insertions, 0 deletions
diff --git a/keyboards/lily58/config.h b/keyboards/lily58/config.h
new file mode 100644
index 000000000..db4844c91
--- /dev/null
+++ b/keyboards/lily58/config.h
@@ -0,0 +1,21 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3Copyright 2015 Jack Humbert
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#pragma once
20
21#include "serial_config.h" \ No newline at end of file
diff --git a/keyboards/lily58/i2c.c b/keyboards/lily58/i2c.c
new file mode 100644
index 000000000..084c890c4
--- /dev/null
+++ b/keyboards/lily58/i2c.c
@@ -0,0 +1,162 @@
1#include <util/twi.h>
2#include <avr/io.h>
3#include <stdlib.h>
4#include <avr/interrupt.h>
5#include <util/twi.h>
6#include <stdbool.h>
7#include "i2c.h"
8
9#ifdef USE_I2C
10
11// Limits the amount of we wait for any one i2c transaction.
12// Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is
13// 9 bits, a single transaction will take around 90μs to complete.
14//
15// (F_CPU/SCL_CLOCK) => # of μC cycles to transfer a bit
16// poll loop takes at least 8 clock cycles to execute
17#define I2C_LOOP_TIMEOUT (9+1)*(F_CPU/SCL_CLOCK)/8
18
19#define BUFFER_POS_INC() (slave_buffer_pos = (slave_buffer_pos+1)%SLAVE_BUFFER_SIZE)
20
21volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
22
23static volatile uint8_t slave_buffer_pos;
24static volatile bool slave_has_register_set = false;
25
26// Wait for an i2c operation to finish
27inline static
28void i2c_delay(void) {
29 uint16_t lim = 0;
30 while(!(TWCR & (1<<TWINT)) && lim < I2C_LOOP_TIMEOUT)
31 lim++;
32
33 // easier way, but will wait slightly longer
34 // _delay_us(100);
35}
36
37// Setup twi to run at 100kHz
38void i2c_master_init(void) {
39 // no prescaler
40 TWSR = 0;
41 // Set TWI clock frequency to SCL_CLOCK. Need TWBR>10.
42 // Check datasheets for more info.
43 TWBR = ((F_CPU/SCL_CLOCK)-16)/2;
44}
45
46// Start a transaction with the given i2c slave address. The direction of the
47// transfer is set with I2C_READ and I2C_WRITE.
48// returns: 0 => success
49// 1 => error
50uint8_t i2c_master_start(uint8_t address) {
51 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTA);
52
53 i2c_delay();
54
55 // check that we started successfully
56 if ( (TW_STATUS != TW_START) && (TW_STATUS != TW_REP_START))
57 return 1;
58
59 TWDR = address;
60 TWCR = (1<<TWINT) | (1<<TWEN);
61
62 i2c_delay();
63
64 if ( (TW_STATUS != TW_MT_SLA_ACK) && (TW_STATUS != TW_MR_SLA_ACK) )
65 return 1; // slave did not acknowledge
66 else
67 return 0; // success
68}
69
70
71// Finish the i2c transaction.
72void i2c_master_stop(void) {
73 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
74
75 uint16_t lim = 0;
76 while(!(TWCR & (1<<TWSTO)) && lim < I2C_LOOP_TIMEOUT)
77 lim++;
78}
79
80// Write one byte to the i2c slave.
81// returns 0 => slave ACK
82// 1 => slave NACK
83uint8_t i2c_master_write(uint8_t data) {
84 TWDR = data;
85 TWCR = (1<<TWINT) | (1<<TWEN);
86
87 i2c_delay();
88
89 // check if the slave acknowledged us
90 return (TW_STATUS == TW_MT_DATA_ACK) ? 0 : 1;
91}
92
93// Read one byte from the i2c slave. If ack=1 the slave is acknowledged,
94// if ack=0 the acknowledge bit is not set.
95// returns: byte read from i2c device
96uint8_t i2c_master_read(int ack) {
97 TWCR = (1<<TWINT) | (1<<TWEN) | (ack<<TWEA);
98
99 i2c_delay();
100 return TWDR;
101}
102
103void i2c_reset_state(void) {
104 TWCR = 0;
105}
106
107void i2c_slave_init(uint8_t address) {
108 TWAR = address << 0; // slave i2c address
109 // TWEN - twi enable
110 // TWEA - enable address acknowledgement
111 // TWINT - twi interrupt flag
112 // TWIE - enable the twi interrupt
113 TWCR = (1<<TWIE) | (1<<TWEA) | (1<<TWINT) | (1<<TWEN);
114}
115
116ISR(TWI_vect);
117
118ISR(TWI_vect) {
119 uint8_t ack = 1;
120 switch(TW_STATUS) {
121 case TW_SR_SLA_ACK:
122 // this device has been addressed as a slave receiver
123 slave_has_register_set = false;
124 break;
125
126 case TW_SR_DATA_ACK:
127 // this device has received data as a slave receiver
128 // The first byte that we receive in this transaction sets the location
129 // of the read/write location of the slaves memory that it exposes over
130 // i2c. After that, bytes will be written at slave_buffer_pos, incrementing
131 // slave_buffer_pos after each write.
132 if(!slave_has_register_set) {
133 slave_buffer_pos = TWDR;
134 // don't acknowledge the master if this memory loctaion is out of bounds
135 if ( slave_buffer_pos >= SLAVE_BUFFER_SIZE ) {
136 ack = 0;
137 slave_buffer_pos = 0;
138 }
139 slave_has_register_set = true;
140 } else {
141 i2c_slave_buffer[slave_buffer_pos] = TWDR;
142 BUFFER_POS_INC();
143 }
144 break;
145
146 case TW_ST_SLA_ACK:
147 case TW_ST_DATA_ACK:
148 // master has addressed this device as a slave transmitter and is
149 // requesting data.
150 TWDR = i2c_slave_buffer[slave_buffer_pos];
151 BUFFER_POS_INC();
152 break;
153
154 case TW_BUS_ERROR: // something went wrong, reset twi state
155 TWCR = 0;
156 default:
157 break;
158 }
159 // Reset everything, so we are ready for the next TWI interrupt
160 TWCR |= (1<<TWIE) | (1<<TWINT) | (ack<<TWEA) | (1<<TWEN);
161}
162#endif
diff --git a/keyboards/lily58/i2c.h b/keyboards/lily58/i2c.h
new file mode 100644
index 000000000..c15b6bc50
--- /dev/null
+++ b/keyboards/lily58/i2c.h
@@ -0,0 +1,49 @@
1#ifndef I2C_H
2#define I2C_H
3
4#include <stdint.h>
5
6#ifndef F_CPU
7#define F_CPU 16000000UL
8#endif
9
10#define I2C_READ 1
11#define I2C_WRITE 0
12
13#define I2C_ACK 1
14#define I2C_NACK 0
15
16#define SLAVE_BUFFER_SIZE 0x10
17
18// i2c SCL clock frequency
19#define SCL_CLOCK 400000L
20
21extern volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
22
23void i2c_master_init(void);
24uint8_t i2c_master_start(uint8_t address);
25void i2c_master_stop(void);
26uint8_t i2c_master_write(uint8_t data);
27uint8_t i2c_master_read(int);
28void i2c_reset_state(void);
29void i2c_slave_init(uint8_t address);
30
31
32static inline unsigned char i2c_start_read(unsigned char addr) {
33 return i2c_master_start((addr << 1) | I2C_READ);
34}
35
36static inline unsigned char i2c_start_write(unsigned char addr) {
37 return i2c_master_start((addr << 1) | I2C_WRITE);
38}
39
40// from SSD1306 scrips
41extern unsigned char i2c_rep_start(unsigned char addr);
42extern void i2c_start_wait(unsigned char addr);
43extern unsigned char i2c_readAck(void);
44extern unsigned char i2c_readNak(void);
45extern unsigned char i2c_read(unsigned char ack);
46
47#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
48
49#endif
diff --git a/keyboards/lily58/keymaps/default/config.h b/keyboards/lily58/keymaps/default/config.h
new file mode 100644
index 000000000..3077c9463
--- /dev/null
+++ b/keyboards/lily58/keymaps/default/config.h
@@ -0,0 +1,41 @@
1/*
2This is the c configuration file for the keymap
3
4Copyright 2012 Jun Wako <wakojun@gmail.com>
5Copyright 2015 Jack Humbert
6
7This program is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "config.h"
23
24/* Use I2C or Serial, not both */
25
26#define USE_SERIAL
27// #define USE_I2C
28
29/* Select hand configuration */
30
31#define MASTER_LEFT
32// #define MASTER_RIGHT
33// #define EE_HANDS
34
35// Underglow
36/*
37#undef RGBLED_NUM
38#define RGBLED_NUM 14 // Number of LEDs
39#define RGBLIGHT_ANIMATIONS
40#define RGBLIGHT_SLEEP
41*/ \ No newline at end of file
diff --git a/keyboards/lily58/keymaps/default/keymap.c b/keyboards/lily58/keymaps/default/keymap.c
new file mode 100644
index 000000000..624ce210b
--- /dev/null
+++ b/keyboards/lily58/keymaps/default/keymap.c
@@ -0,0 +1,155 @@
1#include QMK_KEYBOARD_H
2
3extern keymap_config_t keymap_config;
4
5#define _QWERTY 0
6#define _LOWER 1
7#define _RAISE 2
8#define _ADJUST 16
9
10enum custom_keycodes {
11 QWERTY = SAFE_RANGE,
12 LOWER,
13 RAISE,
14 ADJUST,
15};
16
17
18const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
19
20/* QWERTY
21 * ,-----------------------------------------. ,-----------------------------------------.
22 * | ESC | 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 0 | ~ |
23 * |------+------+------+------+------+------| |------+------+------+------+------+------|
24 * | Tab | Q | W | E | R | T | | Y | U | I | O | P | - |
25 * |------+------+------+------+------+------| |------+------+------+------+------+------|
26 * |LCTRL | A | S | D | F | G |-------. ,-------| H | J | K | L | ; | ' |
27 * |------+------+------+------+------+------| [ | | ] |------+------+------+------+------+------|
28 * |LShift| Z | X | C | V | B |-------| |-------| N | M | , | . | / |RShift|
29 * `-----------------------------------------/ / \ \-----------------------------------------'
30 * |LOWER | LGUI | Alt | /Space / \Enter \ |BackSP| RGUI |RAISE |
31 * | | | |/ / \ \ | | | |
32 * `-------------------''-------' '------''--------------------'
33 */
34
35 [_QWERTY] = LAYOUT( \
36 KC_ESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_GRV, \
37 KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_MINS, \
38 KC_LCTRL, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, \
39 KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_LBRC, KC_RBRC, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, \
40 LOWER,KC_LGUI, KC_LALT, KC_SPC, KC_ENT, KC_BSPC, KC_RGUI, RAISE \
41),
42/* LOWER
43 * ,-----------------------------------------. ,-----------------------------------------.
44 * | F1 | F2 | F3 | F4 | F5 | F6 | | F7 | F8 | F9 | F10 | F11 | F12 |
45 * |------+------+------+------+------+------| |------+------+------+------+------+------|
46 * | ~ | ! | @ | # | $ | % | | ^ | & | * | ( | ) | |
47 * |------+------+------+------+------+------| |------+------+------+------+------+------|
48 * | | | | | | |-------. ,-------| | _ | + | | | |
49 * |------+------+------+------+------+------| [ | | ] |------+------+------+------+------+------|
50 * | | | | | | |-------| |-------| |ISO ~ |ISO | | | | |
51 * `-----------------------------------------/ / \ \-----------------------------------------'
52 * |LOWER | LGUI | Alt | /Space / \Enter \ |BackSP| RGUI |RAISE |
53 * | | | |/ / \ \ | | | |
54 * `-------------------''-------' '------''--------------------'
55 */
56[_LOWER] = LAYOUT( \
57 KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, \
58 KC_TILD, KC_EXLM, KC_AT, KC_HASH, KC_DLR, KC_PERC, KC_CIRC, KC_AMPR, KC_ASTR, KC_LPRN, KC_RPRN, _______, \
59 _______, _______, _______, _______, _______, _______, _______, KC_UNDS, KC_PLUS, KC_LCBR, KC_RCBR, KC_PIPE, \
60 _______, _______, _______, _______, _______, _______, _______, _______, _______,S(KC_NUHS),S(KC_NUBS),_______, _______, _______,\
61 _______, _______, _______, _______, _______, _______, _______, _______\
62),
63/* RAISE
64 * ,-----------------------------------------. ,-----------------------------------------.
65 * | F1 | F2 | F3 | F4 | F5 | F6 | | F7 | F8 | F9 | F10 | F11 | F12 |
66 * |------+------+------+------+------+------| |------+------+------+------+------+------|
67 * | ` | 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 0 | |
68 * |------+------+------+------+------+------| |------+------+------+------+------+------|
69 * | | | | | | |-------. ,-------| | Left | Down | Up |Right | |
70 * |------+------+------+------+------+------| [ | | ] |------+------+------+------+------+------|
71 * | | | | | | |-------| |-------| + | - | = | [ | ] | \ |
72 * `-----------------------------------------/ / \ \-----------------------------------------'
73 * |LOWER | LGUI | Alt | /Space / \Enter \ |BackSP| RGUI |RAISE |
74 * | | | |/ / \ \ | | | |
75 * `-------------------''-------' '------''--------------------'
76 */
77
78[_RAISE] = LAYOUT( \
79 KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, \
80 KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, _______, \
81 _______, _______, _______, _______, _______, _______, XXXXXXX, KC_LEFT, KC_DOWN, KC_UP, KC_RGHT, XXXXXXX, \
82 _______, _______, _______, _______, _______, _______, _______, _______, KC_PLUS, KC_MINS, KC_EQL, KC_LBRC, KC_RBRC, KC_BSLS, \
83 _______, _______, _______, _______, _______, _______, _______, _______ \
84),
85
86/* ADJUST (Layers for Underglow)
87 * ,-----------------------------------------. ,-----------------------------------------.
88 * | | | | | | | | | | | | | |
89 * |------+------+------+------+------+------| |------+------+------+------+------+------|
90 * | | | | | | | | | | | | | |
91 * |------+------+------+------+------+------| |------+------+------+------+------+------|
92 * | | | | | | |-------. ,-------| | | | | | |
93 * |------+------+------+------+------+------| | | |------+------+------+------+------+------|
94 * | | | | | | |-------| |-------| | | | | | |
95 * `-----------------------------------------/ / \ \-----------------------------------------'
96 * |LOWER | LGUI | Alt | /Space / \Enter \ |BackSP| RGUI |RAISE |
97 * | | | |/ / \ \ | | | |
98 * `-------------------''-------' '------''--------------------'
99 */
100 [_ADJUST] = LAYOUT( \
101 XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, \
102 XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, \
103 XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, \
104 XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,\
105 _______, _______, _______, _______, _______, _______, _______, _______ \
106 )
107};
108
109
110
111void persistent_default_layer_set(uint16_t default_layer) {
112 eeconfig_update_default_layer(default_layer);
113 default_layer_set(default_layer);
114}
115
116bool process_record_user(uint16_t keycode, keyrecord_t *record) {
117 switch (keycode) {
118 case QWERTY:
119 if (record->event.pressed) {
120 print("mode just switched to qwerty and this is a huge string\n");
121 set_single_persistent_default_layer(_QWERTY);
122 }
123 return false;
124 break;
125 case LOWER:
126 if (record->event.pressed) {
127 layer_on(_LOWER);
128 update_tri_layer(_LOWER, _RAISE, _ADJUST);
129 } else {
130 layer_off(_LOWER);
131 update_tri_layer(_LOWER, _RAISE, _ADJUST);
132 }
133 return false;
134 break;
135 case RAISE:
136 if (record->event.pressed) {
137 layer_on(_RAISE);
138 update_tri_layer(_LOWER, _RAISE, _ADJUST);
139 } else {
140 layer_off(_RAISE);
141 update_tri_layer(_LOWER, _RAISE, _ADJUST);
142 }
143 return false;
144 break;
145 case ADJUST:
146 if (record->event.pressed) {
147 layer_on(_ADJUST);
148 } else {
149 layer_off(_ADJUST);
150 }
151 return false;
152 break;
153 }
154 return true;
155} \ No newline at end of file
diff --git a/keyboards/lily58/keymaps/default/rules.mk b/keyboards/lily58/keymaps/default/rules.mk
new file mode 100644
index 000000000..73777a1b7
--- /dev/null
+++ b/keyboards/lily58/keymaps/default/rules.mk
@@ -0,0 +1,22 @@
1
2# Build Options
3# change to "no" to disable the options, or define them in the Makefile in
4# the appropriate keymap folder that will get included automatically
5#
6
7OLED_ENABLE = no
8RGBLIGHT_ENABLE = no
9
10BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration(+1000)
11MOUSEKEY_ENABLE = no # Mouse keys(+4700)
12EXTRAKEY_ENABLE = no # Audio control and System control(+450)
13CONSOLE_ENABLE = no # Console for debug(+400)
14COMMAND_ENABLE = no # Commands for debug and configuration
15NKRO_ENABLE = no # Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
16MIDI_ENABLE = no # MIDI controls
17AUDIO_ENABLE = no # Audio output on port C6
18UNICODE_ENABLE = no # Unicode
19BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
20ONEHAND_ENABLE = no # Enable one-hand typing
21# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
22SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend \ No newline at end of file
diff --git a/keyboards/lily58/lily58.c b/keyboards/lily58/lily58.c
new file mode 100644
index 000000000..697e3820c
--- /dev/null
+++ b/keyboards/lily58/lily58.c
@@ -0,0 +1 @@
#include "lily58.h"
diff --git a/keyboards/lily58/lily58.h b/keyboards/lily58/lily58.h
new file mode 100644
index 000000000..1d64dd7b2
--- /dev/null
+++ b/keyboards/lily58/lily58.h
@@ -0,0 +1,28 @@
1#ifndef LILY58_H
2#define LILY58_H
3
4#include "quantum.h"
5
6#ifdef KEYBOARD_lily58_rev1
7 #include "rev1.h"
8#endif
9
10
11
12// Used to create a keymap using only KC_ prefixed keys
13#define LAYOUT_kc( \
14 L00, L01, L02, L03, L04, L05, R00, R01, R02, R03, R04, R05, \
15 L10, L11, L12, L13, L14, L15, R10, R11, R12, R13, R14, R15, \
16 L20, L21, L22, L23, L24, L25, R20, R21, R22, R23, R24, R25, \
17 L30, L31, L32, L33, L34, L35, L45, R40, R30, R31, R32, R33, R34, R35, \
18 L41, L42, L43, L44, R41, R42, R43, R44 \
19 ) \
20 LAYOUT( \
21 KC_##L00, KC_##L01, KC_##L02, KC_##L03, KC_##L04, KC_##L05, KC_##R00, KC_##R01, KC_##R02, KC_##R03, KC_##R04, KC_##R05, \
22 KC_##L10, KC_##L11, KC_##L12, KC_##L13, KC_##L14, KC_##L15, KC_##R10, KC_##R11, KC_##R12, KC_##R13, KC_##R14, KC_##R15, \
23 KC_##L20, KC_##L21, KC_##L22, KC_##L23, KC_##L24, KC_##L25, KC_##R20, KC_##R21, KC_##R22, KC_##R23, KC_##R24, KC_##R25, \
24 KC_##L30, KC_##L31, KC_##L32, KC_##L33, KC_##L34, KC_##L35, KC_##L45, KC_##R40, KC_##R30, KC_##R31, KC_##R32, KC_##R33, KC_##R34, KC_##R35, \
25 KC_##L41, KC_##L42, KC_##L43, KC_##L44, KC_##R41, KC_##R42, KC_##R43, KC_##R44 \
26 )
27
28#endif
diff --git a/keyboards/lily58/matrix.c b/keyboards/lily58/matrix.c
new file mode 100644
index 000000000..fc42dd14d
--- /dev/null
+++ b/keyboards/lily58/matrix.c
@@ -0,0 +1,459 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/*
19 * scan matrix
20 */
21#include <stdint.h>
22#include <stdbool.h>
23#include <avr/io.h>
24#include "wait.h"
25#include "print.h"
26#include "debug.h"
27#include "util.h"
28#include "matrix.h"
29#include "split_util.h"
30#include "pro_micro.h"
31#include "config.h"
32#include "timer.h"
33
34#ifdef USE_I2C
35# include "i2c.h"
36#else // USE_SERIAL
37# include "serial.h"
38#endif
39
40#ifndef DEBOUNCING_DELAY
41# define DEBOUNCING_DELAY 5
42#endif
43
44#if (DEBOUNCING_DELAY > 0)
45 static uint16_t debouncing_time;
46 static bool debouncing = false;
47#endif
48
49#if (MATRIX_COLS <= 8)
50# define print_matrix_header() print("\nr/c 01234567\n")
51# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
52# define matrix_bitpop(i) bitpop(matrix[i])
53# define ROW_SHIFTER ((uint8_t)1)
54#else
55# error "Currently only supports 8 COLS"
56#endif
57static matrix_row_t matrix_debouncing[MATRIX_ROWS];
58
59#define ERROR_DISCONNECT_COUNT 5
60
61#define ROWS_PER_HAND (MATRIX_ROWS/2)
62
63static uint8_t error_count = 0;
64
65static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
66static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
67
68/* matrix state(1:on, 0:off) */
69static matrix_row_t matrix[MATRIX_ROWS];
70static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71
72#if (DIODE_DIRECTION == COL2ROW)
73 static void init_cols(void);
74 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
75 static void unselect_rows(void);
76 static void select_row(uint8_t row);
77 static void unselect_row(uint8_t row);
78#elif (DIODE_DIRECTION == ROW2COL)
79 static void init_rows(void);
80 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
81 static void unselect_cols(void);
82 static void unselect_col(uint8_t col);
83 static void select_col(uint8_t col);
84#endif
85
86__attribute__ ((weak))
87void matrix_init_kb(void) {
88 matrix_init_user();
89}
90
91__attribute__ ((weak))
92void matrix_scan_kb(void) {
93 matrix_scan_user();
94}
95
96__attribute__ ((weak))
97void matrix_init_user(void) {
98}
99
100__attribute__ ((weak))
101void matrix_scan_user(void) {
102}
103
104inline
105uint8_t matrix_rows(void)
106{
107 return MATRIX_ROWS;
108}
109
110inline
111uint8_t matrix_cols(void)
112{
113 return MATRIX_COLS;
114}
115
116void matrix_init(void)
117{
118 debug_enable = true;
119 debug_matrix = true;
120 debug_mouse = true;
121 // initialize row and col
122#if (DIODE_DIRECTION == COL2ROW)
123 unselect_rows();
124 init_cols();
125#elif (DIODE_DIRECTION == ROW2COL)
126 unselect_cols();
127 init_rows();
128#endif
129
130 TX_RX_LED_INIT;
131
132 // initialize matrix state: all keys off
133 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
134 matrix[i] = 0;
135 matrix_debouncing[i] = 0;
136 }
137
138 matrix_init_quantum();
139
140}
141
142uint8_t _matrix_scan(void)
143{
144 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
145#if (DIODE_DIRECTION == COL2ROW)
146 // Set row, read cols
147 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
148# if (DEBOUNCING_DELAY > 0)
149 bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
150
151 if (matrix_changed) {
152 debouncing = true;
153 debouncing_time = timer_read();
154 }
155
156# else
157 read_cols_on_row(matrix+offset, current_row);
158# endif
159
160 }
161
162#elif (DIODE_DIRECTION == ROW2COL)
163 // Set col, read rows
164 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
165# if (DEBOUNCING_DELAY > 0)
166 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
167 if (matrix_changed) {
168 debouncing = true;
169 debouncing_time = timer_read();
170 }
171# else
172 read_rows_on_col(matrix+offset, current_col);
173# endif
174
175 }
176#endif
177
178# if (DEBOUNCING_DELAY > 0)
179 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
180 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
181 matrix[i+offset] = matrix_debouncing[i+offset];
182 }
183 debouncing = false;
184 }
185# endif
186
187 return 1;
188}
189
190#ifdef USE_I2C
191
192// Get rows from other half over i2c
193int i2c_transaction(void) {
194 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
195
196 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
197 if (err) goto i2c_error;
198
199 // start of matrix stored at 0x00
200 err = i2c_master_write(0x00);
201 if (err) goto i2c_error;
202
203 // Start read
204 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
205 if (err) goto i2c_error;
206
207 if (!err) {
208 int i;
209 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
210 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
211 }
212 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
213 i2c_master_stop();
214 } else {
215i2c_error: // the cable is disconnceted, or something else went wrong
216 i2c_reset_state();
217 return err;
218 }
219
220 return 0;
221}
222
223#else // USE_SERIAL
224
225int serial_transaction(void) {
226 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
227
228 if (serial_update_buffers()) {
229 return 1;
230 }
231
232 for (int i = 0; i < ROWS_PER_HAND; ++i) {
233 matrix[slaveOffset+i] = serial_slave_buffer[i];
234 }
235 return 0;
236}
237#endif
238
239uint8_t matrix_scan(void)
240{
241 uint8_t ret = _matrix_scan();
242
243#ifdef USE_I2C
244 if( i2c_transaction() ) {
245#else // USE_SERIAL
246 if( serial_transaction() ) {
247#endif
248 // turn on the indicator led when halves are disconnected
249 TXLED1;
250
251 error_count++;
252
253 if (error_count > ERROR_DISCONNECT_COUNT) {
254 // reset other half if disconnected
255 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
256 for (int i = 0; i < ROWS_PER_HAND; ++i) {
257 matrix[slaveOffset+i] = 0;
258 }
259 }
260 } else {
261 // turn off the indicator led on no error
262 TXLED0;
263 error_count = 0;
264 }
265 matrix_scan_quantum();
266 return ret;
267}
268
269void matrix_slave_scan(void) {
270 _matrix_scan();
271
272 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
273
274#ifdef USE_I2C
275 for (int i = 0; i < ROWS_PER_HAND; ++i) {
276 i2c_slave_buffer[i] = matrix[offset+i];
277 }
278#else // USE_SERIAL
279 for (int i = 0; i < ROWS_PER_HAND; ++i) {
280 serial_slave_buffer[i] = matrix[offset+i];
281 }
282#endif
283}
284
285bool matrix_is_modified(void)
286{
287 if (debouncing) return false;
288 return true;
289}
290
291inline
292bool matrix_is_on(uint8_t row, uint8_t col)
293{
294 return (matrix[row] & ((matrix_row_t)1<<col));
295}
296
297inline
298matrix_row_t matrix_get_row(uint8_t row)
299{
300 return matrix[row];
301}
302
303void matrix_print(void)
304{
305 print("\nr/c 0123456789ABCDEF\n");
306 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
307 phex(row); print(": ");
308 pbin_reverse16(matrix_get_row(row));
309 print("\n");
310 }
311}
312
313uint8_t matrix_key_count(void)
314{
315 uint8_t count = 0;
316 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
317 count += bitpop16(matrix[i]);
318 }
319 return count;
320}
321
322#if (DIODE_DIRECTION == COL2ROW)
323
324static void init_cols(void)
325{
326 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
327 uint8_t pin = col_pins[x];
328 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
329 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
330 }
331}
332
333static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
334{
335 // Store last value of row prior to reading
336 matrix_row_t last_row_value = current_matrix[current_row];
337
338 // Clear data in matrix row
339 current_matrix[current_row] = 0;
340
341 // Select row and wait for row selecton to stabilize
342 select_row(current_row);
343 wait_us(30);
344
345 // For each col...
346 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
347
348 // Select the col pin to read (active low)
349 uint8_t pin = col_pins[col_index];
350 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
351
352 // Populate the matrix row with the state of the col pin
353 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
354 }
355
356 // Unselect row
357 unselect_row(current_row);
358
359 return (last_row_value != current_matrix[current_row]);
360}
361
362static void select_row(uint8_t row)
363{
364 uint8_t pin = row_pins[row];
365 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
366 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
367}
368
369static void unselect_row(uint8_t row)
370{
371 uint8_t pin = row_pins[row];
372 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
373 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
374}
375
376static void unselect_rows(void)
377{
378 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
379 uint8_t pin = row_pins[x];
380 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
381 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
382 }
383}
384
385#elif (DIODE_DIRECTION == ROW2COL)
386
387static void init_rows(void)
388{
389 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
390 uint8_t pin = row_pins[x];
391 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
392 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
393 }
394}
395
396static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
397{
398 bool matrix_changed = false;
399
400 // Select col and wait for col selecton to stabilize
401 select_col(current_col);
402 wait_us(30);
403
404 // For each row...
405 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
406 {
407
408 // Store last value of row prior to reading
409 matrix_row_t last_row_value = current_matrix[row_index];
410
411 // Check row pin state
412 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
413 {
414 // Pin LO, set col bit
415 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
416 }
417 else
418 {
419 // Pin HI, clear col bit
420 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
421 }
422
423 // Determine if the matrix changed state
424 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
425 {
426 matrix_changed = true;
427 }
428 }
429
430 // Unselect col
431 unselect_col(current_col);
432
433 return matrix_changed;
434}
435
436static void select_col(uint8_t col)
437{
438 uint8_t pin = col_pins[col];
439 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
440 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
441}
442
443static void unselect_col(uint8_t col)
444{
445 uint8_t pin = col_pins[col];
446 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
447 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
448}
449
450static void unselect_cols(void)
451{
452 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
453 uint8_t pin = col_pins[x];
454 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
455 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
456 }
457}
458
459#endif
diff --git a/keyboards/lily58/readme.md b/keyboards/lily58/readme.md
new file mode 100644
index 000000000..c71365e46
--- /dev/null
+++ b/keyboards/lily58/readme.md
@@ -0,0 +1,17 @@
1# Lily58
2
3Lily58 is 6×4+5keys column-staggered split keyboard.
4
5![Lily58_01](https://user-images.githubusercontent.com/6285554/45210815-92744a00-b2cb-11e8-977a-8c1a93584f17.jpg)
6
7![Lily58_02](https://user-images.githubusercontent.com/6285554/45337733-7b33a600-b5c4-11e8-85b0-35f1cc9bf946.png)
8
9Keyboard Maintainer: [Naoki Katahira](https://github.com/kata0510/) [Twitter:@F_YUUCHI](https://twitter.com/F_YUUCHI)
10Hardware Supported: Lily58 PCB, ProMicro
11Hardware Availability: [PCB & Case Data](https://github.com/kata0510/Lily58)
12
13Make example for this keyboard (after setting up your build environment):
14
15 make lily58:default
16
17See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs). \ No newline at end of file
diff --git a/keyboards/lily58/rev1/config.h b/keyboards/lily58/rev1/config.h
new file mode 100644
index 000000000..9413e8d4a
--- /dev/null
+++ b/keyboards/lily58/rev1/config.h
@@ -0,0 +1,86 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3Copyright 2015 Jack Humbert
4Copyright 2017 F_YUUCHI
5
6This program is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22//#include QMK_KEYBOARD_CONFIG_H
23#include "config_common.h"
24
25/* USB Device descriptor parameter */
26#define VENDOR_ID 0xFC51
27#define PRODUCT_ID 0x0058
28#define DEVICE_VER 0x0100
29#define MANUFACTURER F_YUUCHI
30#define PRODUCT Lily58
31#define DESCRIPTION Lily58 is 6×4+5keys column-staggered split keyboard.
32
33/* key matrix size */
34// Rows are doubled-up
35#define MATRIX_ROWS 10
36#define MATRIX_COLS 6
37
38// wiring of each half
39#define MATRIX_ROW_PINS { C6, D7, E6, B4, B5 }
40#define MATRIX_COL_PINS { F6, F7, B1, B3, B2, B6 }
41
42#define CATERINA_BOOTLOADER
43
44/* define tapping term */
45#define TAPPING_TERM 100
46
47/* define if matrix has ghost */
48//#define MATRIX_HAS_GHOST
49
50/* Set 0 if debouncing isn't needed */
51#define DEBOUNCING_DELAY 5
52
53/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
54#define LOCKING_SUPPORT_ENABLE
55/* Locking resynchronize hack */
56#define LOCKING_RESYNC_ENABLE
57
58/* key combination for command */
59#define IS_COMMAND() ( \
60 keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) \
61)
62
63/* ws2812 RGB LED */
64#define RGB_DI_PIN D3
65#define RGBLIGHT_TIMER
66#define RGBLED_NUM 14 // Number of LEDs
67#define ws2812_PORTREG PORTD
68#define ws2812_DDRREG DDRD
69
70/*
71 * Feature disable options
72 * These options are also useful to firmware size reduction.
73 */
74
75/* disable debug print */
76// #define NO_DEBUG
77
78/* disable print */
79// #define NO_PRINT
80
81/* disable action features */
82//#define NO_ACTION_LAYER
83//#define NO_ACTION_TAPPING
84//#define NO_ACTION_ONESHOT
85//#define NO_ACTION_MACRO
86//#define NO_ACTION_FUNCTION \ No newline at end of file
diff --git a/keyboards/lily58/rev1/rev1.c b/keyboards/lily58/rev1/rev1.c
new file mode 100644
index 000000000..64dd084b2
--- /dev/null
+++ b/keyboards/lily58/rev1/rev1.c
@@ -0,0 +1,24 @@
1#include "lily58.h"
2
3/*
4#ifdef SSD1306OLED
5void led_set_kb(uint8_t usb_led) {
6 // put your keyboard LED indicator (ex: Caps Lock LED) toggling code here
7 led_set_user(usb_led);
8}
9#endif
10*/
11
12void matrix_init_kb(void) {
13
14 // // green led on
15 // DDRD |= (1<<5);
16 // PORTD &= ~(1<<5);
17
18 // // orange led on
19 // DDRB |= (1<<0);
20 // PORTB &= ~(1<<0);
21
22 matrix_init_user();
23};
24
diff --git a/keyboards/lily58/rev1/rev1.h b/keyboards/lily58/rev1/rev1.h
new file mode 100644
index 000000000..a83046358
--- /dev/null
+++ b/keyboards/lily58/rev1/rev1.h
@@ -0,0 +1,61 @@
1#pragma once
2
3#include "lily58.h"
4
5//void promicro_bootloader_jmp(bool program);
6#include "quantum.h"
7
8
9#ifdef USE_I2C
10#include <stddef.h>
11#ifdef __AVR__
12 #include <avr/io.h>
13 #include <avr/interrupt.h>
14#endif
15#endif
16
17
18//void promicro_bootloader_jmp(bool program);
19#ifndef FLIP_HALF
20#define LAYOUT( \
21 L00, L01, L02, L03, L04, L05, R00, R01, R02, R03, R04, R05, \
22 L10, L11, L12, L13, L14, L15, R10, R11, R12, R13, R14, R15, \
23 L20, L21, L22, L23, L24, L25, R20, R21, R22, R23, R24, R25, \
24 L30, L31, L32, L33, L34, L35, L45, R40, R30, R31, R32, R33, R34, R35, \
25 L41, L42, L43, L44, R41, R42, R43, R44 \
26 ) \
27 { \
28 { L00, L01, L02, L03, L04, L05 }, \
29 { L10, L11, L12, L13, L14, L15 }, \
30 { L20, L21, L22, L23, L24, L25 }, \
31 { L30, L31, L32, L33, L34, L35 }, \
32 { KC_NO, L41, L42, L43, L44, L45 }, \
33 { R05, R04, R03, R02, R01, R00 }, \
34 { R15, R14, R13, R12, R11, R10 }, \
35 { R25, R24, R23, R22, R21, R20 }, \
36 { R35, R34, R33, R32, R31, R30 }, \
37 { KC_NO, R44, R43, R42, R41, R40 } \
38 }
39#else
40// Keymap with right side flipped
41// (TRRS jack on both halves are to the right)
42#define LAYOUT( \
43 L00, L01, L02, L03, L04, L05, R00, R01, R02, R03, R04, R05, \
44 L10, L11, L12, L13, L14, L15, R10, R11, R12, R13, R14, R15, \
45 L20, L21, L22, L23, L24, L25, R20, R21, R22, R23, R24, R25, \
46 L30, L31, L32, L33, L34, L35, L45, R30, R31, R32, R33, R34, R35, R45, \
47 L41, L42, L43, L44, R41, R42, R43, R44 \
48 ) \
49 { \
50 { L00, L01, L02, L03, L04, L05 }, \
51 { L10, L11, L12, L13, L14, L15 }, \
52 { L20, L21, L22, L23, L24, L25 }, \
53 { L30, L31, L32, L33, L34, L35 }, \
54 { KC_NO, L41, L42, L43, L44, L45 }, \
55 { R00, R01, R02, R03, R04, R05 }, \
56 { R10, R11, R12, R13, R14, R15 }, \
57 { R20, R21, R22, R23, R24, R25 }, \
58 { R30, R31, R32, R33, R34, R35 }, \
59 { KC_NO, R41, R42, R43, R44, R45 } \
60 }
61#endif \ No newline at end of file
diff --git a/keyboards/lily58/rev1/rules.mk b/keyboards/lily58/rev1/rules.mk
new file mode 100644
index 000000000..f84561674
--- /dev/null
+++ b/keyboards/lily58/rev1/rules.mk
@@ -0,0 +1 @@
BACKLIGHT_ENABLE = no \ No newline at end of file
diff --git a/keyboards/lily58/rules.mk b/keyboards/lily58/rules.mk
new file mode 100644
index 000000000..23527476a
--- /dev/null
+++ b/keyboards/lily58/rules.mk
@@ -0,0 +1,76 @@
1SRC += matrix.c \
2 i2c.c \
3 split_util.c \
4 serial.c \
5 ssd1306.c
6
7# MCU name
8#MCU = at90usb1287
9MCU = atmega32u4
10
11# Processor frequency.
12# This will define a symbol, F_CPU, in all source code files equal to the
13# processor frequency in Hz. You can then use this symbol in your source code to
14# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
15# automatically to create a 32-bit value in your source code.
16#
17# This will be an integer division of F_USB below, as it is sourced by
18# F_USB after it has run through any CPU prescalers. Note that this value
19# does not *change* the processor frequency - it should merely be updated to
20# reflect the processor speed set externally so that the code can use accurate
21# software delays.
22F_CPU = 16000000
23
24#
25# LUFA specific
26#
27# Target architecture (see library "Board Types" documentation).
28ARCH = AVR8
29
30# Input clock frequency.
31# This will define a symbol, F_USB, in all source code files equal to the
32# input clock frequency (before any prescaling is performed) in Hz. This value may
33# differ from F_CPU if prescaling is used on the latter, and is required as the
34# raw input clock is fed directly to the PLL sections of the AVR for high speed
35# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
36# at the end, this will be done automatically to create a 32-bit value in your
37# source code.
38#
39# If no clock division is performed on the input clock inside the AVR (via the
40# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
41F_USB = $(F_CPU)
42
43# Interrupt driven control endpoint task(+60)
44OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT
45
46
47# Bootloader
48# This definition is optional, and if your keyboard supports multiple bootloaders of
49# different sizes, comment this out, and the correct address will be loaded
50# automatically (+60). See bootloader.mk for all options.
51BOOTLOADER = caterina
52
53# Build Options
54# change to "no" to disable the options, or define them in the Makefile in
55# the appropriate keymap folder that will get included automatically
56#
57BOOTMAGIC_ENABLE = no # Virtual DIP switch configuration(+1000)
58MOUSEKEY_ENABLE = no # Mouse keys(+4700)
59EXTRAKEY_ENABLE = no # Audio control and System control(+450)
60CONSOLE_ENABLE = no # Console for debug(+400)
61COMMAND_ENABLE = no # Commands for debug and configuration
62NKRO_ENABLE = no # Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work
63BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality
64MIDI_ENABLE = no # MIDI controls
65AUDIO_ENABLE = no # Audio output on port C6
66UNICODE_ENABLE = no # Unicode
67BLUETOOTH_ENABLE = no # Enable Bluetooth with the Adafruit EZ-Key HID
68RGBLIGHT_ENABLE = no # Enable WS2812 RGB underlight. Do not enable this with audio at the same time.
69SUBPROJECT_rev1 = no
70USE_I2C = no
71# Do not enable SLEEP_LED_ENABLE. it uses the same timer as BACKLIGHT_ENABLE
72SLEEP_LED_ENABLE = no # Breathing sleep LED during USB suspend
73
74CUSTOM_MATRIX = yes
75
76DEFAULT_FOLDER = lily58/rev1
diff --git a/keyboards/lily58/serial.c b/keyboards/lily58/serial.c
new file mode 100644
index 000000000..f85dc28dc
--- /dev/null
+++ b/keyboards/lily58/serial.c
@@ -0,0 +1,445 @@
1/*
2 * WARNING: be careful changing this code, it is very timing dependent
3 */
4
5#ifndef F_CPU
6#define F_CPU 16000000
7#endif
8
9#include <avr/io.h>
10#include <avr/interrupt.h>
11#include <util/delay.h>
12#include <stddef.h>
13#include <stdbool.h>
14#include "serial.h"
15//#include <pro_micro.h>
16
17#ifdef USE_SERIAL
18//#ifndef USE_SERIAL_PD2
19
20#ifndef SERIAL_USE_MULTI_TRANSACTION
21/* --- USE Simple API (OLD API, compatible with let's split serial.c) */
22 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
23 uint8_t volatile serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH] = {0};
24 #endif
25 #if SERIAL_MASTER_BUFFER_LENGTH > 0
26 uint8_t volatile serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH] = {0};
27 #endif
28 uint8_t volatile status0 = 0;
29
30SSTD_t transactions[] = {
31 { (uint8_t *)&status0,
32 #if SERIAL_MASTER_BUFFER_LENGTH > 0
33 sizeof(serial_master_buffer), (uint8_t *)serial_master_buffer,
34 #else
35 0, (uint8_t *)NULL,
36 #endif
37 #if SERIAL_SLAVE_BUFFER_LENGTH > 0
38 sizeof(serial_slave_buffer), (uint8_t *)serial_slave_buffer
39 #else
40 0, (uint8_t *)NULL,
41 #endif
42 }
43};
44
45void serial_master_init(void)
46{ soft_serial_initiator_init(transactions); }
47
48void serial_slave_init(void)
49{ soft_serial_target_init(transactions); }
50
51// 0 => no error
52// 1 => slave did not respond
53// 2 => checksum error
54int serial_update_buffers()
55{ return soft_serial_transaction(); }
56
57#endif // Simple API (OLD API, compatible with let's split serial.c)
58
59#define ALWAYS_INLINE __attribute__((always_inline))
60#define NO_INLINE __attribute__((noinline))
61#define _delay_sub_us(x) __builtin_avr_delay_cycles(x)
62
63// Serial pulse period in microseconds.
64#define TID_SEND_ADJUST 14
65
66#define SELECT_SERIAL_SPEED 1
67#if SELECT_SERIAL_SPEED == 0
68 // Very High speed
69 #define SERIAL_DELAY 4 // micro sec
70 #define READ_WRITE_START_ADJUST 33 // cycles
71 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
72#elif SELECT_SERIAL_SPEED == 1
73 // High speed
74 #define SERIAL_DELAY 6 // micro sec
75 #define READ_WRITE_START_ADJUST 30 // cycles
76 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
77#elif SELECT_SERIAL_SPEED == 2
78 // Middle speed
79 #define SERIAL_DELAY 12 // micro sec
80 #define READ_WRITE_START_ADJUST 30 // cycles
81 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
82#elif SELECT_SERIAL_SPEED == 3
83 // Low speed
84 #define SERIAL_DELAY 24 // micro sec
85 #define READ_WRITE_START_ADJUST 30 // cycles
86 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
87#elif SELECT_SERIAL_SPEED == 4
88 // Very Low speed
89 #define SERIAL_DELAY 50 // micro sec
90 #define READ_WRITE_START_ADJUST 30 // cycles
91 #define READ_WRITE_WIDTH_ADJUST 3 // cycles
92#else
93#error Illegal Serial Speed
94#endif
95
96
97#define SERIAL_DELAY_HALF1 (SERIAL_DELAY/2)
98#define SERIAL_DELAY_HALF2 (SERIAL_DELAY - SERIAL_DELAY/2)
99
100#define SLAVE_INT_WIDTH_US 1
101#ifndef SERIAL_USE_MULTI_TRANSACTION
102 #define SLAVE_INT_RESPONSE_TIME SERIAL_DELAY
103#else
104 #define SLAVE_INT_ACK_WIDTH_UNIT 2
105 #define SLAVE_INT_ACK_WIDTH 4
106#endif
107
108static SSTD_t *Transaction_table = NULL;
109
110inline static
111void serial_delay(void) {
112 _delay_us(SERIAL_DELAY);
113}
114
115inline static
116void serial_delay_half1(void) {
117 _delay_us(SERIAL_DELAY_HALF1);
118}
119
120inline static
121void serial_delay_half2(void) {
122 _delay_us(SERIAL_DELAY_HALF2);
123}
124
125inline static void serial_output(void) ALWAYS_INLINE;
126inline static
127void serial_output(void) {
128 SERIAL_PIN_DDR |= SERIAL_PIN_MASK;
129}
130
131// make the serial pin an input with pull-up resistor
132inline static void serial_input_with_pullup(void) ALWAYS_INLINE;
133inline static
134void serial_input_with_pullup(void) {
135 SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK;
136 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
137}
138
139inline static
140uint8_t serial_read_pin(void) {
141 return !!(SERIAL_PIN_INPUT & SERIAL_PIN_MASK);
142}
143
144inline static void serial_low(void) ALWAYS_INLINE;
145inline static
146void serial_low(void) {
147 SERIAL_PIN_PORT &= ~SERIAL_PIN_MASK;
148}
149
150inline static void serial_high(void) ALWAYS_INLINE;
151inline static
152void serial_high(void) {
153 SERIAL_PIN_PORT |= SERIAL_PIN_MASK;
154}
155
156void soft_serial_initiator_init(SSTD_t *sstd_table)
157{
158 Transaction_table = sstd_table;
159 serial_output();
160 serial_high();
161}
162
163void soft_serial_target_init(SSTD_t *sstd_table)
164{
165 Transaction_table = sstd_table;
166 serial_input_with_pullup();
167
168#if SERIAL_PIN_MASK == _BV(PD0)
169 // Enable INT0
170 EIMSK |= _BV(INT0);
171 // Trigger on falling edge of INT0
172 EICRA &= ~(_BV(ISC00) | _BV(ISC01));
173#elif SERIAL_PIN_MASK == _BV(PD2)
174 // Enable INT2
175 EIMSK |= _BV(INT2);
176 // Trigger on falling edge of INT2
177 EICRA &= ~(_BV(ISC20) | _BV(ISC21));
178#else
179 #error unknown SERIAL_PIN_MASK value
180#endif
181}
182
183// Used by the sender to synchronize timing with the reciver.
184static void sync_recv(void) NO_INLINE;
185static
186void sync_recv(void) {
187 for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) {
188 }
189 // This shouldn't hang if the target disconnects because the
190 // serial line will float to high if the target does disconnect.
191 while (!serial_read_pin());
192}
193
194// Used by the reciver to send a synchronization signal to the sender.
195static void sync_send(void)NO_INLINE;
196static
197void sync_send(void) {
198 serial_low();
199 serial_delay();
200 serial_high();
201}
202
203// Reads a byte from the serial line
204static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) NO_INLINE;
205static uint8_t serial_read_chunk(uint8_t *pterrcount, uint8_t bit) {
206 uint8_t byte, i, p, pb;
207
208 _delay_sub_us(READ_WRITE_START_ADJUST);
209 for( i = 0, byte = 0, p = 0; i < bit; i++ ) {
210 serial_delay_half1(); // read the middle of pulses
211 if( serial_read_pin() ) {
212 byte = (byte << 1) | 1; p ^= 1;
213 } else {
214 byte = (byte << 1) | 0; p ^= 0;
215 }
216 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
217 serial_delay_half2();
218 }
219 /* recive parity bit */
220 serial_delay_half1(); // read the middle of pulses
221 pb = serial_read_pin();
222 _delay_sub_us(READ_WRITE_WIDTH_ADJUST);
223 serial_delay_half2();
224
225 *pterrcount += (p != pb)? 1 : 0;
226
227 return byte;
228}
229
230// Sends a byte with MSB ordering
231void serial_write_chunk(uint8_t data, uint8_t bit) NO_INLINE;
232void serial_write_chunk(uint8_t data, uint8_t bit) {
233 uint8_t b, p;
234 for( p = 0, b = 1<<(bit-1); b ; b >>= 1) {
235 if(data & b) {
236 serial_high(); p ^= 1;
237 } else {
238 serial_low(); p ^= 0;
239 }
240 serial_delay();
241 }
242 /* send parity bit */
243 if(p & 1) { serial_high(); }
244 else { serial_low(); }
245 serial_delay();
246
247 serial_low(); // sync_send() / senc_recv() need raise edge
248}
249
250static void serial_send_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
251static
252void serial_send_packet(uint8_t *buffer, uint8_t size) {
253 for (uint8_t i = 0; i < size; ++i) {
254 uint8_t data;
255 data = buffer[i];
256 sync_send();
257 serial_write_chunk(data,8);
258 }
259}
260
261static uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) NO_INLINE;
262static
263uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) {
264 uint8_t pecount = 0;
265 for (uint8_t i = 0; i < size; ++i) {
266 uint8_t data;
267 sync_recv();
268 data = serial_read_chunk(&pecount, 8);
269 buffer[i] = data;
270 }
271 return pecount == 0;
272}
273
274inline static
275void change_sender2reciver(void) {
276 sync_send(); //0
277 serial_delay_half1(); //1
278 serial_low(); //2
279 serial_input_with_pullup(); //2
280 serial_delay_half1(); //3
281}
282
283inline static
284void change_reciver2sender(void) {
285 sync_recv(); //0
286 serial_delay(); //1
287 serial_low(); //3
288 serial_output(); //3
289 serial_delay_half1(); //4
290}
291
292// interrupt handle to be used by the target device
293ISR(SERIAL_PIN_INTERRUPT) {
294
295#ifndef SERIAL_USE_MULTI_TRANSACTION
296 serial_low();
297 serial_output();
298 SSTD_t *trans = Transaction_table;
299#else
300 // recive transaction table index
301 uint8_t tid;
302 uint8_t pecount = 0;
303 sync_recv();
304 tid = serial_read_chunk(&pecount,4);
305 if(pecount> 0)
306 return;
307 serial_delay_half1();
308
309 serial_high(); // response step1 low->high
310 serial_output();
311 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT*SLAVE_INT_ACK_WIDTH);
312 SSTD_t *trans = &Transaction_table[tid];
313 serial_low(); // response step2 ack high->low
314#endif
315
316 // target send phase
317 if( trans->target2initiator_buffer_size > 0 )
318 serial_send_packet((uint8_t *)trans->target2initiator_buffer,
319 trans->target2initiator_buffer_size);
320 // target switch to input
321 change_sender2reciver();
322
323 // target recive phase
324 if( trans->initiator2target_buffer_size > 0 ) {
325 if (serial_recive_packet((uint8_t *)trans->initiator2target_buffer,
326 trans->initiator2target_buffer_size) ) {
327 *trans->status = TRANSACTION_ACCEPTED;
328 } else {
329 *trans->status = TRANSACTION_DATA_ERROR;
330 }
331 } else {
332 *trans->status = TRANSACTION_ACCEPTED;
333 }
334
335 sync_recv(); //weit initiator output to high
336}
337
338/////////
339// start transaction by initiator
340//
341// int soft_serial_transaction(int sstd_index)
342//
343// Returns:
344// TRANSACTION_END
345// TRANSACTION_NO_RESPONSE
346// TRANSACTION_DATA_ERROR
347// this code is very time dependent, so we need to disable interrupts
348#ifndef SERIAL_USE_MULTI_TRANSACTION
349int soft_serial_transaction(void) {
350 SSTD_t *trans = Transaction_table;
351#else
352int soft_serial_transaction(int sstd_index) {
353 SSTD_t *trans = &Transaction_table[sstd_index];
354#endif
355 cli();
356
357 // signal to the target that we want to start a transaction
358 serial_output();
359 serial_low();
360 _delay_us(SLAVE_INT_WIDTH_US);
361
362#ifndef SERIAL_USE_MULTI_TRANSACTION
363 // wait for the target response
364 serial_input_with_pullup();
365 _delay_us(SLAVE_INT_RESPONSE_TIME);
366
367 // check if the target is present
368 if (serial_read_pin()) {
369 // target failed to pull the line low, assume not present
370 serial_output();
371 serial_high();
372 *trans->status = TRANSACTION_NO_RESPONSE;
373 sei();
374 return TRANSACTION_NO_RESPONSE;
375 }
376
377#else
378 // send transaction table index
379 sync_send();
380 _delay_sub_us(TID_SEND_ADJUST);
381 serial_write_chunk(sstd_index, 4);
382 serial_delay_half1();
383
384 // wait for the target response (step1 low->high)
385 serial_input_with_pullup();
386 while( !serial_read_pin() ) {
387 _delay_sub_us(2);
388 }
389
390 // check if the target is present (step2 high->low)
391 for( int i = 0; serial_read_pin(); i++ ) {
392 if (i > SLAVE_INT_ACK_WIDTH + 1) {
393 // slave failed to pull the line low, assume not present
394 serial_output();
395 serial_high();
396 *trans->status = TRANSACTION_NO_RESPONSE;
397 sei();
398 return TRANSACTION_NO_RESPONSE;
399 }
400 _delay_sub_us(SLAVE_INT_ACK_WIDTH_UNIT);
401 }
402#endif
403
404 // initiator recive phase
405 // if the target is present syncronize with it
406 if( trans->target2initiator_buffer_size > 0 ) {
407 if (!serial_recive_packet((uint8_t *)trans->target2initiator_buffer,
408 trans->target2initiator_buffer_size) ) {
409 serial_output();
410 serial_high();
411 *trans->status = TRANSACTION_DATA_ERROR;
412 sei();
413 return TRANSACTION_DATA_ERROR;
414 }
415 }
416
417 // initiator switch to output
418 change_reciver2sender();
419
420 // initiator send phase
421 if( trans->initiator2target_buffer_size > 0 ) {
422 serial_send_packet((uint8_t *)trans->initiator2target_buffer,
423 trans->initiator2target_buffer_size);
424 }
425
426 // always, release the line when not in use
427 sync_send();
428
429 *trans->status = TRANSACTION_END;
430 sei();
431 return TRANSACTION_END;
432}
433
434#ifdef SERIAL_USE_MULTI_TRANSACTION
435int soft_serial_get_and_clean_status(int sstd_index) {
436 SSTD_t *trans = &Transaction_table[sstd_index];
437 cli();
438 int retval = *trans->status;
439 *trans->status = 0;;
440 sei();
441 return retval;
442}
443#endif
444
445#endif
diff --git a/keyboards/lily58/serial.h b/keyboards/lily58/serial.h
new file mode 100644
index 000000000..d2b7fd8e6
--- /dev/null
+++ b/keyboards/lily58/serial.h
@@ -0,0 +1,80 @@
1#ifndef SOFT_SERIAL_H
2#define SOFT_SERIAL_H
3
4#include <stdbool.h>
5
6// /////////////////////////////////////////////////////////////////
7// Need Soft Serial defines in serial_config.h
8// /////////////////////////////////////////////////////////////////
9// ex.
10// #define SERIAL_PIN_DDR DDRD
11// #define SERIAL_PIN_PORT PORTD
12// #define SERIAL_PIN_INPUT PIND
13// #define SERIAL_PIN_MASK _BV(PD?) ?=0,2
14// #define SERIAL_PIN_INTERRUPT INT?_vect ?=0,2
15//
16// //// USE Simple API (OLD API, compatible with let's split serial.c)
17// ex.
18// #define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
19// #define SERIAL_MASTER_BUFFER_LENGTH 1
20//
21// //// USE flexible API (using multi-type transaction function)
22// #define SERIAL_USE_MULTI_TRANSACTION
23//
24// /////////////////////////////////////////////////////////////////
25
26
27#ifndef SERIAL_USE_MULTI_TRANSACTION
28/* --- USE Simple API (OLD API, compatible with let's split serial.c) */
29#if SERIAL_SLAVE_BUFFER_LENGTH > 0
30extern volatile uint8_t serial_slave_buffer[SERIAL_SLAVE_BUFFER_LENGTH];
31#endif
32#if SERIAL_MASTER_BUFFER_LENGTH > 0
33extern volatile uint8_t serial_master_buffer[SERIAL_MASTER_BUFFER_LENGTH];
34#endif
35
36void serial_master_init(void);
37void serial_slave_init(void);
38int serial_update_buffers(void);
39
40#endif // USE Simple API
41
42// Soft Serial Transaction Descriptor
43typedef struct _SSTD_t {
44 uint8_t *status;
45 uint8_t initiator2target_buffer_size;
46 uint8_t *initiator2target_buffer;
47 uint8_t target2initiator_buffer_size;
48 uint8_t *target2initiator_buffer;
49} SSTD_t;
50
51// initiator is transaction start side
52void soft_serial_initiator_init(SSTD_t *sstd_table);
53// target is interrupt accept side
54void soft_serial_target_init(SSTD_t *sstd_table);
55
56// initiator resullt
57#define TRANSACTION_END 0
58#define TRANSACTION_NO_RESPONSE 0x1
59#define TRANSACTION_DATA_ERROR 0x2
60#ifndef SERIAL_USE_MULTI_TRANSACTION
61int soft_serial_transaction(void);
62#else
63int soft_serial_transaction(int sstd_index);
64#endif
65
66// target status
67// *SSTD_t.status has
68// initiator:
69// TRANSACTION_END
70// or TRANSACTION_NO_RESPONSE
71// or TRANSACTION_DATA_ERROR
72// target:
73// TRANSACTION_DATA_ERROR
74// or TRANSACTION_ACCEPTED
75#define TRANSACTION_ACCEPTED 0x4
76#ifdef SERIAL_USE_MULTI_TRANSACTION
77int soft_serial_get_and_clean_status(int sstd_index);
78#endif
79
80#endif /* SOFT_SERIAL_H */
diff --git a/keyboards/lily58/serial_config.h b/keyboards/lily58/serial_config.h
new file mode 100644
index 000000000..fef689038
--- /dev/null
+++ b/keyboards/lily58/serial_config.h
@@ -0,0 +1,8 @@
1#define SERIAL_PIN_DDR DDRD
2#define SERIAL_PIN_PORT PORTD
3#define SERIAL_PIN_INPUT PIND
4#define SERIAL_PIN_MASK _BV(PD2)
5#define SERIAL_PIN_INTERRUPT INT2_vect
6
7#define SERIAL_SLAVE_BUFFER_LENGTH MATRIX_ROWS/2
8#define SERIAL_MASTER_BUFFER_LENGTH 1 \ No newline at end of file
diff --git a/keyboards/lily58/split_util.c b/keyboards/lily58/split_util.c
new file mode 100644
index 000000000..ff069b7e2
--- /dev/null
+++ b/keyboards/lily58/split_util.c
@@ -0,0 +1,86 @@
1#include <avr/io.h>
2#include <avr/wdt.h>
3#include <avr/power.h>
4#include <avr/interrupt.h>
5#include <util/delay.h>
6#include <avr/eeprom.h>
7#include "split_util.h"
8#include "matrix.h"
9#include "keyboard.h"
10#include "config.h"
11#include "timer.h"
12
13#ifdef USE_I2C
14# include "i2c.h"
15#else
16# include "serial.h"
17#endif
18
19volatile bool isLeftHand = true;
20
21static void setup_handedness(void) {
22 #ifdef EE_HANDS
23 isLeftHand = eeprom_read_byte(EECONFIG_HANDEDNESS);
24 #else
25 // I2C_MASTER_RIGHT is deprecated, use MASTER_RIGHT instead, since this works for both serial and i2c
26 #if defined(I2C_MASTER_RIGHT) || defined(MASTER_RIGHT)
27 isLeftHand = !has_usb();
28 #else
29 isLeftHand = has_usb();
30 #endif
31 #endif
32}
33
34static void keyboard_master_setup(void) {
35#ifdef USE_I2C
36 i2c_master_init();
37//#ifdef SSD1306OLED
38// matrix_master_OLED_init ();
39//#endif
40#else
41 serial_master_init();
42#endif
43}
44
45static void keyboard_slave_setup(void) {
46 timer_init();
47#ifdef USE_I2C
48 i2c_slave_init(SLAVE_I2C_ADDRESS);
49#else
50 serial_slave_init();
51#endif
52}
53
54bool has_usb(void) {
55 USBCON |= (1 << OTGPADE); //enables VBUS pad
56 _delay_us(5);
57 return (USBSTA & (1<<VBUS)); //checks state of VBUS
58}
59
60void split_keyboard_setup(void) {
61 setup_handedness();
62
63 if (has_usb()) {
64 keyboard_master_setup();
65 } else {
66 keyboard_slave_setup();
67 }
68 sei();
69}
70
71void keyboard_slave_loop(void) {
72 matrix_init();
73
74 while (1) {
75 matrix_slave_scan();
76 }
77}
78
79// this code runs before the usb and keyboard is initialized
80void matrix_setup(void) {
81 split_keyboard_setup();
82
83 if (!has_usb()) {
84 keyboard_slave_loop();
85 }
86}
diff --git a/keyboards/lily58/split_util.h b/keyboards/lily58/split_util.h
new file mode 100644
index 000000000..595a0659e
--- /dev/null
+++ b/keyboards/lily58/split_util.h
@@ -0,0 +1,20 @@
1#ifndef SPLIT_KEYBOARD_UTIL_H
2#define SPLIT_KEYBOARD_UTIL_H
3
4#include <stdbool.h>
5#include "eeconfig.h"
6
7#define SLAVE_I2C_ADDRESS 0x32
8
9extern volatile bool isLeftHand;
10
11// slave version of matix scan, defined in matrix.c
12void matrix_slave_scan(void);
13
14void split_keyboard_setup(void);
15bool has_usb(void);
16void keyboard_slave_loop(void);
17
18void matrix_master_OLED_init (void);
19
20#endif
diff --git a/keyboards/lily58/ssd1306.c b/keyboards/lily58/ssd1306.c
new file mode 100644
index 000000000..d07900119
--- /dev/null
+++ b/keyboards/lily58/ssd1306.c
@@ -0,0 +1,330 @@
1#ifdef SSD1306OLED
2
3#include "ssd1306.h"
4#include "i2c.h"
5#include <string.h>
6#include "print.h"
7#include "glcdfont.c"
8#ifdef ADAFRUIT_BLE_ENABLE
9#include "adafruit_ble.h"
10#endif
11#ifdef PROTOCOL_LUFA
12#include "lufa.h"
13#endif
14#include "sendchar.h"
15#include "timer.h"
16
17// Set this to 1 to help diagnose early startup problems
18// when testing power-on with ble. Turn it off otherwise,
19// as the latency of printing most of the debug info messes
20// with the matrix scan, causing keys to drop.
21#define DEBUG_TO_SCREEN 0
22
23//static uint16_t last_battery_update;
24//static uint32_t vbat;
25//#define BatteryUpdateInterval 10000 /* milliseconds */
26#define ScreenOffInterval 300000 /* milliseconds */
27#if DEBUG_TO_SCREEN
28static uint8_t displaying;
29#endif
30static uint16_t last_flush;
31
32// Write command sequence.
33// Returns true on success.
34static inline bool _send_cmd1(uint8_t cmd) {
35 bool res = false;
36
37 if (i2c_start_write(SSD1306_ADDRESS)) {
38 xprintf("failed to start write to %d\n", SSD1306_ADDRESS);
39 goto done;
40 }
41
42 if (i2c_master_write(0x0 /* command byte follows */)) {
43 print("failed to write control byte\n");
44
45 goto done;
46 }
47
48 if (i2c_master_write(cmd)) {
49 xprintf("failed to write command %d\n", cmd);
50 goto done;
51 }
52 res = true;
53done:
54 i2c_master_stop();
55 return res;
56}
57
58// Write 2-byte command sequence.
59// Returns true on success
60static inline bool _send_cmd2(uint8_t cmd, uint8_t opr) {
61 if (!_send_cmd1(cmd)) {
62 return false;
63 }
64 return _send_cmd1(opr);
65}
66
67// Write 3-byte command sequence.
68// Returns true on success
69static inline bool _send_cmd3(uint8_t cmd, uint8_t opr1, uint8_t opr2) {
70 if (!_send_cmd1(cmd)) {
71 return false;
72 }
73 if (!_send_cmd1(opr1)) {
74 return false;
75 }
76 return _send_cmd1(opr2);
77}
78
79#define send_cmd1(c) if (!_send_cmd1(c)) {goto done;}
80#define send_cmd2(c,o) if (!_send_cmd2(c,o)) {goto done;}
81#define send_cmd3(c,o1,o2) if (!_send_cmd3(c,o1,o2)) {goto done;}
82
83static void clear_display(void) {
84 matrix_clear(&display);
85
86 // Clear all of the display bits (there can be random noise
87 // in the RAM on startup)
88 send_cmd3(PageAddr, 0, (DisplayHeight / 8) - 1);
89 send_cmd3(ColumnAddr, 0, DisplayWidth - 1);
90
91 if (i2c_start_write(SSD1306_ADDRESS)) {
92 goto done;
93 }
94 if (i2c_master_write(0x40)) {
95 // Data mode
96 goto done;
97 }
98 for (uint8_t row = 0; row < MatrixRows; ++row) {
99 for (uint8_t col = 0; col < DisplayWidth; ++col) {
100 i2c_master_write(0);
101 }
102 }
103
104 display.dirty = false;
105
106done:
107 i2c_master_stop();
108}
109
110#if DEBUG_TO_SCREEN
111#undef sendchar
112static int8_t capture_sendchar(uint8_t c) {
113 sendchar(c);
114 iota_gfx_write_char(c);
115
116 if (!displaying) {
117 iota_gfx_flush();
118 }
119 return 0;
120}
121#endif
122
123bool iota_gfx_init(bool rotate) {
124 bool success = false;
125
126 send_cmd1(DisplayOff);
127 send_cmd2(SetDisplayClockDiv, 0x80);
128 send_cmd2(SetMultiPlex, DisplayHeight - 1);
129
130 send_cmd2(SetDisplayOffset, 0);
131
132
133 send_cmd1(SetStartLine | 0x0);
134 send_cmd2(SetChargePump, 0x14 /* Enable */);
135 send_cmd2(SetMemoryMode, 0 /* horizontal addressing */);
136
137 if(rotate){
138 // the following Flip the display orientation 180 degrees
139 send_cmd1(SegRemap);
140 send_cmd1(ComScanInc);
141 }else{
142 // Flips the display orientation 0 degrees
143 send_cmd1(SegRemap | 0x1);
144 send_cmd1(ComScanDec);
145 }
146
147 send_cmd2(SetComPins, 0x2);
148 send_cmd2(SetContrast, 0x8f);
149 send_cmd2(SetPreCharge, 0xf1);
150 send_cmd2(SetVComDetect, 0x40);
151 send_cmd1(DisplayAllOnResume);
152 send_cmd1(NormalDisplay);
153 send_cmd1(DeActivateScroll);
154 send_cmd1(DisplayOn);
155
156 send_cmd2(SetContrast, 0); // Dim
157
158 clear_display();
159
160 success = true;
161
162 iota_gfx_flush();
163
164#if DEBUG_TO_SCREEN
165 print_set_sendchar(capture_sendchar);
166#endif
167
168done:
169 return success;
170}
171
172bool iota_gfx_off(void) {
173 bool success = false;
174
175 send_cmd1(DisplayOff);
176 success = true;
177
178done:
179 return success;
180}
181
182bool iota_gfx_on(void) {
183 bool success = false;
184
185 send_cmd1(DisplayOn);
186 success = true;
187
188done:
189 return success;
190}
191
192void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c) {
193 *matrix->cursor = c;
194 ++matrix->cursor;
195
196 if (matrix->cursor - &matrix->display[0][0] == sizeof(matrix->display)) {
197 // We went off the end; scroll the display upwards by one line
198 memmove(&matrix->display[0], &matrix->display[1],
199 MatrixCols * (MatrixRows - 1));
200 matrix->cursor = &matrix->display[MatrixRows - 1][0];
201 memset(matrix->cursor, ' ', MatrixCols);
202 }
203}
204
205void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c) {
206 matrix->dirty = true;
207
208 if (c == '\n') {
209 // Clear to end of line from the cursor and then move to the
210 // start of the next line
211 uint8_t cursor_col = (matrix->cursor - &matrix->display[0][0]) % MatrixCols;
212
213 while (cursor_col++ < MatrixCols) {
214 matrix_write_char_inner(matrix, ' ');
215 }
216 return;
217 }
218
219 matrix_write_char_inner(matrix, c);
220}
221
222void iota_gfx_write_char(uint8_t c) {
223 matrix_write_char(&display, c);
224}
225
226void matrix_write(struct CharacterMatrix *matrix, const char *data) {
227 const char *end = data + strlen(data);
228 while (data < end) {
229 matrix_write_char(matrix, *data);
230 ++data;
231 }
232}
233
234void matrix_write_ln(struct CharacterMatrix *matrix, const char *data) {
235 char data_ln[strlen(data)+2];
236 snprintf(data_ln, sizeof(data_ln), "%s\n", data);
237 matrix_write(matrix, data_ln);
238}
239
240void iota_gfx_write(const char *data) {
241 matrix_write(&display, data);
242}
243
244void matrix_write_P(struct CharacterMatrix *matrix, const char *data) {
245 while (true) {
246 uint8_t c = pgm_read_byte(data);
247 if (c == 0) {
248 return;
249 }
250 matrix_write_char(matrix, c);
251 ++data;
252 }
253}
254
255void iota_gfx_write_P(const char *data) {
256 matrix_write_P(&display, data);
257}
258
259void matrix_clear(struct CharacterMatrix *matrix) {
260 memset(matrix->display, ' ', sizeof(matrix->display));
261 matrix->cursor = &matrix->display[0][0];
262 matrix->dirty = true;
263}
264
265void iota_gfx_clear_screen(void) {
266 matrix_clear(&display);
267}
268
269void matrix_render(struct CharacterMatrix *matrix) {
270 last_flush = timer_read();
271 iota_gfx_on();
272#if DEBUG_TO_SCREEN
273 ++displaying;
274#endif
275
276 // Move to the home position
277 send_cmd3(PageAddr, 0, MatrixRows - 1);
278 send_cmd3(ColumnAddr, 0, (MatrixCols * FontWidth) - 1);
279
280 if (i2c_start_write(SSD1306_ADDRESS)) {
281 goto done;
282 }
283 if (i2c_master_write(0x40)) {
284 // Data mode
285 goto done;
286 }
287
288 for (uint8_t row = 0; row < MatrixRows; ++row) {
289 for (uint8_t col = 0; col < MatrixCols; ++col) {
290 const uint8_t *glyph = font + (matrix->display[row][col] * FontWidth);
291
292 for (uint8_t glyphCol = 0; glyphCol < FontWidth; ++glyphCol) {
293 uint8_t colBits = pgm_read_byte(glyph + glyphCol);
294 i2c_master_write(colBits);
295 }
296
297 // 1 column of space between chars (it's not included in the glyph)
298 //i2c_master_write(0);
299 }
300 }
301
302 matrix->dirty = false;
303
304done:
305 i2c_master_stop();
306#if DEBUG_TO_SCREEN
307 --displaying;
308#endif
309}
310
311void iota_gfx_flush(void) {
312 matrix_render(&display);
313}
314
315__attribute__ ((weak))
316void iota_gfx_task_user(void) {
317}
318
319void iota_gfx_task(void) {
320 iota_gfx_task_user();
321
322 if (display.dirty) {
323 iota_gfx_flush();
324 }
325
326 if (timer_elapsed(last_flush) > ScreenOffInterval) {
327 iota_gfx_off();
328 }
329}
330#endif
diff --git a/keyboards/lily58/ssd1306.h b/keyboards/lily58/ssd1306.h
new file mode 100644
index 000000000..59d31c9f3
--- /dev/null
+++ b/keyboards/lily58/ssd1306.h
@@ -0,0 +1,94 @@
1#ifndef SSD1306_H
2#define SSD1306_H
3
4#include <stdbool.h>
5#include <stdio.h>
6#include "pincontrol.h"
7#include "config.h"
8
9enum ssd1306_cmds {
10 DisplayOff = 0xAE,
11 DisplayOn = 0xAF,
12
13 SetContrast = 0x81,
14 DisplayAllOnResume = 0xA4,
15
16 DisplayAllOn = 0xA5,
17 NormalDisplay = 0xA6,
18 InvertDisplay = 0xA7,
19 SetDisplayOffset = 0xD3,
20 SetComPins = 0xda,
21 SetVComDetect = 0xdb,
22 SetDisplayClockDiv = 0xD5,
23 SetPreCharge = 0xd9,
24 SetMultiPlex = 0xa8,
25 SetLowColumn = 0x00,
26 SetHighColumn = 0x10,
27 SetStartLine = 0x40,
28
29 SetMemoryMode = 0x20,
30 ColumnAddr = 0x21,
31 PageAddr = 0x22,
32
33 ComScanInc = 0xc0,
34 ComScanDec = 0xc8,
35 SegRemap = 0xa0,
36 SetChargePump = 0x8d,
37 ExternalVcc = 0x01,
38 SwitchCapVcc = 0x02,
39
40 ActivateScroll = 0x2f,
41 DeActivateScroll = 0x2e,
42 SetVerticalScrollArea = 0xa3,
43 RightHorizontalScroll = 0x26,
44 LeftHorizontalScroll = 0x27,
45 VerticalAndRightHorizontalScroll = 0x29,
46 VerticalAndLeftHorizontalScroll = 0x2a,
47};
48
49// Controls the SSD1306 128x32 OLED display via i2c
50
51#ifndef SSD1306_ADDRESS
52#define SSD1306_ADDRESS 0x3C
53#endif
54
55#define DisplayHeight 32
56#define DisplayWidth 128
57
58#define FontHeight 8
59#define FontWidth 6
60
61#define MatrixRows (DisplayHeight / FontHeight)
62#define MatrixCols (DisplayWidth / FontWidth)
63
64struct CharacterMatrix {
65 uint8_t display[MatrixRows][MatrixCols];
66 uint8_t *cursor;
67 bool dirty;
68};
69
70struct CharacterMatrix display;
71
72bool iota_gfx_init(bool rotate);
73void iota_gfx_task(void);
74bool iota_gfx_off(void);
75bool iota_gfx_on(void);
76void iota_gfx_flush(void);
77void iota_gfx_write_char(uint8_t c);
78void iota_gfx_write(const char *data);
79void iota_gfx_write_P(const char *data);
80void iota_gfx_clear_screen(void);
81
82void iota_gfx_task_user(void);
83
84void matrix_clear(struct CharacterMatrix *matrix);
85void matrix_write_char_inner(struct CharacterMatrix *matrix, uint8_t c);
86void matrix_write_char(struct CharacterMatrix *matrix, uint8_t c);
87void matrix_write(struct CharacterMatrix *matrix, const char *data);
88void matrix_write_ln(struct CharacterMatrix *matrix, const char *data);
89void matrix_write_P(struct CharacterMatrix *matrix, const char *data);
90void matrix_render(struct CharacterMatrix *matrix);
91
92
93
94#endif