Skip to content

Commit f4a1bef

Browse files
committed
Add picocalc stm32 i2c keyboard code
1 parent 3bfa816 commit f4a1bef

File tree

16 files changed

+1552
-0
lines changed

16 files changed

+1552
-0
lines changed

Code/picocalc_keyboard/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# picocalc keyboard
2+
* STM32F103R8T6
3+
4+
## Libraries
5+
* https://github.com/stm32duino/Arduino_Core_STM32/

Code/picocalc_keyboard/backlight.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef BACKLIGHT_H
2+
#define BACKLIGHT_H
3+
4+
void lcd_backlight_update_reg();
5+
6+
void lcd_backlight_update(int);
7+
void kbd_backlight_update(int);
8+
9+
#endif
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
#include "backlight.h"
3+
4+
#define KBD_BACKLIGHT_SEG 4
5+
uint8_t kbd_backlight_segs[KBD_BACKLIGHT_SEG] = {20,40,80,110};
6+
uint8_t kbd_backlight_offset = 0;
7+
8+
void lcd_backlight_update_reg() {
9+
10+
uint8_t val;
11+
12+
val = reg_get_value(REG_ID_BKL);
13+
val = val & 0xff;
14+
15+
analogWriteFrequency(10000);
16+
analogWrite(PA8, val);
17+
18+
}
19+
20+
void lcd_backlight_update(int v) {
21+
int val;
22+
23+
val = reg_get_value(REG_ID_BKL);
24+
val += v;
25+
if(val < 0) val = 0;
26+
if(val > 0xff) val = 0xff;
27+
28+
analogWriteFrequency(10000);
29+
analogWrite(PA8, val);
30+
reg_set_value(REG_ID_BKL,val);
31+
}
32+
33+
void kbd_backlight_update(int v){
34+
int val;
35+
36+
val = reg_get_value(REG_ID_BK2);
37+
val += v;
38+
if(val < 20 ) val = 0;
39+
if(val > 0xff) val = 0;
40+
41+
analogWriteFrequency(10000);
42+
analogWrite(PC8, val);
43+
reg_set_value(REG_ID_BK2,val);
44+
}
45+
46+
void kbd_backlight_update_offset(){
47+
int val;
48+
kbd_backlight_offset++;
49+
kbd_backlight_offset = kbd_backlight_offset % KBD_BACKLIGHT_SEG;
50+
51+
val = reg_get_value(REG_ID_BK2);
52+
val += kbd_backlight_segs[kbd_backlight_offset];
53+
if(val < 20 ) val = 0;
54+
if(val > 0xff) val = 0;
55+
56+
analogWriteFrequency(10000);
57+
analogWrite(PC8, val);
58+
reg_set_value(REG_ID_BK2,val);
59+
}

Code/picocalc_keyboard/battery.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef BATTERY_H
2+
#define BATTERY_H
3+
4+
void show_bat_segs();
5+
6+
void low_bat();
7+
8+
void start_chg();
9+
void stop_chg();
10+
11+
#endif

