Add a demo code for PicoCalc

This commit is contained in:
cuu
2025-03-20 14:45:44 +08:00
parent 96b9122375
commit 67452abeb1
11 changed files with 1362 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
# Generated Cmake Pico project file
cmake_minimum_required(VERSION 3.13)
set(CMAKE_C_STANDARD 11)
project(i2ckbd
VERSION 0.0.1
DESCRIPTION "i2ckbd for picocalc."
)
add_library(i2ckbd INTERFACE)
target_sources(i2ckbd INTERFACE
i2ckbd.c
)
target_link_libraries(i2ckbd INTERFACE pico_stdlib hardware_spi hardware_i2c)
target_include_directories(i2ckbd INTERFACE ${CMAKE_CURRENT_LIST_DIR})

View File

@@ -0,0 +1,57 @@
#include <stdio.h>
#include <pico/stdio.h>
#include "i2ckbd.h"
static uint8_t i2c_inited = 0;
void init_i2c_kbd() {
gpio_set_function(I2C_KBD_SCL, GPIO_FUNC_I2C);
gpio_set_function(I2C_KBD_SDA, GPIO_FUNC_I2C);
i2c_init(I2C_KBD_MOD, I2C_KBD_SPEED);
gpio_pull_up(I2C_KBD_SCL);
gpio_pull_up(I2C_KBD_SDA);
i2c_inited = 1;
}
int read_i2c_kbd() {
int retval;
static int ctrlheld = 0;
uint16_t buff = 0;
unsigned char msg[2];
int c = -1;
msg[0] = 0x09;
if (i2c_inited == 0) return -1;
retval = i2c_write_timeout_us(I2C_KBD_MOD, I2C_KBD_ADDR, msg, 1, false, 500000);
if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) {
printf("i2c write error\n");
return -1;
}
retval = i2c_read_timeout_us(I2C_KBD_MOD, I2C_KBD_ADDR, (unsigned char *) &buff, 2, false, 500000);
if (retval == PICO_ERROR_GENERIC || retval == PICO_ERROR_TIMEOUT) {
printf("i2c read error read\n");
return -1;
}
if (buff != 0) {
if (buff == 0x7e03)ctrlheld = 0;
else if (buff == 0x7e02) {
ctrlheld = 1;
} else if ((buff & 0xff) == 1) {//pressed
c = buff >> 8;
int realc = -1;
switch (c) {
default:
realc = c;
break;
}
c = realc;
if (c >= 'a' && c <= 'z' && ctrlheld)c = c - 'a' + 1;
}
return c;
}
return -1;
}

View File

@@ -0,0 +1,19 @@
#ifndef I2C_KEYBOARD_H
#define I2C_KEYBOARD_H
#include <pico/stdlib.h>
#include <pico/platform.h>
#include <hardware/gpio.h>
#include <hardware/i2c.h>
#define I2C_KBD_MOD i2c1
#define I2C_KBD_SDA 6
#define I2C_KBD_SCL 7
#define I2C_KBD_SPEED 400000 // if dual i2c, then the speed of keyboard i2c should be 10khz
#define I2C_KBD_ADDR 0x1F
void init_i2c_kbd();
int read_i2c_kbd();
#endif