Initial push

This commit is contained in:
Philip Smart
2022-01-29 16:16:02 +00:00
commit f828445b76
17 changed files with 5694 additions and 0 deletions

46
.gitignore vendored Normal file
View File

@@ -0,0 +1,46 @@
.metadata
.dm
.gradle
/Releases
/.nb-gradle/
*.bin
*.dmp
*.elf
*.lss
*.map
*.rpt
*.srec
*.swp
*.zpu
*.log
*.done
*.smsg
*.summary
*.jdi
*.pin
*.out.sdc
*.sof
*.sld
*.rbf
*.qws
*.sav
*.pof
*.qdf
*.srf
*.swo
build/
old/
*/old/
*/*/old/
*/*/*/old/
*.o
Manuals/
schematics/previous
*.bak
*.test
*.old
build_properties.temp.cmake
component_properties.temp.cmake
components_with_manifests_list.temp
main/CMakeLists.txt.orig
sdkconfigq

8
CMakeLists.txt Normal file
View File

@@ -0,0 +1,8 @@
# For more information about build system see
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(main)

4
main/CMakeLists.txt Normal file
View File

@@ -0,0 +1,4 @@
set(COMPONENT_SRCS main.cpp ssd1306.c ssd1306_i2c.c ssd1306_spi.c PS2KeyAdvanced.cpp)
set(COMPONENT_ADD_INCLUDEDIRS ".")
register_component()

308
main/Kconfig.projbuild Normal file
View File

@@ -0,0 +1,308 @@
menu "MZ25Key Configuration"
menu "PS2 Keyboard"
config PS2_HW_DATAPIN
int "GPIO pin number used for the PS/2 DATA line"
range 0 46
default 14
help
GPIO number (IOxx) used to connect with the PS/2 Keyboard DATA line.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to I2C.
GPIOs 35-39 are input-only so cannot be used as outputs.
config PS2_HW_CLKPIN
int "GPIO pin number used for the PS/2 CLK line"
range 0 46
default 13
help
GPIO number (IOxx) used to connect with the PS/2 Keyboard CLK line.
This pin must be interrupt capable.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to I2C.
GPIOs 35-39 are input-only so cannot be used as outputs.
choice KEYBOARD
prompt "Keyboard mapping"
default KEYMAP_WYSE_KB3926
help
Choose the PS/2 Keyboard being used with the interface. This option selects the map defined in
PS2KeyTable.h which maps the PS/2 Keyboard scan codes to the standard PS2_KEY_* definitions used
in the map to MZ-2500/2800 keys.
config KEYMAP_WYSE_KB3926
bool "Wyse KB-3926"
help
The Wyse KB3296 PS/2 keyboard mapping.
config KEYMAP_STANDARD
bool "Standard Definition"
help
A generic PS/2 keyboard mapping.
endchoice
endmenu
menu "MZ-2500/2800 Interface"
menu "4Bit Strobe Input"
config MZ_KDB0
int "KDB0 GPIO pin number"
range 0 46
default 23
help
GPIO number (IOxx) used to connect the MZ-2500/2800 4bit bidirectional data bus Bit 0 with the ESP32. See schematic for actual used value. May change with revisions.
config MZ_KDB1
int "KDB1 GPIO pin number"
range 0 46
default 25
help
GPIO number (IOxx) used to connect the MZ-2500/2800 4bit bidirectional data bus Bit 1 with the ESP32. See schematic for actual used value. May change with revisions.
config MZ_KDB2
int "KDB2 GPIO pin number"
range 0 46
default 26
help
GPIO number (IOxx) used to connect the MZ-2500/2800 4bit bidirectional data bus Bit 2 with the ESP32. See schematic for actual used value. May change with revisions.
config MZ_KDB3
int "KDB1 GPIO pin number"
range 0 46
default 27
help
GPIO number (IOxx) used to connect the MZ-2500/2800 4bit bidirectional data bus Bit 3 with the ESP32. See schematic for actual used value. May change with revisions.
endmenu
menu "8Bit Scan Data Output"
config MZ_KDO0
int "KDO0 GPIO pin number"
range 0 46
default 14
help
GPIO number (IOxx) used to connect the MZ-2500/2800 8bit scan data output Bit 0 to the 74HCT257 IC. See schematic for actual used value. May change with revisions.
config MZ_KDO1
int "KDO1 GPIO pin number"
range 0 46
default 15
help
GPIO number (IOxx) used to connect the MZ-2500/2800 8bit scan data output Bit 1 to the 74HCT257 IC. See schematic for actual used value. May change with revisions.
config MZ_KDO2
int "KDO2 GPIO pin number"
range 0 46
default 16
help
GPIO number (IOxx) used to connect the MZ-2500/2800 8bit scan data output Bit 2 to the 74HCT257 IC. See schematic for actual used value. May change with revisions.
config MZ_KDO3
int "KDO3 GPIO pin number"
range 0 46
default 17
help
GPIO number (IOxx) used to connect the MZ-2500/2800 8bit scan data output Bit 3 to the 74HCT257 IC. See schematic for actual used value. May change with revisions.
config MZ_KDO4
int "KDO4 GPIO pin number"
range 0 46
default 18
help
GPIO number (IOxx) used to connect the MZ-2500/2800 8bit scan data output Bit 4 to the 74HCT257 IC. See schematic for actual used value. May change with revisions.
config MZ_KDO5
int "KDO5 GPIO pin number"
range 0 46
default 19
help
GPIO number (IOxx) used to connect the MZ-2500/2800 8bit scan data output Bit 5 to the 74HCT257 IC. See schematic for actual used value. May change with revisions.
config MZ_KDO6
int "KDO6 GPIO pin number"
range 0 46
default 21
help
GPIO number (IOxx) used to connect the MZ-2500/2800 8bit scan data output Bit 6 to the 74HCT257 IC. See schematic for actual used value. May change with revisions.
config MZ_KDO7
int "KDO7 GPIO pin number"
range 0 46
default 21
help
GPIO number (IOxx) used to connect the MZ-2500/2800 8bit scan data output Bit 7 to the 74HCT257 IC. See schematic for actual used value. May change with revisions.
endmenu
config MZ_RTSNI
int "RTSNi GPIO pin number"
range 0 46
default 35
help
GPIO number (IOxx) used to connect the MZ-2500/2800 RTSN line with the ESP32. See schematic for actual used value. May change with revisions.
config MZ_KDI4
int "KDI4 GPIO pin number"
range 0 46
default 13
help
GPIO number (IOxx) used to connect the MZ-2500/2800 KDI4 line with the ESP32. See schematic for actual used value. May change with revisions.
endmenu
menu "Debug Options"
menu "OLED"
choice INTERFACE
prompt "OLED Interface Type"
default OLED_DISABLED
help
Select Interface.
config OLED_DISABLED
bool "Interface disabled"
help
No OLED present or to be disabled.
config I2C_INTERFACE
bool "I2C Interface"
help
I2C Interface.
config SPI_INTERFACE
bool "SPI Interface"
help
SPI Interface.
endchoice
choice PANEL
prompt "OLED Panel Type"
depends on I2C_INTERFACE || SPI_INTERFACE
default SSD1306_128x64
help
Select Panel Type.
config SSD1306_128x32
bool "128x32 Panel"
help
Panel is 128x32.
config SSD1306_128x64
bool "128x64 Panel"
help
Panel is 128x64.
endchoice
config OFFSETX
int "GRAM X OFFSET"
range 0 99
default 0
help
When your TFT have offset(X), set it.
config FLIP
bool "Flip upside down"
default false
help
Flip upside down.
config SCL_GPIO
depends on I2C_INTERFACE
int "SCL GPIO number"
range 0 46
default 22 if IDF_TARGET_ESP32
default 12 if IDF_TARGET_ESP32S2
default 9 if IDF_TARGET_ESP32C3
default 15 if IDF_TARGET_HELTEC32
help
GPIO number (IOxx) to I2C SCL.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to I2C.
GPIOs 35-39 are input-only so cannot be used as outputs.
config SDA_GPIO
depends on I2C_INTERFACE
int "SDA GPIO number"
range 0 46
default 21 if IDF_TARGET_ESP32
default 11 if IDF_TARGET_ESP32S2
default 10 if IDF_TARGET_ESP32C3
default 4 if IDF_TARGET_HELTEC32
help
GPIO number (IOxx) to I2C SDA.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to I2C.
GPIOs 35-39 are input-only so cannot be used as outputs.
config RESET_GPIO
int "RESET GPIO number"
range -1 46
default -1 if IDF_TARGET_ESP32 || IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32C3
default 16 if IDF_TARGET_HELTEC32
help
GPIO number (IOxx) to RESET.
When it is -1, RESET isn't performed.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to Reset.
GPIOs 35-39 are input-only so cannot be used as outputs.
config MOSI_GPIO
depends on SPI_INTERFACE
int "MOSI GPIO number"
range 0 46
default 23 if IDF_TARGET_ESP32
default 35 if IDF_TARGET_ESP32S2
default 0 if IDF_TARGET_ESP32C3
help
GPIO number (IOxx) to SPI MOSI.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to DC.
On the ESP32, GPIOs 35-39 are input-only so cannot be used as outputs.
On the ESP32-S2, GPIO 46 is input-only so cannot be used as outputs.
config SCLK_GPIO
depends on SPI_INTERFACE
int "SCLK GPIO number"
range 0 46
default 18 if IDF_TARGET_ESP32
default 36 if IDF_TARGET_ESP32S2
default 1 if IDF_TARGET_ESP32C3
help
GPIO number (IOxx) to SPI SCLK.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to DC.
On the ESP32, GPIOs 35-39 are input-only so cannot be used as outputs.
On the ESP32-S2, GPIO 46 is input-only so cannot be used as outputs.
config CS_GPIO
depends on SPI_INTERFACE
int "CS GPIO number"
range 0 34
default 5 if IDF_TARGET_ESP32
default 34 if IDF_TARGET_ESP32S2
default 10 if IDF_TARGET_ESP32C3
help
GPIO number (IOxx) to SPI CS.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to CS.
GPIOs 35-39 are input-only so cannot be used as outputs.
config DC_GPIO
depends on SPI_INTERFACE
int "DC GPIO number"
range 0 34
default 2
help
GPIO number (IOxx) to SPI DC.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to DC.
GPIOs 35-39 are input-only so cannot be used as outputs.
endmenu
config DEBUG_SERIAL
bool "Serial debug output"
default false
help
Enable debug output (non ESP logging) on the serial port.
endmenu
config PWRLED
int "GPIO pin number used for Power On and Status LED"
range 0 46
default 25
help
GPIO number (IOxx) used to control the Power On/Status LED.
Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to I2C.
GPIOs 35-39 are input-only so cannot be used as outputs.
endmenu

189
main/MZKeyTable.h Normal file
View File

@@ -0,0 +1,189 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Name: keytable.h
// Created: Jan 2022
// Version: v1.0
// Author(s): Philip Smart
// Description: The PS/2 Scan Code to MZ-2500/2800 Key Matrix mapping logic.
// This source file contains the definitions and tables to convert a PS/2 scan code
// into an MZ 13x8 matrix equivalent for the received key. The matrix is then read
// out to the MZ-2500/2800 as though it was a real keyboard.
// Credits:
// Copyright: (c) 2019-2022 Philip Smart <philip.smart@net2net.org>
//
// History: Jan 2022 - Initial write.
//
// Notes: See Makefile to enable/disable conditional components
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// This source file is free software: you can redistribute it and#or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This source file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// 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 KEYTABLE_H
#define KEYTABLE_H
// The initial mapping is made inside the PS2KeyAdvanced class from Scan Code Set 2 to ASCII
// for a selected keyboard. Special functions are detected and combined inside this module
// before mapping with the table below to MZ Scan Matrix.
// ie. PS/2 Scan Code -> ASCII + Flags -> MZ Scan Matrix
#include <PS2KeyAdvanced.h>
///////////////////////////
// MZ-2500 Keyboard Map. //
///////////////////////////
//
// Row D7 D6 D5 D4 D3 D2 D1 D0
//----------------------------------------------------------------------------------
// 0 F8 F7 F6 F5 F4 F3 F2 F1
// 1 - + . , 9 8 F1O F9
// 2 7 6 5 4 3 2 1 0
// 3 BREAK RIGHT LEFT DOWN UP RETURN SPACE TAB
// 4 G F E D C B A ?
// 5 O N M L K J I H
// 6 W V U T S R Q P
// 7 <¿ .>¿ ¿¿ ¿ | '¿ Z ¿ Y X ¿
// 8 7' 6& 5% 4$ 3# 2" 1! 0
// 9 [( @ -= ;+ :* 9) 8(
// 10 / * ESC BACKSPACE INST/DEL CLR/HOME COPY ]}
// 11 CTRL ¿¿ SHIFT LOCK GRAPH
// 12 ¿¿ ¿¿¿
// 13 HELP ARGO
#define PSMZTBL_KEYPOS 0
#define PSMZTBL_SHIFTPOS 1
#define PSMZTBL_FUNCPOS 2
#define PSMZTBL_CTRLPOS 3
#define PSMZTBL_ALTPOS 4
#define PSMZTBL_ALTGRPOS 5
#define PSMZTBL_MXROW1 6
#define PSMZTBL_MXKEY1 7
#define PSMZTBL_MXROW2 8
#define PSMZTBL_MXKEY2 9
#define PSMZTBL_MXROW3 10
#define PSMZTBL_MXKEY3 11
#define PSMZTBL_MAXROWS 12
// Lookup table to matrix row/column co-ordinates. If a PS2 key is matched, then the Matrix is updated
// using ROW to point into the array, equivalent of strobe line, and the KEY defines the bits to be set.
// A set bit = 0, reset bits = 1. The KEY value needs to be inverted and masked with the matrix
// to affect change.
const unsigned char PS2toMZ[][PSMZTBL_MAXROWS] =
{
// PS2 Code Shift Function Ctrl ALT ALT-Gr MXROW1 MXKEY1 MXROW2 MXKEY2 MXROW3 MXKEY3
PS2_KEY_F1, 0, 0, 0, 0, 0, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F2, 0, 0, 0, 0, 0, 0x00, 0x02, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F3, 0, 0, 0, 0, 0, 0x00, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F4, 0, 0, 0, 0, 0, 0x00, 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F5, 0, 0, 0, 0, 0, 0x00, 0x10, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F6, 0, 0, 0, 0, 0, 0x00, 0x20, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F7, 0, 0, 0, 0, 0, 0x00, 0x40, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F8, 0, 0, 0, 0, 0, 0x00, 0x80, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F9, 0, 0, 0, 0, 0, 0x01, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F10, 0, 0, 0, 0, 0, 0x01, 0x02, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F11, 0, 0, 0, 0, 0, 0x0D, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, // HELP
PS2_KEY_F12, 0, 0, 0, 0, 0, 0x03, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, // BREAK
PS2_KEY_TAB, 0, 0, 0, 0, 0, 0x03, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_PIPE, 0, 0, 0, 0, 0, 0x07, 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_L_ALT, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_R_ALT, 0, 0, 0, 0, 0, 0x0B, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, // GRAPH
PS2_KEY_L_SHIFT, 0, 0, 0, 0, 0, 0x0B, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_L_CTRL, 0, 0, 0, 0, 0, 0x0B, 0x10, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_R_CTRL, 0, 0, 0, 0, 0, 0x0B, 0x10, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_0, 0, 0, 0, 0, 0, 0x02, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_1, 0, 0, 0, 0, 0, 0x02, 0x02, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_2, 0, 0, 0, 0, 0, 0x02, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_3, 0, 0, 0, 0, 0, 0x02, 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_4, 0, 0, 0, 0, 0, 0x02, 0x10, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_5, 0, 0, 0, 0, 0, 0x02, 0x20, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_6, 0, 0, 0, 0, 0, 0x02, 0x40, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_7, 0, 0, 0, 0, 0, 0x02, 0x80, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_8, 0, 0, 0, 0, 0, 0x01, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_9, 0, 0, 0, 0, 0, 0x01, 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_A, 0, 0, 0, 0, 0, 0x04, 0x02, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_B, 0, 0, 0, 0, 0, 0x04, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_C, 0, 0, 0, 0, 0, 0x04, 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_D, 0, 0, 0, 0, 0, 0x04, 0x10, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_E, 0, 0, 0, 0, 0, 0x04, 0x20, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_F, 0, 0, 0, 0, 0, 0x04, 0x40, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_G, 0, 0, 0, 0, 0, 0x04, 0x80, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_H, 0, 0, 0, 0, 0, 0x05, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_I, 0, 0, 0, 0, 0, 0x05, 0x02, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_J, 0, 0, 0, 0, 0, 0x05, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_K, 0, 0, 0, 0, 0, 0x05, 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_L, 0, 0, 0, 0, 0, 0x05, 0x10, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_M, 0, 0, 0, 0, 0, 0x05, 0x20, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_N, 0, 0, 0, 0, 0, 0x05, 0x40, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_O, 0, 0, 0, 0, 0, 0x05, 0x80, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_P, 0, 0, 0, 0, 0, 0x06, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_Q, 0, 0, 0, 0, 0, 0x06, 0x02, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_R, 0, 0, 0, 0, 0, 0x06, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_S, 0, 0, 0, 0, 0, 0x06, 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_T, 0, 0, 0, 0, 0, 0x06, 0x10, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_U, 0, 0, 0, 0, 0, 0x06, 0x20, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_V, 0, 0, 0, 0, 0, 0x06, 0x40, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_W, 0, 0, 0, 0, 0, 0x06, 0x80, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_X, 0, 0, 0, 0, 0, 0x07, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_Y, 0, 0, 0, 0, 0, 0x07, 0x02, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_Z, 0, 0, 0, 0, 0, 0x07, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_SPACE, 0, 0, 0, 0, 0, 0x03, 0x02, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_COMMA, 0, 0, 0, 0, 0, 0x01, 0x10, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_SEMI, 0, 0, 0, 0, 0, 0x09, 0x08, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_STOP, 0, 0, 0, 0, 0, 0x01, 0x20, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_DIV, 0, 0, 0, 0, 0, 0x0A, 0x80, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_MINUS, 0, 0, 0, 0, 0, 0x01, 0x80, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_APOS, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_OPEN_SQ, 0, 0, 0, 0, 0, 0x09, 0x40, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_EQUAL, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_CAPS, 0, 0, 0, 0, 0, 0x08, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, // LOCK
PS2_KEY_R_SHIFT, 0, 0, 0, 0, 0, 0x0B, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_ENTER, 0, 0, 0, 0, 0, 0x03, 0x04, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_CLOSE_SQ, 0, 0, 0, 0, 0, 0x0A, 0x01, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_BACK, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_LESSTHAN, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_BS, 0, 0, 0, 0, 0, 0x0A, 0x10, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_ESC, 0, 0, 0, 0, 0, 0x0A, 0x20, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP1, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP2, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP3, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP4, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP5, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP6, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP7, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP8, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP9, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP_COMMA, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP_PLUS, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP_MINUS, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP_TIMES, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_KP_DIV, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_SCROLL, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_INSERT, 0, 0, 0, 0, 0, 0x0A, 0x08, 0x0B, 0x04, 0xFF, 0xFF, // SHIFT + INST/DEL
PS2_KEY_HOME, 0, 0, 0, 0, 0, 0x0A, 0x04, 0xFF, 0xFF, 0xFF, 0xFF, // CLR/HOME
PS2_KEY_PGUP, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_DELETE, 0, 0, 0, 0, 0, 0x0A, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, // INST/DEL
PS2_KEY_END, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_PGDN, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_UP_ARROW, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_L_ARROW, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_DN_ARROW, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_R_ARROW, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
PS2_KEY_NUM, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0, 0, 0, 0, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
// PS/2 BREAK key code string
const unsigned char BREAK_CODE[8]={0xE1,0x14,0x77,0xE1,0xF0,0x14,0xF0,0x77};
#endif // KEYTABLE_H

1030
main/PS2KeyAdvanced.cpp Normal file

File diff suppressed because it is too large Load Diff

433
main/PS2KeyAdvanced.h Normal file
View File

@@ -0,0 +1,433 @@
/* Version V1.0.9
PS2KeyAdvanced.h - PS2KeyAdvanced library
Copyright (c) 2007 Free Software Foundation. All right reserved.
Written by Paul Carpenter, PC Services <sales@pcserviceselectronics.co.uk>
Created September 2014
Updated January 2016 - Paul Carpenter - add tested on Due and tidy ups for V1.5 Library Management
January 2020 Fix typos, correct keyboard reset status improve library.properties
and additional platform handling and some documentation
March 2020 Add SAMD1 as recognised support as has been tested by user
Improve different architecture handling
November 2020 Add support for STM32 from user Hiabuto-de
Tested on STM32Duino-Framework and PlatformIO on STM32F103C8T6 and an IBM Model M
July 2021 Add workaround for ESP32 issue with Silicon (hardware) from user submissions
IMPORTANT WARNING
If using a DUE or similar board with 3V3 I/O you MUST put a level translator
like a Texas Instruments TXS0102 or FET circuit as the signals are
Bi-directional (signals transmitted from both ends on same wire).
Failure to do so may damage your Arduino Due or similar board.
Test History
September 2014 Uno and Mega 2560 September 2014 using Arduino V1.6.0
January 2016 Uno, Mega 2560 and Due using Arduino 1.6.7 and Due Board
Manager V1.6.6
ONLY use defines in this file others may disappear on updates.
This is for a LATIN style keyboard using Scan code set 2. See various
websites on what different scan code sets use. Scan Code Set 2 is the
default scan code set for PS2 keyboards on power up.
Will support most keyboards even ones with multimedia keys or even 24 function keys.
Fully featured PS2 keyboard library to provide
All function and movement keys supported even multi-lingual
Parity checking of data sent/received on receive request keyboard resend
Resends data when needed handles keyboard protocol for RESEND and ECHO
Functions for get and set of
Scancode set in use READ only
LED and LOCK control
ReadID
Reset keyboard
Send ECHO
Ignore Break codes for keys
Ignore typematic repeat of CTRL, SHIFT, ALT, Num, Scroll, Caps
Handles NUM, CAPS and SCROLL lock keys to LEDs
Handles NUM/SCROLL internally
Read function Returns an UNSIGNED INT containing
Make/Break status
Caps status
Shift, CTRL, ALT, ALT GR, GUI keys
Flag for function key not a displayable/printable character
8 bit key code
Code Ranges (bottom byte of unsigned int)
0 invalid/error
1-1F Functions (Caps, Shift, ALT, Enter, DEL... )
1A-1F Functions with ASCII control code
(DEL, BS, TAB, ESC, ENTER, SPACE)
20-61 Printable characters noting
0-9 = 0x30 to 0x39 as ASCII
A to Z = 0x41 to 0x5A as upper case ASCII type codes
8B Extra European key
61-A0 Function keys and other special keys (plus F2 and F1)
61-78 F1 to F24
79-8A Multimedia
8B NOT included
8C-8E ACPI power
91-A0 and F2 and F1 - Special multilingual
A8-FF Keyboard communications commands (note F2 and F1 are special
codes for special multi-lingual keyboards)
By using these ranges it is possible to perform detection of any key and do
easy translation to ASCII/UTF-8 avoiding keys that do not have a valid code.
Top Byte is 8 bits denoting as follows with defines for bit code
Define name bit description
PS2_BREAK 15 1 = Break key code
(MSB) 0 = Make Key code
PS2_SHIFT 14 1 = Shift key pressed as well (either side)
0 = NO shift key
PS2_CTRL 13 1 = Ctrl key pressed as well (either side)
0 = NO Ctrl key
PS2_CAPS 12 1 = Caps Lock ON
0 = Caps lock OFF
PS2_ALT 11 1 = Left Alt key pressed as well
0 = NO Left Alt key
PS2_ALT_GR 10 1 = Right Alt (Alt GR) key pressed as well
0 = NO Right Alt key
PS2_GUI 9 1 = GUI key pressed as well (either)
0 = NO GUI key
PS2_FUNCTION 8 1 = FUNCTION key non-printable character (plus space, tab, enter)
0 = standard character key
Error Codes
Most functions return 0 or 0xFFFF as error, other codes to note and
handle appropriately
0xAA keyboard has reset and passed power up tests
will happen if keyboard plugged in after code start
0xFC Keyboard General error or power up fail
It is responsibility of your programme to deal with converting special cases like
<CTRL>+<ENTER> sends a special code to something else. If you wish to do that make a
NEW library called SOMETHING different NOT a variant or revision of this one, as you
are changing base functionality
See PS2KeyCode.h for codes from the keyboard this library uses to decode.
(may disappear in updates do not rely on that file or definitions)
See this file for returned definitions of Keys
Note defines starting
PS2_KC_* are internal defines for codes from the keyboard
PS2_KEY_* are the codes this library returns
PS2_* remaining defines for use in higher levels
To get the key as ASCII/UTF-8 single byte character conversion requires use
of PS2KeyMap library AS WELL.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PS2KeyAdvanced_h
#define PS2KeyAdvanced_h
// Platform specific areas
// Harvard architecture settings for PROGMEM
// Add separate for EACH architecture as easier to maintain
// AVR (includes Teensy 2.0)
#if defined( ARDUINO_ARCH_AVR )
#define PS2_SUPPORTED 1
#define PS2_REQUIRES_PROGMEM 1
#define PS2_CLEAR_PENDING_IRQ 1
#endif
// SAM (Due)
#if defined( ARDUINO_ARCH_SAM )
#define PS2_SUPPORTED 1
#define PS2_CLEAR_PENDING_IRQ 1
#endif
// SAMD1
#if defined( ARDUINO_ARCH_SAMD1 )
#define PS2_SUPPORTED 1
#define PS2_CLEAR_PENDING_IRQ 1
#endif
// STM32
#if defined( ARDUINO_ARCH_STM32 )
#define PS2_SUPPORTED 1
#define PS2_CLEAR_PENDING_IRQ 1
#endif
// ESP32
#if defined( ARDUINO_ARCH_ESP32 )
#define PS2_SUPPORTED 1
#define PS2_ONLY_CHANGE_IRQ 1
#endif
// Invalid architecture
#if !( defined( PS2_SUPPORTED ) )
#warning Library is NOT supported on this board Use at your OWN risk
#endif
/* Flags/bit masks for status bits in returned unsigned int value */
#define PS2_BREAK 0x8000
#define PS2_SHIFT 0x4000
#define PS2_CTRL 0x2000
#define PS2_CAPS 0x1000
#define PS2_ALT 0x800
#define PS2_ALT_GR 0x400
#define PS2_GUI 0x200
#define PS2_FUNCTION 0x100
/* General defines of communications codes */
/* Command or response */
#define PS2_KEY_RESEND 0xFE
#define PS2_KEY_ACK 0xFA
#define PS2_KEY_ECHO 0xEE
/* Responses */
#define PS2_KEY_BAT 0xAA
// Actually buffer overrun
#define PS2_KEY_OVERRUN 0xFF
// Below is general error code
#define PS2_KEY_ERROR 0xFC
/* Command parameters for functions */
/* LED codes OR together */
#define PS2_LOCK_SCROLL 0x01
#define PS2_LOCK_NUM 0x02
#define PS2_LOCK_CAPS 0x04
/* Only useful for very few keyboards */
#define PS2_LOCK_EXTRA 0x08
/* Returned keycode definitions */
/* Do NOT change these codings as you will break base
functionality use PS2KeyMap for that and internationalisation */
#define PS2_KEY_NUM 0x01
#define PS2_KEY_SCROLL 0x02
#define PS2_KEY_CAPS 0x03
#define PS2_KEY_PRTSCR 0x04
#define PS2_KEY_PAUSE 0x05
#define PS2_KEY_L_SHIFT 0x06
#define PS2_KEY_R_SHIFT 0x07
#define PS2_KEY_L_CTRL 0X08
#define PS2_KEY_R_CTRL 0X09
#define PS2_KEY_L_ALT 0x0A
#define PS2_KEY_R_ALT 0x0B
/* Sometimes called windows key */
#define PS2_KEY_L_GUI 0x0C
#define PS2_KEY_R_GUI 0x0D
#define PS2_KEY_MENU 0x0E
/* Break is CTRL + PAUSE generated inside keyboard */
#define PS2_KEY_BREAK 0x0F
/* Generated by some keyboards by ALT and PRTSCR */
#define PS2_KEY_SYSRQ 0x10
#define PS2_KEY_HOME 0x11
#define PS2_KEY_END 0x12
#define PS2_KEY_PGUP 0x13
#define PS2_KEY_PGDN 0x14
#define PS2_KEY_L_ARROW 0x15
#define PS2_KEY_R_ARROW 0x16
#define PS2_KEY_UP_ARROW 0x17
#define PS2_KEY_DN_ARROW 0x18
#define PS2_KEY_INSERT 0x19
#define PS2_KEY_DELETE 0x1A
#define PS2_KEY_ESC 0x1B
#define PS2_KEY_BS 0x08
#define PS2_KEY_TAB 0x09
#define PS2_KEY_ENTER 0x0D
#define PS2_KEY_SPACE 0x20
#define PS2_KEY_KP0 0xA0
#define PS2_KEY_KP1 0xA1
#define PS2_KEY_KP2 0xA2
#define PS2_KEY_KP3 0xA3
#define PS2_KEY_KP4 0xA4
#define PS2_KEY_KP5 0xA5
#define PS2_KEY_KP6 0xA6
#define PS2_KEY_KP7 0xA7
#define PS2_KEY_KP8 0xA8
#define PS2_KEY_KP9 0xA9
#define PS2_KEY_KP_DOT 0xAA
#define PS2_KEY_KP_ENTER 0xAB
#define PS2_KEY_KP_PLUS 0xAC
#define PS2_KEY_KP_MINUS 0xAD
#define PS2_KEY_KP_TIMES 0xAE
#define PS2_KEY_KP_DIV 0xAF
#define PS2_KEY_0 0X30
#define PS2_KEY_1 0X31
#define PS2_KEY_2 0X32
#define PS2_KEY_3 0X33
#define PS2_KEY_4 0X34
#define PS2_KEY_5 0X35
#define PS2_KEY_6 0X36
#define PS2_KEY_7 0X37
#define PS2_KEY_8 0X38
#define PS2_KEY_9 0X39
#define PS2_KEY_APOS 0X27
#define PS2_KEY_COMMA 0X2C
#define PS2_KEY_MINUS 0X2D
#define PS2_KEY_DOT 0X2E
#define PS2_KEY_DIV 0X2F
#define PS2_KEY_AT 0X40
#define PS2_KEY_A 0X41
#define PS2_KEY_B 0X42
#define PS2_KEY_C 0X43
#define PS2_KEY_D 0X44
#define PS2_KEY_E 0X45
#define PS2_KEY_F 0X46
#define PS2_KEY_G 0X47
#define PS2_KEY_H 0X48
#define PS2_KEY_I 0X49
#define PS2_KEY_J 0X4A
#define PS2_KEY_K 0X4B
#define PS2_KEY_L 0X4C
#define PS2_KEY_M 0X4D
#define PS2_KEY_N 0X4E
#define PS2_KEY_O 0X4F
#define PS2_KEY_P 0X50
#define PS2_KEY_Q 0X51
#define PS2_KEY_R 0X52
#define PS2_KEY_S 0X53
#define PS2_KEY_T 0X54
#define PS2_KEY_U 0X55
#define PS2_KEY_V 0X56
#define PS2_KEY_W 0X57
#define PS2_KEY_X 0X58
#define PS2_KEY_Y 0X59
#define PS2_KEY_Z 0X5A
#define PS2_KEY_SEMI 0X3B
#define PS2_KEY_BACK 0X5C
#define PS2_KEY_OPEN_SQ 0X5B
#define PS2_KEY_CLOSE_SQ 0X5D
#define PS2_KEY_EQUAL 0X3D
#define PS2_KEY_HASH 0x23
#define PS2_KEY_PIPE 0x7C
#define PS2_KEY_LESSTHAN 0x3C
#define PS2_KEY_BTICK 0x60
/* Some Numeric keypads have a comma key */
#define PS2_KEY_KP_COMMA 0xB0
#define PS2_KEY_F1 0XB1
#define PS2_KEY_F2 0XB2
#define PS2_KEY_F3 0XB3
#define PS2_KEY_F4 0XB4
#define PS2_KEY_F5 0XB5
#define PS2_KEY_F6 0XB6
#define PS2_KEY_F7 0XB7
#define PS2_KEY_F8 0XB8
#define PS2_KEY_F9 0XB9
#define PS2_KEY_F10 0XBA
#define PS2_KEY_F11 0XBB
#define PS2_KEY_F12 0XBC
#define PS2_KEY_NEXT_TR 0XBD
#define PS2_KEY_PREV_TR 0XBE
#define PS2_KEY_STOP 0XBF
#define PS2_KEY_PLAY 0XC0
#define PS2_KEY_MUTE 0XC1
#define PS2_KEY_VOL_UP 0XC2
#define PS2_KEY_VOL_DN 0XC3
#define PS2_KEY_MEDIA 0XC4
#define PS2_KEY_EMAIL 0XC5
#define PS2_KEY_CALC 0XC6
#define PS2_KEY_COMPUTER 0XC7
#define PS2_KEY_WEB_SEARCH 0XC8
#define PS2_KEY_WEB_HOME 0XC9
#define PS2_KEY_WEB_BACK 0XCA
#define PS2_KEY_WEB_FORWARD 0XCB
#define PS2_KEY_WEB_STOP 0XCC
#define PS2_KEY_WEB_REFRESH 0XCD
#define PS2_KEY_WEB_FAVOR 0XCE
#define PS2_KEY_EUROPE2 0XCF
#define PS2_KEY_POWER 0XD0
#define PS2_KEY_SLEEP 0XD1
#define PS2_KEY_WAKE 0XD2
#define PS2_KEY_INTL1 0XD3
#define PS2_KEY_INTL2 0XD4
#define PS2_KEY_INTL3 0XD5
#define PS2_KEY_INTL4 0XD6
#define PS2_KEY_INTL5 0XD7
#define PS2_KEY_LANG1 0XD8
#define PS2_KEY_LANG2 0XD9
#define PS2_KEY_LANG3 0XDA
#define PS2_KEY_LANG4 0XDB
#define PS2_KEY_LANG5 0xDC
/* Some Numeric keyboards have an '=' on right keypad */
#define PS2_KEY_KP_EQUAL 0xDD
/*
Purpose: Provides advanced access to PS2 keyboards
Public class definitions
See standard error codes for error code returns
*/
class PS2KeyAdvanced {
public:
/* This constructor does basically nothing. Please call the begin(int,int)
method before using any other method of this class. */
PS2KeyAdvanced( );
/* Starts the keyboard "service" by registering the external interrupt.
setting the pin modes correctly and driving those needed to high.
Sets default LOCK status (LEDs) to passed in value or default of all off
The best place to call this method is in the setup routine. */
void begin( uint8_t, uint8_t );
uint8_t keyAvailable();
/* Returns number of codes available or 0 for none */
uint8_t available( );
/* Returns the key last read from the keyboard.
If there is no key available, 0 is returned. */
uint16_t read( );
/* Returns the current status of Locks
Use Macro to mask out bits from
PS2_LOCK_NUM PS2_LOCK_CAPS PS2_LOCK_SCROLL */
uint8_t getLock( );
/* Sets the current status of Locks and LEDs
Use macro defines added together from
PS2_LOCK_NUM PS2_LOCK_CAPS PS2_LOCK_SCROLL */
void setLock( uint8_t );
/* Set library to not send break key codes
1 = no break codes
0 = send break codes */
void setNoBreak( uint8_t );
/* Set library to not repeat make codes for CTRL, ALT, GUI, SHIFT
1 = no repeat codes
0 = send repeat codes */
void setNoRepeat( uint8_t );
/* Resets keyboard when reset has completed
keyboard sends AA - Pass or FC for fail
Read from keyboard data buffer */
void resetKey( );
/* Get the current Scancode Set used in keyboard
returned data in keyboard buffer read as keys */
void getScanCodeSet( void );
/* Get the current Scancode Set used in keyboard
returned data in keyboard buffer read as keys */
void readID( void );
/* Send Echo command to keyboard
returned data in keyboard buffer read as keys */
void echo( void );
/* Send Typematic rate/delay command to keyboard
First Parameter rate is 0 - 0x1F (31)
0 = 30 CPS
0x1F = 2 CPS
default in keyboard is 0xB (10.9 CPS)
Second Parameter delay is 0 - 3 for 0.25s to 1s in 0.25 increments
default in keyboard is 1 = 0.5 second delay
Returned data in keyboard buffer read as keys */
int typematic( uint8_t , uint8_t );
};
#endif