Code/picocalc_keyboard/battery.ino

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "battery.h"
2+
3+
4+
void indicator_led_on(){
5+
digitalWrite(PC13, LOW);
6+
}
7+
8+
void indicator_led_off(){
9+
digitalWrite(PC13, HIGH);
10+
}
11+
12+
void flash_one_time(int ts,int restore_status) {
13+
for(int i=0;i<ts;i++) {
14+
indicator_led_on();
15+
delay(400);
16+
indicator_led_off();
17+
delay(200);
18+
}
19+
digitalWrite(PC13,restore_status);
20+
}
21+
22+
void show_bat_segs(){
23+
if(!PMU.isBatteryConnect()) return;
24+
25+
int pcnt = PMU.getBatteryPercent();
26+
int last_d201_status = digitalRead(PC13);
27+
28+
if(pcnt >0 && pcnt < 33) {
29+
//show one time
30+
flash_one_time(1,last_d201_status);
31+
}else if(pcnt >= 33 && pcnt <66){
32+
//show 2 times
33+
flash_one_time(2,last_d201_status);
34+
}else if(pcnt >=66 && pcnt <= 100){
35+
//show 3 times
36+
flash_one_time(3,last_d201_status);
37+
}
38+
39+
if(PMU.isCharging()){
40+
start_chg();
41+
}
42+
43+
}
44+
45+
void low_bat(){
46+
if(PMU.isBatteryConnect() && !PMU.isCharging()){
47+
int pcnt = PMU.getBatteryPercent();
48+
if(pcnt <= LOW_BAT_VAL){
49+
//This is related to the battery charging and discharging logic. If you're not sure what you're doing, please don't modify it, as it could damage the battery.
50+
indicator_led_off();
51+
if(pcnt <= 1) {//This is related to the battery charging and discharging logic. If you're not sure what you're doing, please don't modify it, as it could damage the battery.
52+
PMU.setChargingLedMode(XPOWERS_CHG_LED_BLINK_4HZ);
53+
if(pcnt==0){//This is related to the battery charging and discharging logic. If you're not sure what you're doing, please don't modify it, as it could damage the battery.
54+
PMU.shutdown();//This is related to the battery charging and discharging logic. If you're not sure what you're doing, please don't modify it, as it could damage the battery.
55+
}
56+
}else{
57+
PMU.setChargingLedMode(XPOWERS_CHG_LED_ON);
58+
}
59+
}else{
60+
indicator_led_on();
61+
PMU.setChargingLedMode(XPOWERS_CHG_LED_OFF);
62+
}
63+
}
64+
}
65+
66+
void start_chg(){
67+
indicator_led_on();
68+
PMU.setChargingLedMode(XPOWERS_CHG_LED_BLINK_1HZ);
69+
}
70+
71+
void stop_chg(){
72+
PMU.setChargingLedMode(XPOWERS_CHG_LED_OFF);
73+
low_bat();
74+
}

Code/picocalc_keyboard/conf_app.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef CONF_APP_H
2+
#define CONF_APP_H
3+
4+
#define SLAVE_ADDRESS 0x1F
5+
#define FIFO_SIZE 31
6+
7+
#define INT_DURATION_MS 1
8+
9+
#ifndef CONFIG_PMU_SDA
10+
#define CONFIG_PMU_SDA PB11
11+
#endif
12+
13+
#ifndef CONFIG_PMU_SCL
14+
#define CONFIG_PMU_SCL PB10
15+
#endif
16+
17+
#ifndef CONFIG_PMU_IRQ
18+
#define CONFIG_PMU_IRQ PC9
19+
#endif
20+
21+
22+
#define LOW_BAT_VAL 20
23+
#define LCD_BACKLIGHT_STEP 10
24+
25+
26+
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
27+
#define bitSet(value, bit) ((value) |= (1 << (bit)))
28+
#define bitClear(value, bit) ((value) &= ~(1 << (bit)))
29+
#define bitWrite(value, bit, bitvalue) ((bitvalue) ? bitSet((value), (bit)) : bitClear((value), (bit) ))
30+
31+
#endif

Code/picocalc_keyboard/fifo.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef FIFO_H
2+
#define FIFO_H
3+
4+
#include "keyboard.h"
5+
6+
struct fifo_item {
7+
char key;
8+
enum key_state state;
9+
};
10+
11+
uint8_t fifo_count(void);
12+
void fifo_flush(void);
13+
bool fifo_enqueue(const struct fifo_item item);
14+
void fifo_enqueue_force(const struct fifo_item item);
15+
struct fifo_item fifo_dequeue(void);
16+
17+
18+
#endif

