User Tools

Site Tools


module:micropython:esp32displaykey:esp32displaykey

This is an old revision of the document!


MicroPython: Esp32DisplayKey

Übersicht

KeyPad4x4

  • 4×4 KeyPad: 8x IOPin: 4x Input(34, 35, 36, 39) und 4x Output(12, 13, 14, 27)
  • Keyboard-KeyCode-Buffer: SIZE_KEYCODEBUFFER = 5
  • minimale Zeitauflösung Erkennung einer Taste: TIME_KEYPRESSED = 0.1
  • minimale Zeit zur Erkennung von Tastenwiederholungen: ZeitTIME_KEYREPETITION = 0.5
  • Eigener Thread mit permanenter Tastatur-Abfrage
  • MainLoop: periodische Abfrage GetKeyCode()
  • Keys beim IT01:
 Col  :  0       1       2       3
Row 0 : [1]<24> [2]<40> [3]<72> [A]<136>
Row 1 : [4]<20> [5]<36> [6]<68> [B]<132>
Row 2 : [7]<18> [8]<34> [9]<66> [C]<130>
Row 3 : [*]<17> [0]<33> [#]<65> [D]<129>

LCDisplayI2C

  • Ansteuerung über I2C (PCF8574)
  • Eigener Thread zur Verhinderung von Blockaden ohne time-sleep in MainLoop
  • permanente Ausgabe von Zeilen/Spalten in DisplayThread

Version

Beschreibung

Ausgabe MicroPython-Terminal: Esp32CheckDisplayKey.py

  • Ausgabe der KeyCodes (mit Wiederholungen)
>>>
>>> *** CheckEsp32DisplayKey: begin
66
66
66
34
34
40
40
65
65
65
68
68
136
130
129
40
*** CheckEsp32DisplayKey: end
>>>

Library-Module: Define.py

#
#-----------------------------------------
#   Uart0 / 1 / 2
#-----------------------------------------
ID_UART0     =  0
PIN_UART0_TX =  1 # USB
PIN_UART0_RX =  3 # USB
#
ID_UART1     =  1
PIN_UART1_RX = 25 #  9 - not usable 
PIN_UART1_TX = 26 # 10 - not usable 
#
ID_UART2     =  2
PIN_UART2_RX = 16
PIN_UART2_TX = 17
#
#-----------------------------------------
#   LCDisplayI2C
#-----------------------------------------
I2CADDRESS_LCDISPLAY_T  = 0x27
I2CADDRESS_LCDISPLAY_A  = 0x3F
#
LCDISPLAY_COUNT_ROWS    = 4
LCDISPLAY_COUNT_COLUMNS = 20
#
#-----------------------------------------
#   Keyboard4x4
#-----------------------------------------
PIN_KEYBOARD_IN0 = 39
PIN_KEYBOARD_IN1 = 36
PIN_KEYBOARD_IN2 = 34
PIN_KEYBOARD_IN3 = 35
PIN_KEYBOARD_OUT0 = 13
PIN_KEYBOARD_OUT1 = 12
PIN_KEYBOARD_OUT2 = 14
PIN_KEYBOARD_OUT3 = 27
#
SIZE_KEYCODEBUFFER = 5
TIME_KEYPRESSED = 0.1
TIME_KEYREPETITION = 0.5
#

Main-Module: Esp32CheckDisplayKey.py

#
import time
import machine
#
import Define as DEF
import Thread as THR
import LCDisplayI2C as LCD
import ThreadDisplay as TDP
import Keyboard4x4 as KBD
#
#
#------------------------------------------------------------------
#   Main
#------------------------------------------------------------------
if ('__main__' == __name__):
    print('*** CheckEsp32DisplayKey: begin')
    #
    # Display
    I2CDisplay = machine.SoftI2C(scl=machine.Pin(22), sda=machine.Pin(21), freq=2000000) 
    LCDisplay = LCD.CLCDisplayI2C(I2CDisplay, DEF.I2CADDRESS_LCDISPLAY_T, \
                                  DEF.LCDISPLAY_COUNT_ROWS, DEF.LCDISPLAY_COUNT_COLUMNS)
    ThreadDisplay = TDP.CThreadDisplay(I2CDisplay, LCDisplay)
    # Keyboard
    Keyboard = KBD.CKeyboard4x4(DEF.PIN_KEYBOARD_IN0, DEF.PIN_KEYBOARD_IN1, 
                                DEF.PIN_KEYBOARD_IN2, DEF.PIN_KEYBOARD_IN3, 
                                DEF.PIN_KEYBOARD_OUT0, DEF.PIN_KEYBOARD_OUT1, 
                                DEF.PIN_KEYBOARD_OUT2, DEF.PIN_KEYBOARD_OUT3)
    # Open
    ThreadDisplay.Open()
    Keyboard.Open()
    #
    # MainLoop ....
    for I in range(0, 10000):
        KC = Keyboard.GetKeyCode()
        if (0 < KC):
            print(KC)
        time.sleep(0.001)
    #
    # Close
    Keyboard.Close()
    ThreadDisplay.Close()
    #
    print('*** CheckEsp32DisplayKey: end')
    #
#

Library-Module: Keyboard4x4.py

#
import time
from machine import Pin
#
import Define as DEF
import Thread as THR
#
class CKeyboard4x4():
    #-----------------------------------------------------------------
    #   Constructor
    #-----------------------------------------------------------------
    def __init__(self, \
                 pinin0, pinin1, pinin2, pinin3, \
                 pinout0, pinout1, pinout2, pinout3):
        #
        self.CountRepeat = 3
        self.KeySleep = 0.001
        #
        self.PinIn = []
        self.PinIn.append(Pin(pinin0, mode=Pin.IN, pull=Pin.PULL_DOWN))
        self.PinIn.append(Pin(pinin1, mode=Pin.IN, pull=Pin.PULL_DOWN))
        self.PinIn.append(Pin(pinin2, mode=Pin.IN, pull=Pin.PULL_DOWN))
        self.PinIn.append(Pin(pinin3, mode=Pin.IN, pull=Pin.PULL_DOWN))
        #
        self.PinOut = []
        self.PinOut.append(Pin(pinout0, mode=Pin.OUT, pull=Pin.PULL_DOWN))
        self.PinOut.append(Pin(pinout1, mode=Pin.OUT, pull=Pin.PULL_DOWN))
        self.PinOut.append(Pin(pinout2, mode=Pin.OUT, pull=Pin.PULL_DOWN))
        self.PinOut.append(Pin(pinout3, mode=Pin.OUT, pull=Pin.PULL_DOWN))
        #
        self.Thread = THR.CThread(self.CBOnStart, self.CBOnBusy, self.CBOnAbort, self.CBOnEnd)
        #
        self.KeyCodes = []
        return
    #
    #-----------------------------------------------------------------
    #   Callback
    #-----------------------------------------------------------------
    def CBOnStart(self, thread):
        return
    def CBOnBusy(self, thread):
        while (THR.stBusy == self.Thread.State):
            KeyCode0 = 0x00
            KeyCode1 = 0x00
            KeyCode2 = 0x00
            KeyCode3 = 0x00
            #-----------------------------------------
            # Row0
            #-----------------------------------------
            self.PinOut[0].on()
            self.PinOut[1].off()
            self.PinOut[2].off()
            self.PinOut[3].off()
            CS0 = 0
            CS1 = 0
            CS2 = 0
            CS3 = 0
            for SI in range(0, self.CountRepeat):
                CS0 += self.PinIn[0].value()
                CS1 += self.PinIn[1].value()
                CS2 += self.PinIn[2].value()
                CS3 += self.PinIn[3].value()
                time.sleep(self.KeySleep)
            if (self.CountRepeat <= CS0):
                KeyCode0 = 0x11
            if (self.CountRepeat <= CS1):
                KeyCode0 = 0x21
            if (self.CountRepeat <= CS2):
                KeyCode0 = 0x41
            if (self.CountRepeat <= CS3):
                KeyCode0 = 0x81
            #-----------------------------------------
            # Row1
            #-----------------------------------------
            self.PinOut[0].off()
            self.PinOut[1].on()
            self.PinOut[2].off()
            self.PinOut[3].off()
            CS0 = 0
            CS1 = 0
            CS2 = 0
            CS3 = 0
            for SI in range(0, self.CountRepeat):
                CS0 += self.PinIn[0].value()
                CS1 += self.PinIn[1].value()
                CS2 += self.PinIn[2].value()
                CS3 += self.PinIn[3].value()
                time.sleep(self.KeySleep)
            if (self.CountRepeat <= CS0):
                KeyCode1 = 0x12
            if (self.CountRepeat <= CS1):
                KeyCode1 = 0x22
            if (self.CountRepeat <= CS2):
                KeyCode1 = 0x42
            if (self.CountRepeat <= CS3):
                KeyCode1 = 0x82
            #-----------------------------------------
            # Row2
            #-----------------------------------------
            self.PinOut[0].off()
            self.PinOut[1].off()
            self.PinOut[2].on()
            self.PinOut[3].off()
            CS0 = 0
            CS1 = 0
            CS2 = 0
            CS3 = 0
            for SI in range(0, self.CountRepeat):
                CS0 += self.PinIn[0].value()
                CS1 += self.PinIn[1].value()
                CS2 += self.PinIn[2].value()
                CS3 += self.PinIn[3].value()
                time.sleep(self.KeySleep)
            if (self.CountRepeat <= CS0):
                KeyCode2 = 0x14
            if (self.CountRepeat <= CS1):
                KeyCode2 = 0x24
            if (self.CountRepeat <= CS2):
                KeyCode2 = 0x44
            if (self.CountRepeat <= CS3):
                KeyCode2 = 0x84
            #-----------------------------------------
            # Row3
            #-----------------------------------------
            self.PinOut[0].off()
            self.PinOut[1].off()
            self.PinOut[2].off()
            self.PinOut[3].on()
            CS0 = 0
            CS1 = 0
            CS2 = 0
            CS3 = 0
            for SI in range(0, self.CountRepeat):
                CS0 += self.PinIn[0].value()
                CS1 += self.PinIn[1].value()
                CS2 += self.PinIn[2].value()
                CS3 += self.PinIn[3].value()
                time.sleep(self.KeySleep)
            if (self.CountRepeat <= CS0):
                KeyCode3 = 0x18
            if (self.CountRepeat <= CS1):
                KeyCode3 = 0x28
            if (self.CountRepeat <= CS2):
                KeyCode3 = 0x48
            if (self.CountRepeat <= CS3):
                KeyCode3 = 0x88
            #
            if (0 < KeyCode0):
                self.KeyCodes.append(KeyCode0)
            if (0 < KeyCode1):
                self.KeyCodes.append(KeyCode1)
            if (0 < KeyCode2):
                self.KeyCodes.append(KeyCode2)
            if (0 < KeyCode3):
                self.KeyCodes.append(KeyCode3)
            #
            while (DEF.SIZE_KEYCODEBUFFER < len(self.KeyCodes)):
                self.KeyCodes.pop(0)
            #
            if (0 < len(self.KeyCodes)):
                time.sleep(DEF.TIME_KEYREPETITION)
            else:
                time.sleep(DEF.TIME_KEYPRESSED)
        return
    def CBOnAbort(self, thread):
        return
    def CBOnEnd(self, thread):
        return
    #-----------------------------------------------------------------
    #   Manager
    #-----------------------------------------------------------------
    def Open(self):
        self.Thread.Start()
        return 
    def Close(self):
        self.Thread.Abort()
        return 
    def GetKeyCode(self):
        if (0 < len(self.KeyCodes)):
            KC = self.KeyCodes.pop(0)
            return KC
        return 0
#
#------------------------------------------------------------------
#   Check Library
#------------------------------------------------------------------
if '__main__' == __name__:
    print('*** Check Keyboard4x4: begin')
    #
    Keyboard = CKeyboard4x4(DEF.PIN_KEYBOARD_IN0, DEF.PIN_KEYBOARD_IN1, 
                            DEF.PIN_KEYBOARD_IN2, DEF.PIN_KEYBOARD_IN3, 
                            DEF.PIN_KEYBOARD_OUT0, DEF.PIN_KEYBOARD_OUT1, 
                            DEF.PIN_KEYBOARD_OUT2, DEF.PIN_KEYBOARD_OUT3)
    # Open
    Keyboard.Open()
    # MainLoop ....
    for I in range(0, 10000):
        KC = Keyboard.GetKeyCode()
        if (0 < KC):
            print(KC)
        time.sleep(0.001)
    # Close
    Keyboard.Close()
    #
    print('*** Check Keyboard4x4: end')
    #
#

Library-Module: Uart.py

 

Library-Module: LCDisplayI2C.py

#
"""Implements a HD44780 character LCD connected via PCF8574 on I2C"""
#
from LCDisplay import CLCDisplay
from time import sleep_ms
#
# The PCF8574 has a jumper selectable address: 0x20 - 0x27
DEFAULT_I2C_ADDR = 0x27
#
MASK_RS = 0x01
MASK_RW = 0x02
MASK_E = 0x04
SHIFT_BACKLIGHT = 3
SHIFT_DATA = 4
#
class CLCDisplayI2C(CLCDisplay):
    #
    def __init__(self, i2c, i2caddress, countrows, countcolumns):
        self.I2C = i2c
        self.I2CAddress = i2caddress
        self.I2C.writeto(self.I2CAddress, bytearray([0]))
        sleep_ms(20)   # Allow LCD time to powerup
        # Send reset 3 times
        self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
        sleep_ms(5)    # need to delay at least 4.1 msec
        self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
        sleep_ms(1)
        self.hal_write_init_nibble(self.LCD_FUNCTION_RESET)
        sleep_ms(1)
        # Put LCD into 4 bit mode
        self.hal_write_init_nibble(self.LCD_FUNCTION)
        sleep_ms(1)
        CLCDisplay.__init__(self, countrows, countcolumns)
        cmd = self.LCD_FUNCTION
        if countrows > 1:
            cmd |= self.LCD_FUNCTION_2LINES
        self.hal_write_command(cmd)
    #
    def hal_write_init_nibble(self, nibble):
        byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA
        self.I2C.writeto(self.I2CAddress, bytearray([byte | MASK_E]))
        self.I2C.writeto(self.I2CAddress, bytearray([byte]))
    #
    def hal_backlight_on(self):
        self.I2C.writeto(self.I2CAddress, bytearray([1 << SHIFT_BACKLIGHT]))
    #
    def hal_backlight_off(self):
        self.I2C.writeto(self.I2CAddress, bytearray([0]))
    #
    def hal_write_command(self, cmd):
        """Data is latched on the falling edge of E."""
        byte = ((self.Backlight << SHIFT_BACKLIGHT) | (((cmd >> 4) & 0x0f) << SHIFT_DATA))
        self.I2C.writeto(self.I2CAddress, bytearray([byte | MASK_E]))
        self.I2C.writeto(self.I2CAddress, bytearray([byte]))
        byte = ((self.Backlight << SHIFT_BACKLIGHT) | ((cmd & 0x0f) << SHIFT_DATA))
        self.I2C.writeto(self.I2CAddress, bytearray([byte | MASK_E]))
        self.I2C.writeto(self.I2CAddress, bytearray([byte]))
        if cmd <= 3: # The home and clear commands require a worst case delay of 4.1 msec
            sleep_ms(5)
    #
    def hal_write_data(self, data):
        byte = (MASK_RS | (self.Backlight << SHIFT_BACKLIGHT) | (((data >> 4) & 0x0f) << SHIFT_DATA))
        self.I2C.writeto(self.I2CAddress, bytearray([byte | MASK_E]))
        self.I2C.writeto(self.I2CAddress, bytearray([byte]))
        byte = (MASK_RS | (self.Backlight << SHIFT_BACKLIGHT) | ((data & 0x0f) << SHIFT_DATA))
        self.I2C.writeto(self.I2CAddress, bytearray([byte | MASK_E]))
        self.I2C.writeto(self.I2CAddress, bytearray([byte]))
    #
#    

Library-Module: Thread.py

import time
import _thread as THR
#
# States - EStateThread :
stIdle = 0
stBusy = 1
stEnd = 2
#
class CThread():
    #
    def __init__(self, onstart, onbusy, onabort, onend):
        self.State = stIdle        
        self.Thread = None
        self.OnStart = onstart
        self.OnBusy = onbusy
        self.OnAbort = onabort
        self.OnEnd = onend
        return
    #
    def Start(self):
        self.State = stBusy
        if (None != self.OnStart):
            self.OnStart(self)
        self.ThreadID = THR.start_new_thread(self.CBOnExecute, [self])
        return    
    #
    def Abort(self):
        self.State = stEnd
        if (None != self.OnAbort):
            self.OnAbort(self)
        return
    #
    def CBOnExecute(self, thread):
        if (None != self.OnBusy):
            self.OnBusy(self)
        if (None != self.OnEnd):
            self.OnEnd(self)
        return
    #
#    
#-------------------------------------------------------------
#    Check Library
#-------------------------------------------------------------
def OnStart(thread):
    return
def OnBusy(thread):
    while (stBusy == thread.State):
        print('.')
        time.sleep(0.5)
    return
def OnAbort(thread):
    return
def OnEnd(thread):
    return
#
if ('__main__' == __name__):
    print('*** Check Thread: begin')        
    #
    Thread = CThread(OnStart, OnBusy, OnAbort, OnEnd)
    Thread.Start()
    time.sleep(5.0)
    Thread.Abort()
    #
    print('*** Check Thread: end')
#

Library-Module: .py

 

Library-Module: .py

 

Library-Module: .py

 

Entwicklung

211224 : Versuch zur Thread-Entkopplung

  • Multithreading mit LCDisplay in MicroPython zeigt jetzt (hoffentlich) keine Mucken mehr….
  • zweiter Thread zur Tastatur-Überwachung (ohne Blockade)
  • Trick: die CommandExecutionTime-Blockaden des Displays in einen Thread auszulagern !
  • Klasse CDisplay mit eigenem Thread self.Thread und CCommandList(Row, Col, Text)
  • Commands:
    • Clear()
    • Write(R, C, T)

211223 : Basis

  • ganz grosses Problem mit MultiThreading:
    • Keyboard4x4 mit eigenem Key-Erkennungs-Thread
    • Display mit eigenem Periodic-Display-Thread
  • laufen nicht unabhängig voneinander !!! Ganz grosser MIST!!!!

Open Hard- & Software [ DokuWiki WebSites MediaWiki NextCloud ]

module/micropython/esp32displaykey/esp32displaykey.1640437849.txt.gz · Last modified: 2021/12/25 15:10 (external edit)