276
main/PS2KeyCode.h Normal file
View File

@@ -0,0 +1,276 @@
/* Version V1.0.8
PS2KeyCode.h - PS2KeyAdvanced library Internal actual PS2 key code sequences
Copyright (c) 2007 Free Software Foundation. All right reserved.
Written by Paul Carpenter, PC Services <sales@pcserviceselectronics.co.uk>
Created September 2014
Updated January 2016 - Paul Carpenter - add tested on Due and tidy ups for V1.5 Library Management
Updated December 2019 - Paul Carpenter - Fix typo in code for Multimedia STOP
PRIVATE to library
Test History
September 2014 Uno and Mega 2560 September 2014 using Arduino V1.6.0
January 2016 Uno, Mega 2560 and Due using Arduino 1.6.7 and Due Board
Manager V1.6.6
This is for a LATIN style keyboard. Will support most keyboards even ones
with multimedia keys or even 24 function keys.
Definitions used for key codes from a PS2 keyboard, do not use in your
code these are to be handled INTERNALLY by the library.
(may disappear in updates do not rely on this file or definitions)
See PS2KeyAdvanced.h for codes returned from library and flag settings
Defines are in three groups
Special codes definition of communications bytes
Single Byte codes returned as key codes
Two byte Codes preceded by E0 code returned as keycodes
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PS2KeyCode_h
#define PS2KeyCode_h
/* Ignore code for key code translation */
#define PS2_KEY_IGNORE 0xBB
// buffer sizes keyboard RX and TX, then key reading buffer
// Minimum size 8 can be larger
#define _RX_BUFFER_SIZE 8
// Minimum size 6 can be larger
#define _TX_BUFFER_SIZE 6
// Output Buffer of unsigned int values. Minimum size 4 can be larger
#define _KEY_BUFF_SIZE 4
/* private defines for library files not global */
/* _ps2mode status flags */
#define _PS2_BUSY 0x80
#define _TX_MODE 0x40
#define _BREAK_KEY 0x20
#define _WAIT_RESPONSE 0x10
#define _E0_MODE 0x08
#define _E1_MODE 0x04
#define _LAST_VALID 0x02
/* _tx_ready flags */
#define _HANDSHAKE 0x80
#define _COMMAND 0x01
/* Key Repeat defines */
#define _NO_BREAKS 0x08
#define _NO_REPEATS 0x80
/* PS2_keystatus byte masks (from 16 bit int masks) */
#define _BREAK ( PS2_BREAK >> 8 )
#define _SHIFT ( PS2_SHIFT >> 8 )
#define _CTRL ( PS2_CTRL >> 8 )
#define _CAPS ( PS2_CAPS >> 8 )
#define _ALT ( PS2_ALT >> 8 )
#define _ALT_GR ( PS2_ALT_GR >> 8 )
#define _GUI ( PS2_GUI >> 8 )
#define _FUNCTION ( PS2_FUNCTION >> 8 )
/* General defines of comms codes */
/* Command or response */
#define PS2_KC_RESEND 0xFE
#define PS2_KC_ACK 0xFA
#define PS2_KC_ECHO 0xEE
/* Responses */
#define PS2_KC_BAT 0xAA
// Actually buffer overrun
#define PS2_KC_OVERRUN 0xFF
// Below is general error code
#define PS2_KC_ERROR 0xFC
#define PS2_KC_KEYBREAK 0xF0
#define PS2_KC_EXTEND1 0xE1
#define PS2_KC_EXTEND 0xE0
/* Commands */
#define PS2_KC_RESET 0xFF
#define PS2_KC_DEFAULTS 0xF6
#define PS2_KC_DISABLE 0xF5
#define PS2_KC_ENABLE 0xF4
#define PS2_KC_RATE 0xF3
#define PS2_KC_READID 0xF2
#define PS2_KC_SCANCODE 0xF0
#define PS2_KC_LOCK 0xED
/* Single Byte Key Codes */
#define PS2_KC_NUM 0x77
#define PS2_KC_SCROLL 0x7E
#define PS2_KC_CAPS 0x58
#define PS2_KC_L_SHIFT 0x12
#define PS2_KC_R_SHIFT 0x59
/* This is Left CTRL and ALT but Right version is in E0 with same code */
#define PS2_KC_CTRL 0X14
#define PS2_KC_ALT 0x11
/* Generated by some keyboards by ALT and PRTSCR */
#define PS2_KC_SYSRQ 0x84
#define PS2_KC_ESC 0x76
#define PS2_KC_BS 0x66
#define PS2_KC_TAB 0x0D
#define PS2_KC_ENTER 0x5A
#define PS2_KC_SPACE 0x29
#define PS2_KC_KP0 0x70
#define PS2_KC_KP1 0x69
#define PS2_KC_KP2 0x72
#define PS2_KC_KP3 0x7A
#define PS2_KC_KP4 0x6B
#define PS2_KC_KP5 0x73
#define PS2_KC_KP6 0x74
#define PS2_KC_KP7 0x6C
#define PS2_KC_KP8 0x75
#define PS2_KC_KP9 0x7D
#define PS2_KC_KP_DOT 0x71
#define PS2_KC_KP_PLUS 0x79
#define PS2_KC_KP_MINUS 0x7B
#define PS2_KC_KP_TIMES 0x7C
/* Some keyboards have an '=' on right keypad */
#define PS2_KC_KP_EQUAL 0x0F
#define PS2_KC_0 0X45
#define PS2_KC_1 0X16
#define PS2_KC_2 0X1E
#define PS2_KC_3 0X26
#define PS2_KC_4 0X25
#define PS2_KC_5 0X2E
#define PS2_KC_6 0X36
#define PS2_KC_7 0X3D
#define PS2_KC_8 0X3E
#define PS2_KC_9 0X46
#define PS2_KC_APOS 0X52
#define PS2_KC_COMMA 0X41
#define PS2_KC_MINUS 0X4E
#define PS2_KC_DOT 0X49
#define PS2_KC_DIV 0X4A
/* Single quote or back apostrophe */
#define PS2_KC_BTICK 0X0E
#define PS2_KC_A 0X1C
#define PS2_KC_B 0X32
#define PS2_KC_C 0X21
#define PS2_KC_D 0X23
#define PS2_KC_E 0X24
#define PS2_KC_F 0X2B
#define PS2_KC_G 0X34
#define PS2_KC_H 0X33
#define PS2_KC_I 0X43
#define PS2_KC_J 0X3B
#define PS2_KC_K 0X42
#define PS2_KC_L 0X4B
#define PS2_KC_M 0X3A
#define PS2_KC_N 0X31
#define PS2_KC_O 0X44
#define PS2_KC_P 0X4D
#define PS2_KC_Q 0X15
#define PS2_KC_R 0X2D
#define PS2_KC_S 0X1B
#define PS2_KC_T 0X2C
#define PS2_KC_U 0X3C
#define PS2_KC_V 0X2A
#define PS2_KC_W 0X1D
#define PS2_KC_X 0X22
#define PS2_KC_Y 0X35
#define PS2_KC_Z 0X1A
#define PS2_KC_SEMI 0X4C
#define PS2_KC_BACK 0X5D
// Extra key left of Z on 102 keyboards
#define PS2_KC_EUROPE2 0x61
#define PS2_KC_OPEN_SQ 0X54
#define PS2_KC_CLOSE_SQ 0X5B
#define PS2_KC_EQUAL 0X55
#define PS2_KC_F1 0X05
#define PS2_KC_F2 0X06
#define PS2_KC_F3 0X04
#define PS2_KC_F4 0X0C
#define PS2_KC_F5 0X03
#define PS2_KC_F6 0X0B
#define PS2_KC_F7 0X83
#define PS2_KC_F8 0X0A
#define PS2_KC_F9 0X01
#define PS2_KC_F10 0X09
#define PS2_KC_F11 0X78
#define PS2_KC_F12 0X07
#define PS2_KC_F13 0X08
#define PS2_KC_F14 0X10
#define PS2_KC_F15 0X18
#define PS2_KC_F16 0X20
#define PS2_KC_F17 0X28
#define PS2_KC_F18 0X30
#define PS2_KC_F19 0X38
#define PS2_KC_F20 0X40
#define PS2_KC_F21 0X48
#define PS2_KC_F22 0X50
#define PS2_KC_F23 0X57
#define PS2_KC_F24 0X5F
#define PS2_KC_KP_COMMA 0X6D
#define PS2_KC_INTL1 0X51
#define PS2_KC_INTL2 0X13
#define PS2_KC_INTL3 0X6A
#define PS2_KC_INTL4 0X64
#define PS2_KC_INTL5 0X67
#define PS2_KC_LANG1 0XF2
#define PS2_KC_LANG2 0XF1
#define PS2_KC_LANG3 0X63
#define PS2_KC_LANG4 0X62
#define PS2_KC_LANG5 0X5F
/* Extended key codes E0 table for two byte codes */
/* PS2_CTRL and PS2_ALT Need using in any table for the right keys */
/* first is special case for PRTSCR not always used so ignored by decoding */
#define PS2_KC_IGNORE 0x12
#define PS2_KC_PRTSCR 0x7C
/* Sometimes called windows key */
#define PS2_KC_L_GUI 0x1F
#define PS2_KC_R_GUI 0x27
#define PS2_KC_MENU 0x2F
/* Break is CTRL + PAUSE generated inside keyboard */
#define PS2_KC_BREAK 0x7E
#define PS2_KC_HOME 0x6C
#define PS2_KC_END 0x69
#define PS2_KC_PGUP 0x7D
#define PS2_KC_PGDN 0x7A
#define PS2_KC_L_ARROW 0x6B
#define PS2_KC_R_ARROW 0x74
#define PS2_KC_UP_ARROW 0x75
#define PS2_KC_DN_ARROW 0x72
#define PS2_KC_INSERT 0x70
#define PS2_KC_DELETE 0x71
#define PS2_KC_KP_ENTER 0x5A
#define PS2_KC_KP_DIV 0x4A
#define PS2_KC_NEXT_TR 0X4D
#define PS2_KC_PREV_TR 0X15
#define PS2_KC_STOP 0X3B
#define PS2_KC_PLAY 0X34
#define PS2_KC_MUTE 0X23
#define PS2_KC_VOL_UP 0X32
#define PS2_KC_VOL_DN 0X21
#define PS2_KC_MEDIA 0X50
#define PS2_KC_EMAIL 0X48
#define PS2_KC_CALC 0X2B
#define PS2_KC_COMPUTER 0X40
#define PS2_KC_WEB_SEARCH 0X10
#define PS2_KC_WEB_HOME 0X3A
#define PS2_KC_WEB_BACK 0X38
#define PS2_KC_WEB_FORWARD 0X30
#define PS2_KC_WEB_STOP 0X28
#define PS2_KC_WEB_REFRESH 0X20
#define PS2_KC_WEB_FAVOR 0X18
#define PS2_KC_POWER 0X37
#define PS2_KC_SLEEP 0X3F
#define PS2_KC_WAKE 0X5E
#endif

