]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard/ergodox/ergodox.h
Add a qwerty layer
[max/tmk_keyboard.git] / keyboard / ergodox / ergodox.h
1 /*
2 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18 Copyright (c) 2012, 2013 Ben Blazak <benblazak.dev@gmail.com>
19 Released under The MIT License (see "doc/licenses/MIT.md")
20 Project located at <https://github.com/benblazak/ergodox-firmware>
21
22 Most used files are located at
23 <https://github.com/benblazak/ergodox-firmware/tree/partial-rewrite/firmware/keyboard/ergodox/controller>
24
25 */
26
27 #ifndef ERGODOX_H
28 #define ERGODOX_H
29
30 #include <stdint.h>
31 #include <stdbool.h>
32 #include <avr/io.h>
33 #include "i2cmaster.h"
34
35 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
36 #define CPU_16MHz       0x00
37
38 // I2C aliases and register addresses (see "mcp23018.md")
39 #define I2C_ADDR        0b0100000
40 #define I2C_ADDR_WRITE  ( (I2C_ADDR<<1) | I2C_WRITE )
41 #define I2C_ADDR_READ   ( (I2C_ADDR<<1) | I2C_READ  )
42 #define IODIRA          0x00            // i/o direction register
43 #define IODIRB          0x01
44 #define GPPUA           0x0C            // GPIO pull-up resistor register
45 #define GPPUB           0x0D
46 #define GPIOA           0x12            // general purpose i/o port register (write modifies OLAT)
47 #define GPIOB           0x13
48 #define OLATA           0x14            // output latch register
49 #define OLATB           0x15
50
51 extern uint8_t mcp23018_status;
52
53 void init_ergodox(void);
54 void ergodox_blink_all_leds(void);
55 uint8_t init_mcp23018(void);
56 uint8_t ergodox_left_leds_update(void);
57
58 #define LED_BRIGHTNESS_LO       31
59 #define LED_BRIGHTNESS_HI       255
60
61 #define LEFT_LED_1_SHIFT        7       // in MCP23018 port B
62 #define LEFT_LED_2_SHIFT        6       // in MCP23018 port B
63 #define LEFT_LED_3_SHIFT        7       // in MCP23018 port A
64
65 extern bool ergodox_left_led_1;         // left top
66 extern bool ergodox_left_led_2;         // left middle
67 extern bool ergodox_left_led_3;         // left bottom
68
69 static inline void ergodox_board_led_on(void)      { DDRD |=  (1<<6); PORTD |=  (1<<6); }
70 static inline void ergodox_right_led_1_on(void)    { DDRB |=  (1<<5); PORTB |=  (1<<5); }
71 static inline void ergodox_right_led_2_on(void)    { DDRB |=  (1<<6); PORTB |=  (1<<6); }
72 static inline void ergodox_right_led_3_on(void)    { DDRB |=  (1<<7); PORTB |=  (1<<7); }
73 static inline void ergodox_left_led_1_on(void)     { ergodox_left_led_1 = 1; }
74 static inline void ergodox_left_led_2_on(void)     { ergodox_left_led_2 = 1; }
75 static inline void ergodox_left_led_3_on(void)     { ergodox_left_led_3 = 1; }
76
77 static inline void ergodox_board_led_off(void)     { DDRD &= ~(1<<6); PORTD &= ~(1<<6); }
78 static inline void ergodox_right_led_1_off(void)   { DDRB &= ~(1<<5); PORTB &= ~(1<<5); }
79 static inline void ergodox_right_led_2_off(void)   { DDRB &= ~(1<<6); PORTB &= ~(1<<6); }
80 static inline void ergodox_right_led_3_off(void)   { DDRB &= ~(1<<7); PORTB &= ~(1<<7); }
81 static inline void ergodox_left_led_1_off(void)    { ergodox_left_led_1 = 0; }
82 static inline void ergodox_left_led_2_off(void)    { ergodox_left_led_2 = 0; }
83 static inline void ergodox_left_led_3_off(void)    { ergodox_left_led_3 = 0; }
84
85 static inline void ergodox_led_all_on(void)
86 {
87     ergodox_board_led_on();
88     ergodox_right_led_1_on();
89     ergodox_right_led_2_on();
90     ergodox_right_led_3_on();
91     ergodox_left_led_1_on();
92     ergodox_left_led_2_on();
93     ergodox_left_led_3_on();
94     ergodox_left_leds_update();
95 }
96
97 static inline void ergodox_led_all_off(void)
98 {
99     ergodox_board_led_off();
100     ergodox_right_led_1_off();
101     ergodox_right_led_2_off();
102     ergodox_right_led_3_off();
103     ergodox_left_led_1_off();
104     ergodox_left_led_2_off();
105     ergodox_left_led_3_off();
106     ergodox_left_leds_update();
107 }
108
109 static inline void ergodox_right_led_1_set(uint8_t n)    { OCR1A = n; }
110 static inline void ergodox_right_led_2_set(uint8_t n)    { OCR1B = n; }
111 static inline void ergodox_right_led_3_set(uint8_t n)    { OCR1C = n; }
112
113 static inline void ergodox_led_all_set(uint8_t n)
114 {
115     ergodox_right_led_1_set(n);
116     ergodox_right_led_2_set(n);
117     ergodox_right_led_3_set(n);
118 }
119
120 #endif