Code/picocalc_keyboard/fifo.ino

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "fifo.h"
2+
3+
static struct {
4+
struct fifo_item fifo[FIFO_SIZE];
5+
uint8_t count;
6+
uint8_t read_idx;
7+
uint8_t write_idx;
8+
} fifo_self;
9+
10+
uint8_t fifo_count(void)
11+
{
12+
return fifo_self.count;
13+
}
14+
15+
void fifo_flush(void)
16+
{
17+
fifo_self.write_idx = 0;
18+
fifo_self.read_idx = 0;
19+
fifo_self.count = 0;
20+
}
21+
22+
bool fifo_enqueue(const struct fifo_item item)
23+
{
24+
if (fifo_self.count >= FIFO_SIZE)
25+
return false;
26+
27+
fifo_self.fifo[fifo_self.write_idx++] = item;
28+
29+
fifo_self.write_idx %= FIFO_SIZE;
30+
++fifo_self.count;
31+
32+
return true;
33+
}
34+
35+
void fifo_enqueue_force(const struct fifo_item item)
36+
{
37+
if (fifo_enqueue(item))
38+
return;
39+
40+
fifo_self.fifo[fifo_self.write_idx++] = item;
41+
fifo_self.write_idx %= FIFO_SIZE;
42+
43+
fifo_self.read_idx++;
44+
fifo_self.read_idx %= FIFO_SIZE;
45+
}
46+
47+
struct fifo_item fifo_dequeue(void)
48+
{
49+
struct fifo_item item = { 0 };
50+
if (fifo_self.count == 0)
51+
return item;
52+
53+
item = fifo_self.fifo[fifo_self.read_idx++];
54+
fifo_self.read_idx %= FIFO_SIZE;
55+
--fifo_self.count;
56+
57+
return item;
58+
}

Code/picocalc_keyboard/keyboard.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#ifndef KEYBOARD_H
2+
#define KEYBOARD_H
3+
4+
enum key_state
5+
{
6+
KEY_STATE_IDLE = 0,
7+
KEY_STATE_PRESSED,
8+
KEY_STATE_HOLD,
9+
KEY_STATE_RELEASED,
10+
};
11+
12+
#define KEY_JOY_UP 0x01
13+
#define KEY_JOY_DOWN 0x02
14+
#define KEY_JOY_LEFT 0x03
15+
#define KEY_JOY_RIGHT 0x04
16+
#define KEY_JOY_CENTER 0x05
17+
#define KEY_BTN_LEFT1 0x06
18+
#define KEY_BTN_RIGHT1 0x07
19+
20+
#define KEY_BACKSPACE 0x08
21+
#define KEY_TAB 0x09
22+
#define KEY_ENTER 0x0A
23+
// 0x0D - CARRIAGE RETURN
24+
#define KEY_BTN_LEFT2 0x11
25+
#define KEY_BTN_RIGHT2 0x12
26+
27+
28+
#define KEY_MOD_ALT 0xA1
29+
#define KEY_MOD_SHL 0xA2
30+
#define KEY_MOD_SHR 0xA3
31+
#define KEY_MOD_SYM 0xA4
32+
#define KEY_MOD_CTRL 0xA5
33+
34+
#define KEY_ESC 0xB1
35+
#define KEY_UP 0xb5
36+
#define KEY_DOWN 0xb6
37+
#define KEY_LEFT 0xb4
38+
#define KEY_RIGHT 0xb7
39+
40+
#define KEY_BREAK 0xd0 // == KEY_PAUSE
41+
#define KEY_INSERT 0xD1
42+
#define KEY_HOME 0xD2
43+
#define KEY_DEL 0xD4
44+
#define KEY_END 0xD5
45+
#define KEY_PAGE_UP 0xd6
46+
#define KEY_PAGE_DOWN 0xd7
47+
48+
#define KEY_CAPS_LOCK 0xC1
49+
50+
#define KEY_F1 0x81
51+
#define KEY_F2 0x82
52+
#define KEY_F3 0x83
53+
#define KEY_F4 0x84
54+
#define KEY_F5 0x85
55+
#define KEY_F6 0x86
56+
#define KEY_F7 0x87
57+
#define KEY_F8 0x88
58+
#define KEY_F9 0x89
59+
#define KEY_F10 0x90
60+
61+
typedef void (*key_callback)(char, enum key_state);
62+
typedef void (*lock_callback)(bool, bool);
63+
64+
void keyboard_process(void);
65+
void keyboard_set_key_callback(key_callback callback);
66+
void keyboard_set_lock_callback(lock_callback callback);
67+
bool keyboard_get_capslock(void);
68+
bool keyboard_get_numlock(void);
69+
void keyboard_init(void);
70+
71+
#define NUM_OF_COLS 8
72+
#define NUM_OF_ROWS 7
73+
74+
#define NUM_OF_BTNS 12
75+
76+
#define KEY_POLL_TIME 16
77+
78+
#define KEY_LIST_SIZE 10
79+
80+
81+
#define KEY_HOLD_TIME 300
82+
83+
#endif

0 commit comments

Comments
 (0)