397
main/PS2KeyTable.h Normal file
View File

@@ -0,0 +1,397 @@
/* Version V1.0.8
PS2KeyTable.h - PS2KeyAdvanced library keycode values to return values
Copyright (c) 2007 Free Software Foundation. All right reserved.
Written by Paul Carpenter, PC Services <sales@pcserviceselectronics.co.uk>
Created September 2014
V1.0.2 Updated January 2016 - Paul Carpenter - add tested on Due and tidy ups for V1.5 Library Management
PRIVATE to library
Test History
September 2014 Uno and Mega 2560 September 2014 using Arduino V1.6.0
January 2016 Uno, Mega 2560 and Due using Arduino 1.6.7 and Due Board
Manager V1.6.6
Internal to library private tables
(may disappear in updates do not rely on this file or definitions)
This is for a LATIN style keyboard. Will support most keyboards even ones
with multimedia keys or even 24 function keys.
Definitions used for key codes from a PS2 keyboard, do not use in your
code these are handled by the library.
See PS2KeyAdvanced.h for codes returned from library and flag settings
Two sets of tables
Single Byte codes returned as key codes
Two byte Codes preceded by E0 code returned as keycodes
Same tables used for make and break decode
Special cases are -
PRTSCR that ignores one of the sequences (E0,12) as this is not always sent
especially with modifier keys or some keyboards when typematic repeat comes on.
PAUSE as this is an 8 byte sequence only one starting E1 so main code gets E1
and waits for 7 more valid bytes to make the coding.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PS2KeyTable_h
#define PS2KeyTable_h
/* Table contents are pairs of numbers
first code from keyboard
second is either PS2_KEY_IGNOPRE code or key code to return
Single byte Key table
In codes can only be 1 - 0x9F, plus 0xF2 and 0xF1
Out Codes in range 1 to 0x9F
*/
#if defined(CONFIG_KEYMAP_WYSE_KB3926)
const uint8_t single_key[][ 2 ] = {
{ PS2_KC_NUM, PS2_KEY_NUM },
{ PS2_KC_SCROLL, PS2_KEY_SCROLL },
{ PS2_KC_CAPS, PS2_KEY_CAPS },
{ PS2_KC_L_SHIFT, PS2_KEY_L_SHIFT },
{ PS2_KC_R_SHIFT, PS2_KEY_R_SHIFT },
{ PS2_KC_CTRL, PS2_KEY_L_CTRL },
{ PS2_KC_ALT, PS2_KEY_L_ALT },
{ PS2_KC_SYSRQ, PS2_KEY_SYSRQ },
{ PS2_KC_ESC, PS2_KEY_ESC },
{ PS2_KC_BS, PS2_KEY_BS },
{ PS2_KC_TAB, PS2_KEY_TAB },
{ PS2_KC_ENTER, PS2_KEY_ENTER },
{ PS2_KC_SPACE, PS2_KEY_SPACE },
{ PS2_KC_KP0, PS2_KEY_KP0 },
{ PS2_KC_KP1, PS2_KEY_KP1 },
{ PS2_KC_KP2, PS2_KEY_KP2 },
{ PS2_KC_KP3, PS2_KEY_KP3 },
{ PS2_KC_KP4, PS2_KEY_KP4 },
{ PS2_KC_KP5, PS2_KEY_KP5 },
{ PS2_KC_KP6, PS2_KEY_KP6 },
{ PS2_KC_KP7, PS2_KEY_KP7 },
{ PS2_KC_KP8, PS2_KEY_KP8 },
{ PS2_KC_KP9, PS2_KEY_KP9 },
{ PS2_KC_KP_DOT, PS2_KEY_KP_DOT },
{ PS2_KC_KP_PLUS, PS2_KEY_KP_PLUS },
{ PS2_KC_KP_MINUS, PS2_KEY_KP_MINUS },
{ PS2_KC_KP_TIMES, PS2_KEY_KP_TIMES },
{ PS2_KC_KP_EQUAL, PS2_KEY_KP_EQUAL },
{ PS2_KC_0, PS2_KEY_0 },
{ PS2_KC_1, PS2_KEY_1 },
{ PS2_KC_2, PS2_KEY_2 },
{ PS2_KC_3, PS2_KEY_3 },
{ PS2_KC_4, PS2_KEY_4 },
{ PS2_KC_5, PS2_KEY_5 },
{ PS2_KC_6, PS2_KEY_6 },
{ PS2_KC_7, PS2_KEY_7 },
{ PS2_KC_8, PS2_KEY_8 },
{ PS2_KC_9, PS2_KEY_9 },
{ PS2_KC_APOS, PS2_KEY_APOS },
{ PS2_KC_COMMA, PS2_KEY_COMMA },
{ PS2_KC_MINUS, PS2_KEY_MINUS },
{ PS2_KC_DOT, PS2_KEY_DOT },
{ PS2_KC_DIV, PS2_KEY_DIV },
{ PS2_KC_BTICK, PS2_KEY_BTICK },
{ PS2_KC_A, PS2_KEY_A },
{ PS2_KC_B, PS2_KEY_B },
{ PS2_KC_C, PS2_KEY_C },
{ PS2_KC_D, PS2_KEY_D },
{ PS2_KC_E, PS2_KEY_E },
{ PS2_KC_F, PS2_KEY_F },
{ PS2_KC_G, PS2_KEY_G },
{ PS2_KC_H, PS2_KEY_H },
{ PS2_KC_I, PS2_KEY_I },
{ PS2_KC_J, PS2_KEY_J },
{ PS2_KC_K, PS2_KEY_K },
{ PS2_KC_L, PS2_KEY_L },
{ PS2_KC_M, PS2_KEY_M },
{ PS2_KC_N, PS2_KEY_N },
{ PS2_KC_O, PS2_KEY_O },
{ PS2_KC_P, PS2_KEY_P },
{ PS2_KC_Q, PS2_KEY_Q },
{ PS2_KC_R, PS2_KEY_R },
{ PS2_KC_S, PS2_KEY_S },
{ PS2_KC_T, PS2_KEY_T },
{ PS2_KC_U, PS2_KEY_U },
{ PS2_KC_V, PS2_KEY_V },
{ PS2_KC_W, PS2_KEY_W },
{ PS2_KC_X, PS2_KEY_X },
{ PS2_KC_Y, PS2_KEY_Y },
{ PS2_KC_Z, PS2_KEY_Z },
{ PS2_KC_SEMI, PS2_KEY_SEMI },
{ PS2_KC_BACK, PS2_KEY_HASH },
{ PS2_KC_OPEN_SQ, PS2_KEY_OPEN_SQ },
{ PS2_KC_CLOSE_SQ, PS2_KEY_CLOSE_SQ },
{ PS2_KC_EQUAL, PS2_KEY_EQUAL },
{ PS2_KC_EUROPE2, PS2_KEY_BACK },
{ PS2_KC_F1, PS2_KEY_F1 },
{ PS2_KC_F2, PS2_KEY_F2 },
{ PS2_KC_F3, PS2_KEY_F3 },
{ PS2_KC_F4, PS2_KEY_F4 },
{ PS2_KC_F5, PS2_KEY_F5 },
{ PS2_KC_F6, PS2_KEY_F6 },
{ PS2_KC_F7, PS2_KEY_F7 },
{ PS2_KC_F8, PS2_KEY_F8 },
{ PS2_KC_F9, PS2_KEY_F9 },
{ PS2_KC_F10, PS2_KEY_F10 },
{ PS2_KC_F11, PS2_KEY_F11 },
{ PS2_KC_F12, PS2_KEY_F12 },
{ PS2_KC_KP_COMMA, PS2_KEY_KP_COMMA },
{ PS2_KC_INTL1, PS2_KEY_INTL1 },
{ PS2_KC_INTL2, PS2_KEY_INTL2 },
{ PS2_KC_INTL3, PS2_KEY_INTL3 },
{ PS2_KC_INTL4, PS2_KEY_INTL4 },
{ PS2_KC_INTL5, PS2_KEY_INTL5 },
{ PS2_KC_LANG1, PS2_KEY_LANG1 },
{ PS2_KC_LANG2, PS2_KEY_LANG2 },
{ PS2_KC_LANG3, PS2_KEY_LANG3 },
{ PS2_KC_LANG4, PS2_KEY_LANG4 },
{ PS2_KC_LANG5, PS2_KEY_LANG5 }
};
/* Two byte Key table after an E0 byte received */
const uint8_t extended_key[][ 2 ] = {
{ PS2_KC_IGNORE, PS2_KEY_IGNORE },
{ PS2_KC_PRTSCR, PS2_KEY_PRTSCR },
{ PS2_KC_CTRL, PS2_KEY_R_CTRL },
{ PS2_KC_ALT, PS2_KEY_R_ALT },
{ PS2_KC_L_GUI, PS2_KEY_L_GUI },
{ PS2_KC_R_GUI, PS2_KEY_R_GUI },
{ PS2_KC_MENU, PS2_KEY_MENU },
{ PS2_KC_BREAK, PS2_KEY_BREAK },
{ PS2_KC_HOME, PS2_KEY_HOME },
{ PS2_KC_END, PS2_KEY_END },
{ PS2_KC_PGUP, PS2_KEY_PGUP },
{ PS2_KC_PGDN, PS2_KEY_PGDN },
{ PS2_KC_L_ARROW, PS2_KEY_L_ARROW },
{ PS2_KC_R_ARROW, PS2_KEY_R_ARROW },
{ PS2_KC_UP_ARROW, PS2_KEY_UP_ARROW },
{ PS2_KC_DN_ARROW, PS2_KEY_DN_ARROW },
{ PS2_KC_INSERT, PS2_KEY_INSERT },
{ PS2_KC_DELETE, PS2_KEY_DELETE },
{ PS2_KC_KP_ENTER, PS2_KEY_KP_ENTER },
{ PS2_KC_KP_DIV, PS2_KEY_KP_DIV },
{ PS2_KC_NEXT_TR, PS2_KEY_NEXT_TR },
{ PS2_KC_PREV_TR, PS2_KEY_PREV_TR },
{ PS2_KC_STOP, PS2_KEY_STOP },
{ PS2_KC_PLAY, PS2_KEY_PLAY },
{ PS2_KC_MUTE, PS2_KEY_MUTE },
{ PS2_KC_VOL_UP, PS2_KEY_VOL_UP },
{ PS2_KC_VOL_DN, PS2_KEY_VOL_DN },
{ PS2_KC_MEDIA, PS2_KEY_MEDIA },
{ PS2_KC_EMAIL, PS2_KEY_EMAIL },
{ PS2_KC_CALC, PS2_KEY_CALC },
{ PS2_KC_COMPUTER, PS2_KEY_COMPUTER },
{ PS2_KC_WEB_SEARCH, PS2_KEY_WEB_SEARCH },
{ PS2_KC_WEB_HOME, PS2_KEY_WEB_HOME },
{ PS2_KC_WEB_BACK, PS2_KEY_WEB_BACK },
{ PS2_KC_WEB_FORWARD, PS2_KEY_WEB_FORWARD },
{ PS2_KC_WEB_STOP, PS2_KEY_WEB_STOP },
{ PS2_KC_WEB_REFRESH, PS2_KEY_WEB_REFRESH },
{ PS2_KC_WEB_FAVOR, PS2_KEY_WEB_FAVOR },
{ PS2_KC_POWER, PS2_KEY_POWER },
{ PS2_KC_SLEEP, PS2_KEY_SLEEP },
{ PS2_KC_WAKE, PS2_KEY_WAKE }
};
/* Scroll lock numeric keypad re-mappings for NOT NUMLOCK */
/* in translated code order order is important */
const uint8_t scroll_remap[] = {
PS2_KEY_INSERT, // PS2_KEY_KP0
PS2_KEY_END, // PS2_KEY_KP1
PS2_KEY_DN_ARROW, // PS2_KEY_KP2
PS2_KEY_PGDN, // PS2_KEY_KP3
PS2_KEY_L_ARROW, // PS2_KEY_KP4
PS2_KEY_IGNORE, // PS2_KEY_KP5
PS2_KEY_R_ARROW, // PS2_KEY_KP6
PS2_KEY_HOME, // PS2_KEY_KP7
PS2_KEY_UP_ARROW, // PS2_KEY_KP8
PS2_KEY_PGUP, // PS2_KEY_KP9
PS2_KEY_DELETE // PS2_KEY_KP_DOT
};
#endif
#if defined(CONFIG_KEYMAP_STANDARD)
const uint8_t single_key[][ 2 ] = {
{ PS2_KC_NUM, PS2_KEY_NUM },
{ PS2_KC_SCROLL, PS2_KEY_SCROLL },
{ PS2_KC_CAPS, PS2_KEY_CAPS },
{ PS2_KC_L_SHIFT, PS2_KEY_L_SHIFT },
{ PS2_KC_R_SHIFT, PS2_KEY_R_SHIFT },
{ PS2_KC_CTRL, PS2_KEY_L_CTRL },
{ PS2_KC_ALT, PS2_KEY_L_ALT },
{ PS2_KC_SYSRQ, PS2_KEY_SYSRQ },
{ PS2_KC_ESC, PS2_KEY_ESC },
{ PS2_KC_BS, PS2_KEY_BS },
{ PS2_KC_TAB, PS2_KEY_TAB },
{ PS2_KC_ENTER, PS2_KEY_ENTER },
{ PS2_KC_SPACE, PS2_KEY_SPACE },
{ PS2_KC_KP0, PS2_KEY_KP0 },
{ PS2_KC_KP1, PS2_KEY_KP1 },
{ PS2_KC_KP2, PS2_KEY_KP2 },
{ PS2_KC_KP3, PS2_KEY_KP3 },
{ PS2_KC_KP4, PS2_KEY_KP4 },
{ PS2_KC_KP5, PS2_KEY_KP5 },
{ PS2_KC_KP6, PS2_KEY_KP6 },
{ PS2_KC_KP7, PS2_KEY_KP7 },
{ PS2_KC_KP8, PS2_KEY_KP8 },
{ PS2_KC_KP9, PS2_KEY_KP9 },
{ PS2_KC_KP_DOT, PS2_KEY_KP_DOT },
{ PS2_KC_KP_PLUS, PS2_KEY_KP_PLUS },
{ PS2_KC_KP_MINUS, PS2_KEY_KP_MINUS },
{ PS2_KC_KP_TIMES, PS2_KEY_KP_TIMES },
{ PS2_KC_KP_EQUAL, PS2_KEY_KP_EQUAL },
{ PS2_KC_0, PS2_KEY_0 },
{ PS2_KC_1, PS2_KEY_1 },
{ PS2_KC_2, PS2_KEY_2 },
{ PS2_KC_3, PS2_KEY_3 },
{ PS2_KC_4, PS2_KEY_4 },
{ PS2_KC_5, PS2_KEY_5 },
{ PS2_KC_6, PS2_KEY_6 },
{ PS2_KC_7, PS2_KEY_7 },
{ PS2_KC_8, PS2_KEY_8 },
{ PS2_KC_9, PS2_KEY_9 },
{ PS2_KC_APOS, PS2_KEY_APOS },
{ PS2_KC_COMMA, PS2_KEY_COMMA },
{ PS2_KC_MINUS, PS2_KEY_MINUS },
{ PS2_KC_DOT, PS2_KEY_DOT },
{ PS2_KC_DIV, PS2_KEY_DIV },
{ PS2_KC_BTICK, PS2_KEY_BTICK },
{ PS2_KC_A, PS2_KEY_A },
{ PS2_KC_B, PS2_KEY_B },
{ PS2_KC_C, PS2_KEY_C },
{ PS2_KC_D, PS2_KEY_D },
{ PS2_KC_E, PS2_KEY_E },
{ PS2_KC_F, PS2_KEY_F },
{ PS2_KC_G, PS2_KEY_G },
{ PS2_KC_H, PS2_KEY_H },
{ PS2_KC_I, PS2_KEY_I },
{ PS2_KC_J, PS2_KEY_J },
{ PS2_KC_K, PS2_KEY_K },
{ PS2_KC_L, PS2_KEY_L },
{ PS2_KC_M, PS2_KEY_M },
{ PS2_KC_N, PS2_KEY_N },
{ PS2_KC_O, PS2_KEY_O },
{ PS2_KC_P, PS2_KEY_P },
{ PS2_KC_Q, PS2_KEY_Q },
{ PS2_KC_R, PS2_KEY_R },
{ PS2_KC_S, PS2_KEY_S },
{ PS2_KC_T, PS2_KEY_T },
{ PS2_KC_U, PS2_KEY_U },
{ PS2_KC_V, PS2_KEY_V },
{ PS2_KC_W, PS2_KEY_W },
{ PS2_KC_X, PS2_KEY_X },
{ PS2_KC_Y, PS2_KEY_Y },
{ PS2_KC_Z, PS2_KEY_Z },
{ PS2_KC_SEMI, PS2_KEY_SEMI },
{ PS2_KC_BACK, PS2_KEY_BACK },
{ PS2_KC_OPEN_SQ, PS2_KEY_OPEN_SQ },
{ PS2_KC_CLOSE_SQ, PS2_KEY_CLOSE_SQ },
{ PS2_KC_EQUAL, PS2_KEY_EQUAL },
{ PS2_KC_EUROPE2, PS2_KEY_BACK },
{ PS2_KC_F1, PS2_KEY_F1 },
{ PS2_KC_F2, PS2_KEY_F2 },
{ PS2_KC_F3, PS2_KEY_F3 },
{ PS2_KC_F4, PS2_KEY_F4 },
{ PS2_KC_F5, PS2_KEY_F5 },
{ PS2_KC_F6, PS2_KEY_F6 },
{ PS2_KC_F7, PS2_KEY_F7 },
{ PS2_KC_F8, PS2_KEY_F8 },
{ PS2_KC_F9, PS2_KEY_F9 },
{ PS2_KC_F10, PS2_KEY_F10 },
{ PS2_KC_F11, PS2_KEY_F11 },
{ PS2_KC_F12, PS2_KEY_F12 },
{ PS2_KC_KP_COMMA, PS2_KEY_KP_COMMA },
{ PS2_KC_INTL1, PS2_KEY_INTL1 },
{ PS2_KC_INTL2, PS2_KEY_INTL2 },
{ PS2_KC_INTL3, PS2_KEY_INTL3 },
{ PS2_KC_INTL4, PS2_KEY_INTL4 },
{ PS2_KC_INTL5, PS2_KEY_INTL5 },
{ PS2_KC_LANG1, PS2_KEY_LANG1 },
{ PS2_KC_LANG2, PS2_KEY_LANG2 },
{ PS2_KC_LANG3, PS2_KEY_LANG3 },
{ PS2_KC_LANG4, PS2_KEY_LANG4 },
{ PS2_KC_LANG5, PS2_KEY_LANG5 }
};
/* Two byte Key table after an E0 byte received */
const uint8_t extended_key[][ 2 ] = {
{ PS2_KC_IGNORE, PS2_KEY_IGNORE },
{ PS2_KC_PRTSCR, PS2_KEY_PRTSCR },
{ PS2_KC_CTRL, PS2_KEY_R_CTRL },
{ PS2_KC_ALT, PS2_KEY_R_ALT },
{ PS2_KC_L_GUI, PS2_KEY_L_GUI },
{ PS2_KC_R_GUI, PS2_KEY_R_GUI },
{ PS2_KC_MENU, PS2_KEY_MENU },
{ PS2_KC_BREAK, PS2_KEY_BREAK },
{ PS2_KC_HOME, PS2_KEY_HOME },
{ PS2_KC_END, PS2_KEY_END },
{ PS2_KC_PGUP, PS2_KEY_PGUP },
{ PS2_KC_PGDN, PS2_KEY_PGDN },
{ PS2_KC_L_ARROW, PS2_KEY_L_ARROW },
{ PS2_KC_R_ARROW, PS2_KEY_R_ARROW },
{ PS2_KC_UP_ARROW, PS2_KEY_UP_ARROW },
{ PS2_KC_DN_ARROW, PS2_KEY_DN_ARROW },
{ PS2_KC_INSERT, PS2_KEY_INSERT },
{ PS2_KC_DELETE, PS2_KEY_DELETE },
{ PS2_KC_KP_ENTER, PS2_KEY_KP_ENTER },
{ PS2_KC_KP_DIV, PS2_KEY_KP_DIV },
{ PS2_KC_NEXT_TR, PS2_KEY_NEXT_TR },
{ PS2_KC_PREV_TR, PS2_KEY_PREV_TR },
{ PS2_KC_STOP, PS2_KEY_STOP },
{ PS2_KC_PLAY, PS2_KEY_PLAY },
{ PS2_KC_MUTE, PS2_KEY_MUTE },
{ PS2_KC_VOL_UP, PS2_KEY_VOL_UP },
{ PS2_KC_VOL_DN, PS2_KEY_VOL_DN },
{ PS2_KC_MEDIA, PS2_KEY_MEDIA },
{ PS2_KC_EMAIL, PS2_KEY_EMAIL },
{ PS2_KC_CALC, PS2_KEY_CALC },
{ PS2_KC_COMPUTER, PS2_KEY_COMPUTER },
{ PS2_KC_WEB_SEARCH, PS2_KEY_WEB_SEARCH },
{ PS2_KC_WEB_HOME, PS2_KEY_WEB_HOME },
{ PS2_KC_WEB_BACK, PS2_KEY_WEB_BACK },
{ PS2_KC_WEB_FORWARD, PS2_KEY_WEB_FORWARD },
{ PS2_KC_WEB_STOP, PS2_KEY_WEB_STOP },
{ PS2_KC_WEB_REFRESH, PS2_KEY_WEB_REFRESH },
{ PS2_KC_WEB_FAVOR, PS2_KEY_WEB_FAVOR },
{ PS2_KC_POWER, PS2_KEY_POWER },
{ PS2_KC_SLEEP, PS2_KEY_SLEEP },
{ PS2_KC_WAKE, PS2_KEY_WAKE }
};
/* Scroll lock numeric keypad re-mappings for NOT NUMLOCK */
/* in translated code order order is important */
const uint8_t scroll_remap[] = {
PS2_KEY_INSERT, // PS2_KEY_KP0
PS2_KEY_END, // PS2_KEY_KP1
PS2_KEY_DN_ARROW, // PS2_KEY_KP2
PS2_KEY_PGDN, // PS2_KEY_KP3
PS2_KEY_L_ARROW, // PS2_KEY_KP4
PS2_KEY_IGNORE, // PS2_KEY_KP5
PS2_KEY_R_ARROW, // PS2_KEY_KP6
PS2_KEY_HOME, // PS2_KEY_KP7
PS2_KEY_UP_ARROW, // PS2_KEY_KP8
PS2_KEY_PGUP, // PS2_KEY_KP9
PS2_KEY_DELETE // PS2_KEY_KP_DOT
};
#endif
#endif

8
main/component.mk Normal file
View File

@@ -0,0 +1,8 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#

174
main/font8x8_basic.h Normal file
View File

@@ -0,0 +1,174 @@
/*
* font8x8_basic.h
*
* Created on: 2017/05/03
* Author: yanbe
*/
#ifndef MAIN_FONT8X8_BASIC_H_
#define MAIN_FONT8X8_BASIC_H_
/*
Constant: font8x8_basic_tr
Contains an 90 digree transposed 8x8 font map for unicode points
U+0000 - U+007F (basic latin)
To make it easy to use with SSD1306's GDDRAM mapping and API,
this constant is an 90 degree transposed.
The original version written by Marcel Sondaar is availble at:
https://github.com/dhepper/font8x8/blob/master/font8x8_basic.h
Conversion is done via following procedure:
for (int code = 0; code < 128; code++) {
uint8_t trans[8];
for (int w = 0; w < 8; w++) {
trans[w] = 0x00;
for (int b = 0; b < 8; b++) {
trans[w] |= ((font8x8_basic[code][b] & (1 << w)) >> w) << b;
}
}
for (int w = 0; w < 8; w++) {
if (w == 0) { printf(" { "); }
printf("0x%.2X", trans[w]);
if (w < 7) { printf(", "); }
if (w == 7) { printf(" }, // U+00%.2X (%c)\n", code, code); }
}
}
*/
static uint8_t font8x8_basic_tr[128][8] = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0000 (nul)
{ 0x00, 0x04, 0x02, 0xFF, 0x02, 0x04, 0x00, 0x00 }, // U+0001 (Up Allow)
{ 0x00, 0x20, 0x40, 0xFF, 0x40, 0x20, 0x00, 0x00 }, // U+0002 (Down Allow)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0003
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0004
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0005
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0006
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0007
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0008
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0009
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+000A
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+000B
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+000C
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+000D
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+000E
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+000F
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0010
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0011
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0012
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0013
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0014
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0015
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0016
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0017
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0018
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0019
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+001A
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+001B
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+001C
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+001D
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+001E
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+001F
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0020 (space)
{ 0x00, 0x00, 0x06, 0x5F, 0x5F, 0x06, 0x00, 0x00 }, // U+0021 (!)
{ 0x00, 0x03, 0x03, 0x00, 0x03, 0x03, 0x00, 0x00 }, // U+0022 (")
{ 0x14, 0x7F, 0x7F, 0x14, 0x7F, 0x7F, 0x14, 0x00 }, // U+0023 (#)
{ 0x24, 0x2E, 0x6B, 0x6B, 0x3A, 0x12, 0x00, 0x00 }, // U+0024 ($)
{ 0x46, 0x66, 0x30, 0x18, 0x0C, 0x66, 0x62, 0x00 }, // U+0025 (%)
{ 0x30, 0x7A, 0x4F, 0x5D, 0x37, 0x7A, 0x48, 0x00 }, // U+0026 (&)
{ 0x04, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 }, // U+0027 (')
{ 0x00, 0x1C, 0x3E, 0x63, 0x41, 0x00, 0x00, 0x00 }, // U+0028 (()
{ 0x00, 0x41, 0x63, 0x3E, 0x1C, 0x00, 0x00, 0x00 }, // U+0029 ())
{ 0x08, 0x2A, 0x3E, 0x1C, 0x1C, 0x3E, 0x2A, 0x08 }, // U+002A (*)
{ 0x08, 0x08, 0x3E, 0x3E, 0x08, 0x08, 0x00, 0x00 }, // U+002B (+)
{ 0x00, 0x80, 0xE0, 0x60, 0x00, 0x00, 0x00, 0x00 }, // U+002C (,)
{ 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00 }, // U+002D (-)
{ 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00 }, // U+002E (.)
{ 0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00 }, // U+002F (/)
{ 0x3E, 0x7F, 0x71, 0x59, 0x4D, 0x7F, 0x3E, 0x00 }, // U+0030 (0)
{ 0x40, 0x42, 0x7F, 0x7F, 0x40, 0x40, 0x00, 0x00 }, // U+0031 (1)
{ 0x62, 0x73, 0x59, 0x49, 0x6F, 0x66, 0x00, 0x00 }, // U+0032 (2)
{ 0x22, 0x63, 0x49, 0x49, 0x7F, 0x36, 0x00, 0x00 }, // U+0033 (3)
{ 0x18, 0x1C, 0x16, 0x53, 0x7F, 0x7F, 0x50, 0x00 }, // U+0034 (4)
{ 0x27, 0x67, 0x45, 0x45, 0x7D, 0x39, 0x00, 0x00 }, // U+0035 (5)
{ 0x3C, 0x7E, 0x4B, 0x49, 0x79, 0x30, 0x00, 0x00 }, // U+0036 (6)
{ 0x03, 0x03, 0x71, 0x79, 0x0F, 0x07, 0x00, 0x00 }, // U+0037 (7)
{ 0x36, 0x7F, 0x49, 0x49, 0x7F, 0x36, 0x00, 0x00 }, // U+0038 (8)
{ 0x06, 0x4F, 0x49, 0x69, 0x3F, 0x1E, 0x00, 0x00 }, // U+0039 (9)
{ 0x00, 0x00, 0x66, 0x66, 0x00, 0x00, 0x00, 0x00 }, // U+003A (:)
{ 0x00, 0x80, 0xE6, 0x66, 0x00, 0x00, 0x00, 0x00 }, // U+003B (;)
{ 0x08, 0x1C, 0x36, 0x63, 0x41, 0x00, 0x00, 0x00 }, // U+003C (<)
{ 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x00, 0x00 }, // U+003D (=)
{ 0x00, 0x41, 0x63, 0x36, 0x1C, 0x08, 0x00, 0x00 }, // U+003E (>)
{ 0x02, 0x03, 0x51, 0x59, 0x0F, 0x06, 0x00, 0x00 }, // U+003F (?)
{ 0x3E, 0x7F, 0x41, 0x5D, 0x5D, 0x1F, 0x1E, 0x00 }, // U+0040 (@)
{ 0x7C, 0x7E, 0x13, 0x13, 0x7E, 0x7C, 0x00, 0x00 }, // U+0041 (A)
{ 0x41, 0x7F, 0x7F, 0x49, 0x49, 0x7F, 0x36, 0x00 }, // U+0042 (B)
{ 0x1C, 0x3E, 0x63, 0x41, 0x41, 0x63, 0x22, 0x00 }, // U+0043 (C)
{ 0x41, 0x7F, 0x7F, 0x41, 0x63, 0x3E, 0x1C, 0x00 }, // U+0044 (D)
{ 0x41, 0x7F, 0x7F, 0x49, 0x5D, 0x41, 0x63, 0x00 }, // U+0045 (E)
{ 0x41, 0x7F, 0x7F, 0x49, 0x1D, 0x01, 0x03, 0x00 }, // U+0046 (F)
{ 0x1C, 0x3E, 0x63, 0x41, 0x51, 0x73, 0x72, 0x00 }, // U+0047 (G)
{ 0x7F, 0x7F, 0x08, 0x08, 0x7F, 0x7F, 0x00, 0x00 }, // U+0048 (H)
{ 0x00, 0x41, 0x7F, 0x7F, 0x41, 0x00, 0x00, 0x00 }, // U+0049 (I)
{ 0x30, 0x70, 0x40, 0x41, 0x7F, 0x3F, 0x01, 0x00 }, // U+004A (J)
{ 0x41, 0x7F, 0x7F, 0x08, 0x1C, 0x77, 0x63, 0x00 }, // U+004B (K)
{ 0x41, 0x7F, 0x7F, 0x41, 0x40, 0x60, 0x70, 0x00 }, // U+004C (L)
{ 0x7F, 0x7F, 0x0E, 0x1C, 0x0E, 0x7F, 0x7F, 0x00 }, // U+004D (M)
{ 0x7F, 0x7F, 0x06, 0x0C, 0x18, 0x7F, 0x7F, 0x00 }, // U+004E (N)
{ 0x1C, 0x3E, 0x63, 0x41, 0x63, 0x3E, 0x1C, 0x00 }, // U+004F (O)
{ 0x41, 0x7F, 0x7F, 0x49, 0x09, 0x0F, 0x06, 0x00 }, // U+0050 (P)
{ 0x1E, 0x3F, 0x21, 0x71, 0x7F, 0x5E, 0x00, 0x00 }, // U+0051 (Q)
{ 0x41, 0x7F, 0x7F, 0x09, 0x19, 0x7F, 0x66, 0x00 }, // U+0052 (R)
{ 0x26, 0x6F, 0x4D, 0x59, 0x73, 0x32, 0x00, 0x00 }, // U+0053 (S)
{ 0x03, 0x41, 0x7F, 0x7F, 0x41, 0x03, 0x00, 0x00 }, // U+0054 (T)
{ 0x7F, 0x7F, 0x40, 0x40, 0x7F, 0x7F, 0x00, 0x00 }, // U+0055 (U)
{ 0x1F, 0x3F, 0x60, 0x60, 0x3F, 0x1F, 0x00, 0x00 }, // U+0056 (V)
{ 0x7F, 0x7F, 0x30, 0x18, 0x30, 0x7F, 0x7F, 0x00 }, // U+0057 (W)
{ 0x43, 0x67, 0x3C, 0x18, 0x3C, 0x67, 0x43, 0x00 }, // U+0058 (X)
{ 0x07, 0x4F, 0x78, 0x78, 0x4F, 0x07, 0x00, 0x00 }, // U+0059 (Y)
{ 0x47, 0x63, 0x71, 0x59, 0x4D, 0x67, 0x73, 0x00 }, // U+005A (Z)
{ 0x00, 0x7F, 0x7F, 0x41, 0x41, 0x00, 0x00, 0x00 }, // U+005B ([)
{ 0x01, 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x00 }, // U+005C (\)
{ 0x00, 0x41, 0x41, 0x7F, 0x7F, 0x00, 0x00, 0x00 }, // U+005D (])
{ 0x08, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x08, 0x00 }, // U+005E (^)
{ 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }, // U+005F (_)
{ 0x00, 0x00, 0x03, 0x07, 0x04, 0x00, 0x00, 0x00 }, // U+0060 (`)
{ 0x20, 0x74, 0x54, 0x54, 0x3C, 0x78, 0x40, 0x00 }, // U+0061 (a)
{ 0x41, 0x7F, 0x3F, 0x48, 0x48, 0x78, 0x30, 0x00 }, // U+0062 (b)
{ 0x38, 0x7C, 0x44, 0x44, 0x6C, 0x28, 0x00, 0x00 }, // U+0063 (c)
{ 0x30, 0x78, 0x48, 0x49, 0x3F, 0x7F, 0x40, 0x00 }, // U+0064 (d)
{ 0x38, 0x7C, 0x54, 0x54, 0x5C, 0x18, 0x00, 0x00 }, // U+0065 (e)
{ 0x48, 0x7E, 0x7F, 0x49, 0x03, 0x02, 0x00, 0x00 }, // U+0066 (f)
{ 0x98, 0xBC, 0xA4, 0xA4, 0xF8, 0x7C, 0x04, 0x00 }, // U+0067 (g)
{ 0x41, 0x7F, 0x7F, 0x08, 0x04, 0x7C, 0x78, 0x00 }, // U+0068 (h)
{ 0x00, 0x44, 0x7D, 0x7D, 0x40, 0x00, 0x00, 0x00 }, // U+0069 (i)
{ 0x60, 0xE0, 0x80, 0x80, 0xFD, 0x7D, 0x00, 0x00 }, // U+006A (j)
{ 0x41, 0x7F, 0x7F, 0x10, 0x38, 0x6C, 0x44, 0x00 }, // U+006B (k)
{ 0x00, 0x41, 0x7F, 0x7F, 0x40, 0x00, 0x00, 0x00 }, // U+006C (l)
{ 0x7C, 0x7C, 0x18, 0x38, 0x1C, 0x7C, 0x78, 0x00 }, // U+006D (m)
{ 0x7C, 0x7C, 0x04, 0x04, 0x7C, 0x78, 0x00, 0x00 }, // U+006E (n)
{ 0x38, 0x7C, 0x44, 0x44, 0x7C, 0x38, 0x00, 0x00 }, // U+006F (o)
{ 0x84, 0xFC, 0xF8, 0xA4, 0x24, 0x3C, 0x18, 0x00 }, // U+0070 (p)
{ 0x18, 0x3C, 0x24, 0xA4, 0xF8, 0xFC, 0x84, 0x00 }, // U+0071 (q)
{ 0x44, 0x7C, 0x78, 0x4C, 0x04, 0x1C, 0x18, 0x00 }, // U+0072 (r)
{ 0x48, 0x5C, 0x54, 0x54, 0x74, 0x24, 0x00, 0x00 }, // U+0073 (s)
{ 0x00, 0x04, 0x3E, 0x7F, 0x44, 0x24, 0x00, 0x00 }, // U+0074 (t)
{ 0x3C, 0x7C, 0x40, 0x40, 0x3C, 0x7C, 0x40, 0x00 }, // U+0075 (u)
{ 0x1C, 0x3C, 0x60, 0x60, 0x3C, 0x1C, 0x00, 0x00 }, // U+0076 (v)
{ 0x3C, 0x7C, 0x70, 0x38, 0x70, 0x7C, 0x3C, 0x00 }, // U+0077 (w)
{ 0x44, 0x6C, 0x38, 0x10, 0x38, 0x6C, 0x44, 0x00 }, // U+0078 (x)
{ 0x9C, 0xBC, 0xA0, 0xA0, 0xFC, 0x7C, 0x00, 0x00 }, // U+0079 (y)
{ 0x4C, 0x64, 0x74, 0x5C, 0x4C, 0x64, 0x00, 0x00 }, // U+007A (z)
{ 0x08, 0x08, 0x3E, 0x77, 0x41, 0x41, 0x00, 0x00 }, // U+007B ({)
{ 0x00, 0x00, 0x00, 0x77, 0x77, 0x00, 0x00, 0x00 }, // U+007C (|)
{ 0x41, 0x41, 0x77, 0x3E, 0x08, 0x08, 0x00, 0x00 }, // U+007D (})
{ 0x02, 0x03, 0x01, 0x03, 0x02, 0x03, 0x01, 0x00 }, // U+007E (~)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } // U+007F
};
#endif /* MAIN_FONT8X8_BASIC_H_ */

462
main/main.cpp Normal file
View File

@@ -0,0 +1,462 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Name: mz25key.ino
// Created: Jan 2022
// Version: v1.0
// Author(s): Philip Smart
// Description: MZ2500/2800 Key Matrix logic.
// This source file contains the logic to transmit the virtual key matrix, which is
// built from PS/2 scan codes, to the MZ2500/2800.
// Credits:
// Copyright: (c) 2022 Philip Smart <philip.smart@net2net.org>
//
// History: Jan 2022 - Initial write.
//
// Notes: See Makefile to enable/disable conditional components
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// This source file is free software: you can redistribute it and#or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This source file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "Arduino.h"
#include "driver/gpio.h"
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"
#include "PS2KeyAdvanced.h"
#include "MZKeyTable.h"
#include "ssd1306.h"
#include "font8x8_basic.h"
#include "sdkconfig.h"
// ESP Logging tag.
#define tag "mz25key"
//////////////////////////////////////////////////////////////////////////
// Important:
//
// All configuration is performed via the 'idf.py menuconfig' command.
// Optionally override via the definitions below.
// Debugging options.
//////////////////////////////////////////////////////////////////////////
//#define CONFIG_DEBUG_OLED !CONFIG_OLED_DISABLED
//#define CONFIG_PWRLED 25
//#define CONFIG_PS2_HW_DATAPIN 14
//#define CONFIG_PS2_HW_CLKPIN 13
// Macros.
//
#define NUMELEM(a) (sizeof(a)/sizeof(a[0]))
// Structure to manage the translated key matrix. This is updated by the ps2Interface thread and read by the mzInterface thead.
typedef struct {
uint8_t strobeAll;
uint8_t keyMatrix[15];
} t_mzControl;
volatile t_mzControl mzControl = { 0xFF, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
// Instantiate base classes. First, production required objects.
PS2KeyAdvanced Keyboard;
// Nex/t, debug required objects.
SSD1306_t SSD1306;
// Handle to interact with the mz-2500 interface thread.
TaskHandle_t TaskMZ25IF = NULL;
TaskHandle_t TaskPS2IF = NULL;
// Spin lock mutex to hold a core tied to an uninterruptable method. This only works on dual core ESP32's.
static portMUX_TYPE mzMutex = portMUX_INITIALIZER_UNLOCKED;
#if defined(CONFIG_DEBUG_OLED) || !defined(CONFIG_OLED_DISABLED)
// Printf to terminal, needed when OLED is connected for debugging.
void terminalPrintf(const char * format, ...)
{
va_list ap;
va_start(ap, format);
int size = vsnprintf(nullptr, 0, format, ap) + 1;
if (size > 0)
{
va_end(ap);
va_start(ap, format);
char buf[size + 1];
vsnprintf(buf, size, format, ap);
// u8g2.print(buf);
// u8g2.sendBuffer();
}
va_end(ap);
}
#endif
// Method to connect and interact with the MZ-2500/MZ-2800 keyboard controller.
// The basic requirement is to:
// 1. Detect a falling edge on the RTSN signal
// 2. Read the provided ROW number.
// 3. Lookup the matrix data for given ROW.
// 4. Output data to LS257 Mux.
// 5. Loop
//
// The ps2Interface method is responsible for obtaining a PS/2 Keyboard scancode and
// creating the corresponding virtual matrix.
//
// NB: As this method holds Core 1 under spinlock, no FreeRTOS or Arduino access
// can be made except for basic I/O ports. The spinlock has to be released for non
// I/O work.
//
IRAM_ATTR void mz25Interface( void * pvParameters )
{
// Locals.
volatile unsigned long idx;
// Create, initialise and hold a spinlock so the current core is bound to this one method.
portENTER_CRITICAL(&mzMutex);
// Permanent loop, just wait for an RTSN strobe, latch the row, lookup matrix and output.
for(;;)
{
gpio_set_level((gpio_num_t)CONFIG_PWRLED, 1);
// digitalWrite(CONFIG_PWRLED, HIGH);
for(idx=0; idx < 10000000; idx++)
{
if(idx % 1000 == 0)
{
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
TIMERG0.wdt_feed=1; // feed dog
TIMERG0.wdt_wprotect=0; // write protect
TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
TIMERG1.wdt_feed=1; // feed dog
TIMERG1.wdt_wprotect=0; // write protect
}
}
// digitalWrite(CONFIG_PWRLED, LOW);
gpio_set_level((gpio_num_t)CONFIG_PWRLED, 0);
for(idx=0; idx < 10000000; idx++)
{
if(idx % 1000 == 0)
{
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
TIMERG0.wdt_feed=1; // feed dog
TIMERG0.wdt_wprotect=0; // write protect
TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
TIMERG1.wdt_feed=1; // feed dog
TIMERG1.wdt_wprotect=0; // write protect
}
}
}
}
// Method to convert the PS2 scan code into a key matrix representation which the MZ-2500/2800 is expecting.
//
IRAM_ATTR unsigned char updateMatrix(uint16_t data)
{
// Locals.
uint8_t idx;
uint8_t idx2;
// Loop through the entire conversion table to find a match on this key, if found appy the conversion to the virtual
// switch matrix.
//
for(idx=0; idx < NUMELEM(PS2toMZ); idx++)
{
// Match key code?
if(PS2toMZ[idx][PSMZTBL_KEYPOS] == (uint8_t)(data&0xFF))
{
// Match Raw, Shift, Function, Control, ALT or ALT-Gr?
if( (PS2toMZ[idx][PSMZTBL_SHIFTPOS] == 0 && PS2toMZ[idx][PSMZTBL_FUNCPOS] == 0 && PS2toMZ[idx][PSMZTBL_CTRLPOS] == 0 && PS2toMZ[idx][PSMZTBL_ALTPOS] == 0 && PS2toMZ[idx][PSMZTBL_ALTGRPOS] == 0) ||
((data & PS2_SHIFT) && PS2toMZ[idx][PSMZTBL_SHIFTPOS] == 1) ||
((data & PS2_FUNCTION) && PS2toMZ[idx][PSMZTBL_FUNCPOS] == 1) ||
((data & PS2_CTRL) && PS2toMZ[idx][PSMZTBL_CTRLPOS] == 1) ||
((data & PS2_ALT) && PS2toMZ[idx][PSMZTBL_ALTPOS] == 1) ||
((data & PS2_ALT_GR) && PS2toMZ[idx][PSMZTBL_ALTGRPOS] == 1) )
{
// RELEASE (PS2_BREAK == 1) or PRESS?
if((data & PS2_BREAK))
{
// Reset the matrix bit according to the lookup table. 1 = No key, 0 = key in the matrix.
if(PS2toMZ[idx][PSMZTBL_MXROW1] != 0xFF)
{
mzControl.keyMatrix[PS2toMZ[idx][PSMZTBL_MXROW1]] |= PS2toMZ[idx][PSMZTBL_MXKEY1];
}
if(PS2toMZ[idx][PSMZTBL_MXROW2] != 0xFF)
{
mzControl.keyMatrix[PS2toMZ[idx][PSMZTBL_MXROW2]] |= PS2toMZ[idx][PSMZTBL_MXKEY2];
}
if(PS2toMZ[idx][PSMZTBL_MXROW3] != 0xFF)
{
mzControl.keyMatrix[PS2toMZ[idx][PSMZTBL_MXROW3]] |= PS2toMZ[idx][PSMZTBL_MXKEY3];
}
} else
{
// Set the matrix bit according to the lookup table. 1 = No key, 0 = key in the matrix.
if(PS2toMZ[idx][PSMZTBL_MXROW1] != 0xFF)
{
mzControl.keyMatrix[PS2toMZ[idx][PSMZTBL_MXROW1]] &= ~PS2toMZ[idx][PSMZTBL_MXKEY1];
}
if(PS2toMZ[idx][PSMZTBL_MXROW2] != 0xFF)
{
mzControl.keyMatrix[PS2toMZ[idx][PSMZTBL_MXROW2]] &= ~PS2toMZ[idx][PSMZTBL_MXKEY2];
}
if(PS2toMZ[idx][PSMZTBL_MXROW3] != 0xFF)
{
mzControl.keyMatrix[PS2toMZ[idx][PSMZTBL_MXROW3]] &= ~PS2toMZ[idx][PSMZTBL_MXKEY3];
}
}
}
// Re-calculate the Strobe All (KD4 = 1) signal, this indicates if any bit (key) in the matrix is active.
mzControl.strobeAll = 0xFF;
for(idx2=0; idx2 < 15; idx2++)
{
mzControl.strobeAll &= mzControl.keyMatrix[idx2];
}
}
}
return data;
}
// Primary PS/2 thread, running on Core 1.
// This thread is responsible for receiving PS/2 scan codes and mapping them to an MZ-2500/2800 keyboard matrix.
// The PS/2 data is received via interrupt.
//
IRAM_ATTR void ps2Interface( void * pvParameters )
//IRAM_ATTR void ps2Interface( )
{
// Locals.
uint16_t scanCode = 0x0000;
#if defined(CONFIG_DEBUG_OLED) || !defined(CONFIG_OLED_DISABLED)
uint8_t dataChange = 0;
static int clrScreen = 1;
static int scanPrtCol = 0;
static uint32_t clrTimer = 0;
#endif
#if defined(CONFIG_DEBUG_OLED) || !defined(CONFIG_OLED_DISABLED)
if((clrTimer > 0 && --clrTimer == 0) || ((scanCode&0xFF) == PS2_KEY_C && scanCode & PS2_BREAK ))
{
// Clear old scan code data.
// u8g2.drawStr(0, 8*7, " ");
// u8g2.sendBuffer();
scanPrtCol = 0;
}
#endif
while(1)
{
// Check for PS/2 keyboard scan codes.
while((scanCode = Keyboard.read()) != 0)
{
printf("%04x\n", scanCode);
#if defined(CONFIG_DEBUG_OLED) || !defined(CONFIG_OLED_DISABLED)
// Clear screen as requested.
if(clrScreen == 1)
{
ssd1306_clear_screen(&SSD1306, false);
clrScreen = 0;
}
// Output the scan code for verification.
// u8g2.setCursor((scanPrtCol*5)*6, 8*7);
terminalPrintf("%04x,", scanCode);
if(scanPrtCol++ >= 3) scanPrtCol = 0;
// u8g2.sendBuffer();
clrTimer = 2000000;
dataChange = 1;
#endif
// Update the virtual matrix with the new key value.
updateMatrix(scanCode);
}
#if defined(CONFIG_DEBUG_OLED) || !defined(CONFIG_OLED_DISABLED)
// Output the MZ virtual keyboard matrix for verification.
uint8_t oledBuf[8][16];
if(dataChange)
{
for(int idx=0; idx < 15; idx++)
{
for(int idx2=0; idx2 < 8; idx2++)
{
oledBuf[idx2][idx] = ((mzControl.keyMatrix[idx] >> idx2)&0x01) == 1 ? '1' : '0';
}
}
for(int idx=0; idx < 8; idx++)
{
ssd1306_display_text(&SSD1306, idx, (char *)oledBuf[idx], 15, false);
}
}
#endif
}
}
// Setup method to configure ports, devices and threads prior to application run.
// Configuration:
// PS/2 Keyboard over 2 wire interface
// Power/Status LED
// Optional OLED debug output screen
// 4 bit input - MZ-2500/2800 Row Number
// 8 bit output - MZ-2500/2800 Scan data
// 1 bit input - RTSN strobe line, low indicating a new Row Number available.
// 1 bit input - KD4, High = Key scan data required, Low = AND of all key matrix rows required.
//
void setup()
{
// Locals.
gpio_config_t io_conf;
// Start the keyboard, no mouse.
ESP_LOGI(tag, "Initialise PS2 keyboard.");
Keyboard.begin(CONFIG_PS2_HW_DATAPIN, CONFIG_PS2_HW_CLKPIN);
// If OLED connected and enabled, include the screen controller for debug output.
//
#if defined(CONFIG_DEBUG_OLED) || !defined(CONFIG_OLED_DISABLED)
#if CONFIG_I2C_INTERFACE
ESP_LOGI(tag, "INTERFACE is i2c");
ESP_LOGI(tag, "CONFIG_SDA_GPIO=%d", CONFIG_SDA_GPIO);
ESP_LOGI(tag, "CONFIG_SCL_GPIO=%d", CONFIG_SCL_GPIO);
ESP_LOGI(tag, "CONFIG_RESET_GPIO=%d", CONFIG_RESET_GPIO);
i2c_master_init(&SSD1306, CONFIG_SDA_GPIO, CONFIG_SCL_GPIO, CONFIG_RESET_GPIO);
#endif // CONFIG_I2C_INTERFACE
#if CONFIG_SPI_INTERFACE
ESP_LOGI(tag, "INTERFACE is SPI");
ESP_LOGI(tag, "CONFIG_MOSI_GPIO=%d", CONFIG_MOSI_GPIO);
ESP_LOGI(tag, "CONFIG_SCLK_GPIO=%d", CONFIG_SCLK_GPIO);
ESP_LOGI(tag, "CONFIG_CS_GPIO=%d", CONFIG_CS_GPIO);
ESP_LOGI(tag, "CONFIG_DC_GPIO=%d", CONFIG_DC_GPIO);
ESP_LOGI(tag, "CONFIG_RESET_GPIO=%d", CONFIG_RESET_GPIO);
spi_master_init(&SSD1306, CONFIG_MOSI_GPIO, CONFIG_SCLK_GPIO, CONFIG_CS_GPIO, CONFIG_DC_GPIO, CONFIG_RESET_GPIO);
#endif // CONFIG_SPI_INTERFACE
#if CONFIG_SSD1306_128x64
ESP_LOGI(tag, "Panel is 128x64");
ssd1306_init(&SSD1306, 128, 64);
#endif // CONFIG_SSD1306_128x64
#if CONFIG_SSD1306_128x32
ESP_LOGI(tag, "Panel is 128x32");
ssd1306_init(&SSD1306, 128, 32);
#endif // CONFIG_SSD1306_128x32
ssd1306_clear_screen(&SSD1306, false);
ssd1306_contrast(&SSD1306, 0xff);
#endif
// Setup power LED.
//pinMode(CONFIG_PWRLED, OUTPUT);
//#define GPIO_OUTPUT_PIN_SEL ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1))
ESP_LOGI(tag, "Configuring Power LED.");
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL<<CONFIG_PWRLED);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
// Configure 4 inputs to be the Strobe Row Number which is used to index the virtual key matrix and the strobe data returned.
ESP_LOGI(tag, "Configuring MZ-2500/2800 4 bit Row Number Inputs.");
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDB0);
io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDB1);
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDB2);
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDB3);
gpio_config(&io_conf);
ESP_LOGI(tag, "Configuring MZ-2500/2800 8 bit Strobe data Outputs.");
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDO0);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDO1);
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDO2);
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDO3);
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDO4);
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDO5);
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDO6);
gpio_config(&io_conf);
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDO7);
gpio_config(&io_conf);
ESP_LOGI(tag, "Configuring MZ-2500/2800 RTSN Input.");
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_RTSNI);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&io_conf);
ESP_LOGI(tag, "Configuring MZ-2500/2800 KD4 Input.");
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL<<CONFIG_MZ_KDI4);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&io_conf);
// Check to see if keyboard available, no keyboard = no point, so halt!
// Firstly, ping keyboard to see if it is there.
ESP_LOGI(tag, "Detecting PS2 keyboard.");
Keyboard.echo();
vTaskDelay(6);
uint16_t chr = Keyboard.read();
if( (chr & 0xFF) != PS2_KEY_ECHO && (chr & 0xFF) != PS2_KEY_BAT)
{
ESP_LOGE(tag, "No PS2 keyboard detected, connect and reset to continue.\n");
#if defined(CONFIG_DEBUG_OLED) || !defined(CONFIG_OLED_DISABLED)
ssd1306_display_text(&SSD1306, 0, "No PS2 Keyboard", 15, false);
#endif
while(1);
}
// Create a task pinned to core 0 which will fulfill the MZ-2500/2800 interface. This task has the highest priority
// and it will also hold spinlock and manipulate the watchdog to ensure a scan cycle timing can be met. This means
// all other tasks running on Core 1 will suspend. The PS/2 controller, running on the ULP processor will continue
// to interact with the PS/2 keyboard and buffer scan codes.
//
ESP_LOGI(tag, "Starting mz25if thread...");
xTaskCreatePinnedToCore(mz25Interface, "mz25if", 32768, NULL, 25, &TaskMZ25IF, 1);
vTaskDelay(500);
ESP_LOGI(tag, "Starting ps2if thread...");
xTaskCreatePinnedToCore(ps2Interface, "ps2if", 32768, NULL, 22, &TaskPS2IF, 0);
vTaskDelay(500);
}
extern "C" void app_main()
{
// initArduino();
// Setup hardware and start primary control threads,
setup();
// Nothing to do, yield CPU.
while(1) {
vTaskDelay(10000);
}
}

229
main/ssd1306.c Normal file
View File

@@ -0,0 +1,229 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "ssd1306.h"
#include "font8x8_basic.h"
#define tag "SSD1306"
void ssd1306_init(SSD1306_t * dev, int width, int height)
{
if (dev->_address == SPIAddress) {
spi_init(dev, width, height);
} else {
i2c_init(dev, width, height);
}
}
void ssd1306_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert)
{
if (page >= dev->_pages) return;
int _text_len = text_len;
if (_text_len > 16) _text_len = 16;
uint8_t seg = 0;
uint8_t image[8];
for (uint8_t i = 0; i < _text_len; i++) {
memcpy(image, font8x8_basic_tr[(uint8_t)text[i]], 8);
if (invert) ssd1306_invert(image, 8);
if (dev->_flip) ssd1306_flip(image, 8);
if (dev->_address == SPIAddress) {
spi_display_image(dev, page, seg, image, 8);
} else {
i2c_display_image(dev, page, seg, image, 8);
}
seg = seg + 8;
}
}
void ssd1306_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width)
{
if (dev->_address == SPIAddress) {
spi_display_image(dev, page, seg, images, width);
} else {
i2c_display_image(dev, page, seg, images, width);
}
}
void ssd1306_clear_screen(SSD1306_t * dev, bool invert)
{
char space[16];
memset(space, 0x20, sizeof(space));
for (int page = 0; page < dev->_pages; page++) {
ssd1306_display_text(dev, page, space, sizeof(space), invert);
}
}
void ssd1306_clear_line(SSD1306_t * dev, int page, bool invert)
{
char space[16];
memset(space, 0x20, sizeof(space));
ssd1306_display_text(dev, page, space, sizeof(space), invert);
}
void ssd1306_contrast(SSD1306_t * dev, int contrast)
{
if (dev->_address == SPIAddress) {
spi_contrast(dev, contrast);
} else {
i2c_contrast(dev, contrast);
}
}
void ssd1306_software_scroll(SSD1306_t * dev, int start, int end)
{
ESP_LOGD(tag, "software_scroll start=%d end=%d _pages=%d", start, end, dev->_pages);
if (start < 0 || end < 0) {
dev->_scEnable = false;
} else if (start >= dev->_pages || end >= dev->_pages) {
dev->_scEnable = false;
} else {
dev->_scEnable = true;
dev->_scStart = start;
dev->_scEnd = end;
dev->_scDirection = 1;
if (start > end ) dev->_scDirection = -1;
for (int i=0;i<dev->_pages;i++) {
dev->_page[i]._valid = false;
dev->_page[i]._segLen = 0;
}
}
}
void ssd1306_scroll_text(SSD1306_t * dev, char * text, int text_len, bool invert)
{
ESP_LOGD(tag, "dev->_scEnable=%d", dev->_scEnable);
if (dev->_scEnable == false) return;
void (*func)(SSD1306_t * dev, int page, int seg, uint8_t * images, int width);
if (dev->_address == SPIAddress) {
func = spi_display_image;
} else {
func = i2c_display_image;
}
int srcIndex = dev->_scEnd - dev->_scDirection;
while(1) {
int dstIndex = srcIndex + dev->_scDirection;
ESP_LOGD(tag, "srcIndex=%d dstIndex=%d", srcIndex,dstIndex);
dev->_page[dstIndex]._valid = dev->_page[srcIndex]._valid;
dev->_page[dstIndex]._segLen = dev->_page[srcIndex]._segLen;
for(int seg = 0; seg < dev->_width; seg++) {
dev->_page[dstIndex]._segs[seg] = dev->_page[srcIndex]._segs[seg];
}
ESP_LOGD(tag, "_valid=%d", dev->_page[dstIndex]._valid);
if (dev->_page[dstIndex]._valid) (*func)(dev, dstIndex, 0, dev->_page[dstIndex]._segs, dev->_page[srcIndex]._segLen);
if (srcIndex == dev->_scStart) break;
srcIndex = srcIndex - dev->_scDirection;
}
int _text_len = text_len;
if (_text_len > 16) _text_len = 16;
uint8_t seg = 0;
uint8_t image[8];
for (uint8_t i = 0; i < _text_len; i++) {
memcpy(image, font8x8_basic_tr[(uint8_t)text[i]], 8);
if (invert) ssd1306_invert(image, 8);
if (dev->_flip) ssd1306_flip(image, 8);
(*func)(dev, srcIndex, seg, image, 8);
for(int j=0;j<8;j++) dev->_page[srcIndex]._segs[seg+j] = image[j];
seg = seg + 8;
}
dev->_page[srcIndex]._valid = true;
dev->_page[srcIndex]._segLen = seg;
}
void ssd1306_scroll_clear(SSD1306_t * dev)
{
ESP_LOGD(tag, "dev->_scEnable=%d", dev->_scEnable);
if (dev->_scEnable == false) return;
int srcIndex = dev->_scEnd - dev->_scDirection;
while(1) {
int dstIndex = srcIndex + dev->_scDirection;
ESP_LOGD(tag, "srcIndex=%d dstIndex=%d", srcIndex,dstIndex);
ssd1306_clear_line(dev, dstIndex, false);
dev->_page[dstIndex]._valid = false;
if (dstIndex == dev->_scStart) break;
srcIndex = srcIndex - dev->_scDirection;
}
}
void ssd1306_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll)
{
if (dev->_address == SPIAddress) {
spi_hardware_scroll(dev, scroll);
} else {
i2c_hardware_scroll(dev, scroll);
}
}
void ssd1306_invert(uint8_t *buf, size_t blen)
{
uint8_t wk;
for(int i=0; i<blen; i++){
wk = buf[i];
buf[i] = ~wk;
}
}
// Flip upside down
void ssd1306_flip(uint8_t *buf, size_t blen)
{
for(int i=0; i<blen; i++){
buf[i] = ssd1306_rotate(buf[i]);
}
}
// Rotate 8-bit data
// 0x12-->0x48
uint8_t ssd1306_rotate(uint8_t ch1) {
uint8_t ch2 = 0;
for (int j=0;j<8;j++) {
ch2 = (ch2 << 1) + (ch1 & 0x01);
ch1 = ch1 >> 1;
}
return ch2;
}
void ssd1306_fadeout(SSD1306_t * dev)
{
void (*func)(SSD1306_t * dev, int page, int seg, uint8_t * images, int width);
if (dev->_address == SPIAddress) {
func = spi_display_image;
} else {
func = i2c_display_image;
}
uint8_t image[1];
for(int page=0; page<dev->_pages; page++) {
image[0] = 0xFF;
for(int line=0; line<8; line++) {
if (dev->_flip) {
image[0] = image[0] >> 1;
} else {
image[0] = image[0] << 1;
}
for(int seg=0; seg<128; seg++) {
(*func)(dev, page, seg, image, 1);
}
}
}
}
void ssd1306_dump(SSD1306_t dev)
{
printf("_address=%x\n",dev._address);
printf("_width=%x\n",dev._width);
printf("_height=%x\n",dev._height);
printf("_pages=%x\n",dev._pages);
}

143
main/ssd1306.h Normal file
View File

@@ -0,0 +1,143 @@
#ifdef __cplusplus
extern "C" {
#endif
#ifndef MAIN_SSD1306_H_
#define MAIN_SSD1306_H_
#include "driver/spi_master.h"
// Following definitions are bollowed from
// http://robotcantalk.blogspot.com/2015/03/interfacing-arduino-with-ssd1306-driven.html
/* Control byte for i2c
Co : bit 8 : Continuation Bit
* 1 = no-continuation (only one byte to follow)
* 0 = the controller should expect a stream of bytes.
D/C# : bit 7 : Data/Command Select bit
* 1 = the next byte or byte stream will be Data.
* 0 = a Command byte or byte stream will be coming up next.
Bits 6-0 will be all zeros.
Usage:
0x80 : Single Command byte
0x00 : Command Stream
0xC0 : Single Data byte
0x40 : Data Stream
*/
#define OLED_CONTROL_BYTE_CMD_SINGLE 0x80
#define OLED_CONTROL_BYTE_CMD_STREAM 0x00
#define OLED_CONTROL_BYTE_DATA_SINGLE 0xC0
#define OLED_CONTROL_BYTE_DATA_STREAM 0x40
// Fundamental commands (pg.28)
#define OLED_CMD_SET_CONTRAST 0x81 // follow with 0x7F
#define OLED_CMD_DISPLAY_RAM 0xA4
#define OLED_CMD_DISPLAY_ALLON 0xA5
#define OLED_CMD_DISPLAY_NORMAL 0xA6
#define OLED_CMD_DISPLAY_INVERTED 0xA7
#define OLED_CMD_DISPLAY_OFF 0xAE
#define OLED_CMD_DISPLAY_ON 0xAF
// Addressing Command Table (pg.30)
#define OLED_CMD_SET_MEMORY_ADDR_MODE 0x20
#define OLED_CMD_SET_HORI_ADDR_MODE 0x00 // Horizontal Addressing Mode
#define OLED_CMD_SET_VERT_ADDR_MODE 0x01 // Vertical Addressing Mode
#define OLED_CMD_SET_PAGE_ADDR_MODE 0x02 // Page Addressing Mode
#define OLED_CMD_SET_COLUMN_RANGE 0x21 // can be used only in HORZ/VERT mode - follow with 0x00 and 0x7F = COL127
#define OLED_CMD_SET_PAGE_RANGE 0x22 // can be used only in HORZ/VERT mode - follow with 0x00 and 0x07 = PAGE7
// Hardware Config (pg.31)
#define OLED_CMD_SET_DISPLAY_START_LINE 0x40
#define OLED_CMD_SET_SEGMENT_REMAP_0 0xA0
#define OLED_CMD_SET_SEGMENT_REMAP_1 0xA1
#define OLED_CMD_SET_MUX_RATIO 0xA8 // follow with 0x3F = 64 MUX
#define OLED_CMD_SET_COM_SCAN_MODE 0xC8
#define OLED_CMD_SET_DISPLAY_OFFSET 0xD3 // follow with 0x00
#define OLED_CMD_SET_COM_PIN_MAP 0xDA // follow with 0x12
#define OLED_CMD_NOP 0xE3 // NOP
// Timing and Driving Scheme (pg.32)
#define OLED_CMD_SET_DISPLAY_CLK_DIV 0xD5 // follow with 0x80
#define OLED_CMD_SET_PRECHARGE 0xD9 // follow with 0xF1
#define OLED_CMD_SET_VCOMH_DESELCT 0xDB // follow with 0x30
// Charge Pump (pg.62)
#define OLED_CMD_SET_CHARGE_PUMP 0x8D // follow with 0x14
// Scrolling Command
#define OLED_CMD_HORIZONTAL_RIGHT 0x26
#define OLED_CMD_HORIZONTAL_LEFT 0x27
#define OLED_CMD_CONTINUOUS_SCROLL 0x29
#define OLED_CMD_DEACTIVE_SCROLL 0x2E
#define OLED_CMD_ACTIVE_SCROLL 0x2F
#define OLED_CMD_VERTICAL 0xA3
#define I2CAddress 0x3C
#define SPIAddress 0xFF
typedef enum {
SCROLL_RIGHT = 1,
SCROLL_LEFT = 2,
SCROLL_DOWN = 3,
SCROLL_UP = 4,
SCROLL_STOP = 5
} ssd1306_scroll_type_t;
typedef struct {
bool _valid;
int _segLen; // 0-128
uint8_t _segs[128];
} PAGE_t;
typedef struct {
int _address;
int _width;
int _height;
int _pages;
int _dc;
spi_device_handle_t _SPIHandle;
bool _scEnable;
int _scStart;
int _scEnd;
int _scDirection;
PAGE_t _page[8];
bool _flip;
} SSD1306_t;
void ssd1306_init(SSD1306_t * dev, int width, int height);
void ssd1306_display_text(SSD1306_t * dev, int page, char * text, int text_len, bool invert);
void ssd1306_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width);
void ssd1306_clear_screen(SSD1306_t * dev, bool invert);
void ssd1306_clear_line(SSD1306_t * dev, int page, bool invert);
void ssd1306_contrast(SSD1306_t * dev, int contrast);
void ssd1306_software_scroll(SSD1306_t * dev, int start, int end);
void ssd1306_scroll_text(SSD1306_t * dev, char * text, int text_len, bool invert);
void ssd1306_scroll_clear(SSD1306_t * dev);
void ssd1306_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll);
void ssd1306_invert(uint8_t *buf, size_t blen);
void ssd1306_flip(uint8_t *buf, size_t blen);
uint8_t ssd1306_rotate(uint8_t ch1);
void ssd1306_fadeout(SSD1306_t * dev);
void ssd1306_dump(SSD1306_t dev);
void i2c_master_init(SSD1306_t * dev, int16_t sda, int16_t scl, int16_t reset);
void i2c_init(SSD1306_t * dev, int width, int height);
void i2c_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width);
void i2c_contrast(SSD1306_t * dev, int contrast);
void i2c_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll);
void spi_master_init(SSD1306_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int16_t GPIO_CS, int16_t GPIO_DC, int16_t GPIO_RESET);
bool spi_master_write_byte(spi_device_handle_t SPIHandle, const uint8_t* Data, size_t DataLength );
bool spi_master_write_command(SSD1306_t * dev, uint8_t Command );
bool spi_master_write_data(SSD1306_t * dev, const uint8_t* Data, size_t DataLength );
void spi_init(SSD1306_t * dev, int width, int height);
void spi_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width);
void spi_contrast(SSD1306_t * dev, int contrast);
void spi_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll);
#endif /* MAIN_SSD1306_H_ */
#ifdef __cplusplus
}
#endif

253
main/ssd1306_i2c.c Normal file
View File

@@ -0,0 +1,253 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
#include "esp_log.h"
#include "ssd1306.h"
#define tag "SSD1306"
#define I2C_NUM I2C_NUM_0
//#define I2C_NUM I2C_NUM_1
#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency. no higher than 1MHz for now */
void i2c_master_init(SSD1306_t * dev, int16_t sda, int16_t scl, int16_t reset)
{
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = sda,
.scl_io_num = scl,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ
};
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM, &i2c_config));
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM, I2C_MODE_MASTER, 0, 0, 0));
if (reset >= 0) {
//gpio_pad_select_gpio(reset);
gpio_reset_pin(reset);
gpio_set_direction(reset, GPIO_MODE_OUTPUT);
gpio_set_level(reset, 0);
vTaskDelay(50 / portTICK_PERIOD_MS);
gpio_set_level(reset, 1);
}
dev->_address = I2CAddress;
dev->_flip = false;
}
void i2c_init(SSD1306_t * dev, int width, int height) {
dev->_width = width;
dev->_height = height;
dev->_pages = 8;
if (dev->_height == 32) dev->_pages = 4;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->_address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true);
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_OFF, true); // AE
i2c_master_write_byte(cmd, OLED_CMD_SET_MUX_RATIO, true); // A8
if (dev->_height == 64) i2c_master_write_byte(cmd, 0x3F, true);
if (dev->_height == 32) i2c_master_write_byte(cmd, 0x1F, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_DISPLAY_OFFSET, true); // D3
i2c_master_write_byte(cmd, 0x00, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_DATA_STREAM, true); // 40
//i2c_master_write_byte(cmd, OLED_CMD_SET_SEGMENT_REMAP, true); // A1
if (dev->_flip) {
i2c_master_write_byte(cmd, OLED_CMD_SET_SEGMENT_REMAP_0, true); // A0
} else {
i2c_master_write_byte(cmd, OLED_CMD_SET_SEGMENT_REMAP_1, true); // A1
}
i2c_master_write_byte(cmd, OLED_CMD_SET_COM_SCAN_MODE, true); // C8
i2c_master_write_byte(cmd, OLED_CMD_SET_DISPLAY_CLK_DIV, true); // D5
i2c_master_write_byte(cmd, 0x80, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_COM_PIN_MAP, true); // DA
if (dev->_height == 64) i2c_master_write_byte(cmd, 0x12, true);
if (dev->_height == 32) i2c_master_write_byte(cmd, 0x02, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_CONTRAST, true); // 81
i2c_master_write_byte(cmd, 0xFF, true);
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_RAM, true); // A4
i2c_master_write_byte(cmd, OLED_CMD_SET_VCOMH_DESELCT, true); // DB
i2c_master_write_byte(cmd, 0x40, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_MEMORY_ADDR_MODE, true); // 20
//i2c_master_write_byte(cmd, OLED_CMD_SET_HORI_ADDR_MODE, true); // 00
i2c_master_write_byte(cmd, OLED_CMD_SET_PAGE_ADDR_MODE, true); // 02
// Set Lower Column Start Address for Page Addressing Mode
i2c_master_write_byte(cmd, 0x00, true);
// Set Higher Column Start Address for Page Addressing Mode
i2c_master_write_byte(cmd, 0x10, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_CHARGE_PUMP, true); // 8D
i2c_master_write_byte(cmd, 0x14, true);
i2c_master_write_byte(cmd, OLED_CMD_DEACTIVE_SCROLL, true); // 2E
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_NORMAL, true); // A6
i2c_master_write_byte(cmd, OLED_CMD_DISPLAY_ON, true); // AF
i2c_master_stop(cmd);
esp_err_t espRc = i2c_master_cmd_begin(I2C_NUM, cmd, 10/portTICK_PERIOD_MS);
if (espRc == ESP_OK) {
ESP_LOGI(tag, "OLED configured successfully");
} else {
ESP_LOGE(tag, "OLED configuration failed. code: 0x%.2X", espRc);
}
i2c_cmd_link_delete(cmd);
}
void i2c_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width) {
i2c_cmd_handle_t cmd;
if (page >= dev->_pages) return;
if (seg >= dev->_width) return;
int _seg = seg + CONFIG_OFFSETX;
uint8_t columLow = _seg & 0x0F;
uint8_t columHigh = (_seg >> 4) & 0x0F;
int _page = page;
if (dev->_flip) {
_page = (dev->_pages - page) - 1;
}
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->_address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true);
// Set Lower Column Start Address for Page Addressing Mode
i2c_master_write_byte(cmd, (0x00 + columLow), true);
// Set Higher Column Start Address for Page Addressing Mode
i2c_master_write_byte(cmd, (0x10 + columHigh), true);
// Set Page Start Address for Page Addressing Mode
i2c_master_write_byte(cmd, 0xB0 | _page, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM, cmd, 10/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->_address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_DATA_STREAM, true);
i2c_master_write(cmd, images, width, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM, cmd, 10/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
}
void i2c_contrast(SSD1306_t * dev, int contrast) {
i2c_cmd_handle_t cmd;
int _contrast = contrast;
if (contrast < 0x0) _contrast = 0;
if (contrast > 0xFF) _contrast = 0xFF;
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->_address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true);
i2c_master_write_byte(cmd, OLED_CMD_SET_CONTRAST, true); // 81
i2c_master_write_byte(cmd, _contrast, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_NUM, cmd, 10/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
}
void i2c_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll) {
esp_err_t espRc;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->_address << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, OLED_CONTROL_BYTE_CMD_STREAM, true);
if (scroll == SCROLL_RIGHT) {
i2c_master_write_byte(cmd, OLED_CMD_HORIZONTAL_RIGHT, true); // 26
i2c_master_write_byte(cmd, 0x00, true); // Dummy byte
i2c_master_write_byte(cmd, 0x00, true); // Define start page address
i2c_master_write_byte(cmd, 0x07, true); // Frame frequency
i2c_master_write_byte(cmd, 0x07, true); // Define end page address
i2c_master_write_byte(cmd, 0x00, true); //
i2c_master_write_byte(cmd, 0xFF, true); //
i2c_master_write_byte(cmd, OLED_CMD_ACTIVE_SCROLL, true); // 2F
}
if (scroll == SCROLL_LEFT) {
i2c_master_write_byte(cmd, OLED_CMD_HORIZONTAL_LEFT, true); // 27
i2c_master_write_byte(cmd, 0x00, true); // Dummy byte
i2c_master_write_byte(cmd, 0x00, true); // Define start page address
i2c_master_write_byte(cmd, 0x07, true); // Frame frequency
i2c_master_write_byte(cmd, 0x07, true); // Define end page address
i2c_master_write_byte(cmd, 0x00, true); //
i2c_master_write_byte(cmd, 0xFF, true); //
i2c_master_write_byte(cmd, OLED_CMD_ACTIVE_SCROLL, true); // 2F
}
if (scroll == SCROLL_DOWN) {
i2c_master_write_byte(cmd, OLED_CMD_CONTINUOUS_SCROLL, true); // 29
i2c_master_write_byte(cmd, 0x00, true); // Dummy byte
i2c_master_write_byte(cmd, 0x00, true); // Define start page address
i2c_master_write_byte(cmd, 0x07, true); // Frame frequency
//i2c_master_write_byte(cmd, 0x01, true); // Define end page address
i2c_master_write_byte(cmd, 0x00, true); // Define end page address
i2c_master_write_byte(cmd, 0x3F, true); // Vertical scrolling offset
i2c_master_write_byte(cmd, OLED_CMD_VERTICAL, true); // A3
i2c_master_write_byte(cmd, 0x00, true);
if (dev->_height == 64)
//i2c_master_write_byte(cmd, 0x7F, true);
i2c_master_write_byte(cmd, 0x40, true);
if (dev->_height == 32)
i2c_master_write_byte(cmd, 0x20, true);
i2c_master_write_byte(cmd, OLED_CMD_ACTIVE_SCROLL, true); // 2F
}
if (scroll == SCROLL_UP) {
i2c_master_write_byte(cmd, OLED_CMD_CONTINUOUS_SCROLL, true); // 29
i2c_master_write_byte(cmd, 0x00, true); // Dummy byte
i2c_master_write_byte(cmd, 0x00, true); // Define start page address
i2c_master_write_byte(cmd, 0x07, true); // Frame frequency
//i2c_master_write_byte(cmd, 0x01, true); // Define end page address
i2c_master_write_byte(cmd, 0x00, true); // Define end page address
i2c_master_write_byte(cmd, 0x01, true); // Vertical scrolling offset
i2c_master_write_byte(cmd, OLED_CMD_VERTICAL, true); // A3
i2c_master_write_byte(cmd, 0x00, true);
if (dev->_height == 64)
//i2c_master_write_byte(cmd, 0x7F, true);
i2c_master_write_byte(cmd, 0x40, true);
if (dev->_height == 32)
i2c_master_write_byte(cmd, 0x20, true);
i2c_master_write_byte(cmd, OLED_CMD_ACTIVE_SCROLL, true); // 2F
}
if (scroll == SCROLL_STOP) {
i2c_master_write_byte(cmd, OLED_CMD_DEACTIVE_SCROLL, true); // 2E
}
i2c_master_stop(cmd);
espRc = i2c_master_cmd_begin(I2C_NUM, cmd, 10/portTICK_PERIOD_MS);
if (espRc == ESP_OK) {
ESP_LOGD(tag, "Scroll command succeeded");
} else {
ESP_LOGE(tag, "Scroll command failed. code: 0x%.2X", espRc);
}
i2c_cmd_link_delete(cmd);
}
#ifdef __cplusplus
}
#endif

253
main/ssd1306_spi.c Normal file
View File

@@ -0,0 +1,253 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "ssd1306.h"
#define tag "SSD1306"
#ifdef CONFIG_IDF_TARGET_ESP32
#define LCD_HOST HSPI_HOST
#elif defined CONFIG_IDF_TARGET_ESP32S2
#define LCD_HOST SPI2_HOST
#elif defined CONFIG_IDF_TARGET_ESP32C3
#define LCD_HOST SPI2_HOST
#endif
static const int SPI_Command_Mode = 0;
static const int SPI_Data_Mode = 1;
static const int SPI_Frequency = 1000000;
void spi_master_init(SSD1306_t * dev, int16_t GPIO_MOSI, int16_t GPIO_SCLK, int16_t GPIO_CS, int16_t GPIO_DC, int16_t GPIO_RESET)
{
esp_err_t ret;
//gpio_pad_select_gpio( GPIO_CS );
gpio_reset_pin( GPIO_CS );
gpio_set_direction( GPIO_CS, GPIO_MODE_OUTPUT );
gpio_set_level( GPIO_CS, 0 );
//gpio_pad_select_gpio( GPIO_DC );
gpio_reset_pin( GPIO_DC );
gpio_set_direction( GPIO_DC, GPIO_MODE_OUTPUT );
gpio_set_level( GPIO_DC, 0 );
if ( GPIO_RESET >= 0 ) {
//gpio_pad_select_gpio( GPIO_RESET );
gpio_reset_pin( GPIO_RESET );
gpio_set_direction( GPIO_RESET, GPIO_MODE_OUTPUT );
gpio_set_level( GPIO_RESET, 0 );
vTaskDelay( pdMS_TO_TICKS( 100 ) );
gpio_set_level( GPIO_RESET, 1 );
}
spi_bus_config_t spi_bus_config = {
.mosi_io_num = GPIO_MOSI,
.miso_io_num = -1,
.sclk_io_num = GPIO_SCLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 0,
.flags = 0
};
ret = spi_bus_initialize( LCD_HOST, &spi_bus_config, SPI_DMA_CH_AUTO );
ESP_LOGI(tag, "spi_bus_initialize=%d",ret);
assert(ret==ESP_OK);
spi_device_interface_config_t devcfg;
memset( &devcfg, 0, sizeof( spi_device_interface_config_t ) );
devcfg.clock_speed_hz = SPI_Frequency;
devcfg.spics_io_num = GPIO_CS;
devcfg.queue_size = 1;
spi_device_handle_t handle;
ret = spi_bus_add_device( LCD_HOST, &devcfg, &handle);
ESP_LOGI(tag, "spi_bus_add_device=%d",ret);
assert(ret==ESP_OK);
dev->_dc = GPIO_DC;
dev->_SPIHandle = handle;
dev->_address = SPIAddress;
dev->_flip = false;
}
bool spi_master_write_byte(spi_device_handle_t SPIHandle, const uint8_t* Data, size_t DataLength )
{
spi_transaction_t SPITransaction;
if ( DataLength > 0 ) {
memset( &SPITransaction, 0, sizeof( spi_transaction_t ) );
SPITransaction.length = DataLength * 8;
SPITransaction.tx_buffer = Data;
spi_device_transmit( SPIHandle, &SPITransaction );
}
return true;
}
bool spi_master_write_command(SSD1306_t * dev, uint8_t Command )
{
static uint8_t CommandByte = 0;
CommandByte = Command;
gpio_set_level( dev->_dc, SPI_Command_Mode );
return spi_master_write_byte( dev->_SPIHandle, &CommandByte, 1 );
}
bool spi_master_write_data(SSD1306_t * dev, const uint8_t* Data, size_t DataLength )
{
gpio_set_level( dev->_dc, SPI_Data_Mode );
return spi_master_write_byte( dev->_SPIHandle, Data, DataLength );
}
void spi_init(SSD1306_t * dev, int width, int height)
{
dev->_width = width;
dev->_height = height;
dev->_pages = 8;
if (dev->_height == 32) dev->_pages = 4;
spi_master_write_command(dev, OLED_CMD_DISPLAY_OFF); // AE
spi_master_write_command(dev, OLED_CMD_SET_MUX_RATIO); // A8
if (dev->_height == 64) spi_master_write_command(dev, 0x3F);
if (dev->_height == 32) spi_master_write_command(dev, 0x1F);
spi_master_write_command(dev, OLED_CMD_SET_DISPLAY_OFFSET); // D3
spi_master_write_command(dev, 0x00);
spi_master_write_command(dev, OLED_CONTROL_BYTE_DATA_STREAM); // 40
if (dev->_flip) {
spi_master_write_command(dev, OLED_CMD_SET_SEGMENT_REMAP_0); // A0
} else {
spi_master_write_command(dev, OLED_CMD_SET_SEGMENT_REMAP_1); // A1
}
//spi_master_write_command(dev, OLED_CMD_SET_SEGMENT_REMAP); // A1
spi_master_write_command(dev, OLED_CMD_SET_COM_SCAN_MODE); // C8
spi_master_write_command(dev, OLED_CMD_SET_DISPLAY_CLK_DIV); // D5
spi_master_write_command(dev, 0x80);
spi_master_write_command(dev, OLED_CMD_SET_COM_PIN_MAP); // DA
if (dev->_height == 64) spi_master_write_command(dev, 0x12);
if (dev->_height == 32) spi_master_write_command(dev, 0x02);
spi_master_write_command(dev, OLED_CMD_SET_CONTRAST); // 81
spi_master_write_command(dev, 0xFF);
spi_master_write_command(dev, OLED_CMD_DISPLAY_RAM); // A4
spi_master_write_command(dev, OLED_CMD_SET_VCOMH_DESELCT); // DB
spi_master_write_command(dev, 0x40);
spi_master_write_command(dev, OLED_CMD_SET_MEMORY_ADDR_MODE); // 20
//spi_master_write_command(dev, OLED_CMD_SET_HORI_ADDR_MODE); // 00
spi_master_write_command(dev, OLED_CMD_SET_PAGE_ADDR_MODE); // 02
// Set Lower Column Start Address for Page Addressing Mode
spi_master_write_command(dev, 0x00);
// Set Higher Column Start Address for Page Addressing Mode
spi_master_write_command(dev, 0x10);
spi_master_write_command(dev, OLED_CMD_SET_CHARGE_PUMP); // 8D
spi_master_write_command(dev, 0x14);
spi_master_write_command(dev, OLED_CMD_DEACTIVE_SCROLL); // 2E
spi_master_write_command(dev, OLED_CMD_DISPLAY_NORMAL); // A6
spi_master_write_command(dev, OLED_CMD_DISPLAY_ON); // AF
}
void spi_display_image(SSD1306_t * dev, int page, int seg, uint8_t * images, int width)
{
if (page >= dev->_pages) return;
if (seg >= dev->_width) return;
int _seg = seg + CONFIG_OFFSETX;
uint8_t columLow = _seg & 0x0F;
uint8_t columHigh = (_seg >> 4) & 0x0F;
int _page = page;
if (dev->_flip) {
_page = (dev->_pages - page) - 1;
}
// Set Lower Column Start Address for Page Addressing Mode
spi_master_write_command(dev, (0x00 + columLow));
// Set Higher Column Start Address for Page Addressing Mode
spi_master_write_command(dev, (0x10 + columHigh));
// Set Page Start Address for Page Addressing Mode
spi_master_write_command(dev, 0xB0 | _page);
spi_master_write_data(dev, images, width);
}
void spi_contrast(SSD1306_t * dev, int contrast) {
int _contrast = contrast;
if (contrast < 0x0) _contrast = 0;
if (contrast > 0xFF) _contrast = 0xFF;
spi_master_write_command(dev, OLED_CMD_SET_CONTRAST); // 81
spi_master_write_command(dev, _contrast);
}
void spi_hardware_scroll(SSD1306_t * dev, ssd1306_scroll_type_t scroll)
{
if (scroll == SCROLL_RIGHT) {
spi_master_write_command(dev, OLED_CMD_HORIZONTAL_RIGHT); // 26
spi_master_write_command(dev, 0x00); // Dummy byte
spi_master_write_command(dev, 0x00); // Define start page address
spi_master_write_command(dev, 0x07); // Frame frequency
spi_master_write_command(dev, 0x07); // Define end page address
spi_master_write_command(dev, 0x00); //
spi_master_write_command(dev, 0xFF); //
spi_master_write_command(dev, OLED_CMD_ACTIVE_SCROLL); // 2F
}
if (scroll == SCROLL_LEFT) {
spi_master_write_command(dev, OLED_CMD_HORIZONTAL_LEFT); // 27
spi_master_write_command(dev, 0x00); // Dummy byte
spi_master_write_command(dev, 0x00); // Define start page address
spi_master_write_command(dev, 0x07); // Frame frequency
spi_master_write_command(dev, 0x07); // Define end page address
spi_master_write_command(dev, 0x00); //
spi_master_write_command(dev, 0xFF); //
spi_master_write_command(dev, OLED_CMD_ACTIVE_SCROLL); // 2F
}
if (scroll == SCROLL_DOWN) {
spi_master_write_command(dev, OLED_CMD_CONTINUOUS_SCROLL); // 29
spi_master_write_command(dev, 0x00); // Dummy byte
spi_master_write_command(dev, 0x00); // Define start page address
spi_master_write_command(dev, 0x07); // Frame frequency
//spi_master_write_command(dev, 0x01); // Define end page address
spi_master_write_command(dev, 0x00); // Define end page address
spi_master_write_command(dev, 0x3F); // Vertical scrolling offset
spi_master_write_command(dev, OLED_CMD_VERTICAL); // A3
spi_master_write_command(dev, 0x00);
if (dev->_height == 64)
spi_master_write_command(dev, 0x40);
if (dev->_height == 32)
spi_master_write_command(dev, 0x20);
spi_master_write_command(dev, OLED_CMD_ACTIVE_SCROLL); // 2F
}
if (scroll == SCROLL_UP) {
spi_master_write_command(dev, OLED_CMD_CONTINUOUS_SCROLL); // 29
spi_master_write_command(dev, 0x00); // Dummy byte
spi_master_write_command(dev, 0x00); // Define start page address
spi_master_write_command(dev, 0x07); // Frame frequency
//spi_master_write_command(dev, 0x01); // Define end page address
spi_master_write_command(dev, 0x00); // Define end page address
spi_master_write_command(dev, 0x01); // Vertical scrolling offset
spi_master_write_command(dev, OLED_CMD_VERTICAL); // A3
spi_master_write_command(dev, 0x00);
if (dev->_height == 64)
spi_master_write_command(dev, 0x40);
if (dev->_height == 32)
spi_master_write_command(dev, 0x20);
spi_master_write_command(dev, OLED_CMD_ACTIVE_SCROLL); // 2F
}
if (scroll == SCROLL_STOP) {
spi_master_write_command(dev, OLED_CMD_DEACTIVE_SCROLL); // 2E
}
}

1481
sdkconfig Normal file

File diff suppressed because it is too large Load Diff