module:micropython:esp32displaykey:esp32displaykey
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| module:micropython:esp32displaykey:esp32displaykey [2021/12/25 14:04] – [Ausgabe MicroPython-Terminal: Esp32CheckDisplayKey.py] omdevelop | module:micropython:esp32displaykey:esp32displaykey [2022/09/13 11:58] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 18: | Line 18: | ||
| * minimale Zeit zur Erkennung von Tastenwiederholungen: | * minimale Zeit zur Erkennung von Tastenwiederholungen: | ||
| * Eigener Thread mit permanenter Tastatur-Abfrage | * Eigener Thread mit permanenter Tastatur-Abfrage | ||
| + | * Entprellung über TIME_KEYPRESSES | ||
| + | * Tasten-Wiederholung über TIME_KEYREPETITION | ||
| * MainLoop: periodische Abfrage GetKeyCode() | * MainLoop: periodische Abfrage GetKeyCode() | ||
| * Keys beim IT01: | * Keys beim IT01: | ||
| Line 42: | Line 44: | ||
| ===== Version ===== | ===== Version ===== | ||
| - | {{: | + | {{: |
| ===== Beschreibung ===== | ===== Beschreibung ===== | ||
| + | {{: | ||
| ==== Ausgabe MicroPython-Terminal: | ==== Ausgabe MicroPython-Terminal: | ||
| * Ausgabe der KeyCodes (mit Wiederholungen) | * Ausgabe der KeyCodes (mit Wiederholungen) | ||
| Line 70: | Line 73: | ||
| >>> | >>> | ||
| </ | </ | ||
| - | ==== Haupt-Module: Esp32CheckDisplayKey.py ==== | + | |
| + | ==== Library-Module: | ||
| + | <code python> | ||
| + | # | ||
| + | # | ||
| + | # Uart0 / 1 / 2 | ||
| + | # | ||
| + | ID_UART0 | ||
| + | PIN_UART0_TX = 1 # USB | ||
| + | PIN_UART0_RX = 3 # USB | ||
| + | # | ||
| + | ID_UART1 | ||
| + | PIN_UART1_RX = 25 # 9 - not usable | ||
| + | PIN_UART1_TX = 26 # 10 - not usable | ||
| + | # | ||
| + | ID_UART2 | ||
| + | PIN_UART2_RX = 16 | ||
| + | PIN_UART2_TX = 17 | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | I2CADDRESS_LCDISPLAY_T | ||
| + | I2CADDRESS_LCDISPLAY_A | ||
| + | # | ||
| + | LCDISPLAY_COUNT_ROWS | ||
| + | LCDISPLAY_COUNT_COLUMNS = 20 | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | 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 ==== | ||
| <code python> | <code python> | ||
| # | # | ||
| Line 120: | Line 170: | ||
| - | ==== Main-Program: | + | ==== Library-Module: |
| - | <code python> | + | |
| - | </ | + | |
| - | + | ||
| - | ==== Library-Module: | + | |
| - | <code python> | + | |
| - | </ | + | |
| - | + | ||
| - | + | ||
| - | ==== Library-Module: | + | |
| - | <code python> | + | |
| - | </ | + | |
| - | + | ||
| - | ==== Library-Module: | + | |
| - | <code python> | + | |
| - | </ | + | |
| - | + | ||
| - | ==== Library-Module: | + | |
| - | <code python> | + | |
| - | </ | + | |
| - | + | ||
| - | ==== Library-Module: | + | |
| - | <code python> | + | |
| - | </ | + | |
| - | + | ||
| - | ==== Library-Module: | + | |
| <code python> | <code python> | ||
| + | # | ||
| + | import time | ||
| + | from machine import Pin | ||
| + | # | ||
| + | import Define as DEF | ||
| + | import Thread as THR | ||
| + | # | ||
| + | class CKeyboard4x4(): | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def __init__(self, | ||
| + | | ||
| + | | ||
| + | # | ||
| + | self.CountRepeat = 3 | ||
| + | self.KeySleep = 0.001 | ||
| + | # | ||
| + | self.PinIn = [] | ||
| + | self.PinIn.append(Pin(pinin0, | ||
| + | self.PinIn.append(Pin(pinin1, | ||
| + | self.PinIn.append(Pin(pinin2, | ||
| + | self.PinIn.append(Pin(pinin3, | ||
| + | # | ||
| + | self.PinOut = [] | ||
| + | self.PinOut.append(Pin(pinout0, | ||
| + | self.PinOut.append(Pin(pinout1, | ||
| + | self.PinOut.append(Pin(pinout2, | ||
| + | self.PinOut.append(Pin(pinout3, | ||
| + | # | ||
| + | self.Thread = THR.CThread(self.CBOnStart, | ||
| + | # | ||
| + | self.KeyCodes = [] | ||
| + | return | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | def CBOnStart(self, | ||
| + | return | ||
| + | def CBOnBusy(self, | ||
| + | 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, | ||
| + | return | ||
| + | def CBOnEnd(self, | ||
| + | return | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | 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 ' | ||
| + | print(' | ||
| + | # | ||
| + | Keyboard = CKeyboard4x4(DEF.PIN_KEYBOARD_IN0, | ||
| + | DEF.PIN_KEYBOARD_IN2, | ||
| + | DEF.PIN_KEYBOARD_OUT0, | ||
| + | DEF.PIN_KEYBOARD_OUT2, | ||
| + | # 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(' | ||
| + | # | ||
| + | # | ||
| </ | </ | ||
| Line 298: | Line 530: | ||
| </ | </ | ||
| - | ==== Library-Module: | + | |
| + | ==== Library-Module: | ||
| <code python> | <code python> | ||
| # | # | ||
| - | # | + | """ |
| - | # Uart0 / 1 / 2 | + | |
| - | # | + | |
| - | ID_UART0 | + | |
| - | PIN_UART0_TX = 1 # USB | + | |
| - | PIN_UART0_RX = 3 # USB | + | |
| # | # | ||
| - | ID_UART1 | + | from LCDisplay import CLCDisplay |
| - | PIN_UART1_RX = 25 # 9 - not usable | + | from time import sleep_ms |
| - | PIN_UART1_TX = 26 # 10 - not usable | + | |
| # | # | ||
| - | ID_UART2 | + | # The PCF8574 has a jumper selectable address: 0x20 - 0x27 |
| - | PIN_UART2_RX = 16 | + | DEFAULT_I2C_ADDR |
| - | PIN_UART2_TX | + | |
| # | # | ||
| - | # | + | MASK_RS = 0x01 |
| - | # | + | MASK_RW = 0x02 |
| - | # | + | MASK_E = 0x04 |
| - | I2CADDRESS_LCDISPLAY_T | + | SHIFT_BACKLIGHT |
| - | I2CADDRESS_LCDISPLAY_A | + | SHIFT_DATA |
| # | # | ||
| - | LCDISPLAY_COUNT_ROWS | + | class CLCDisplayI2C(CLCDisplay): |
| - | LCDISPLAY_COUNT_COLUMNS | + | # |
| + | def __init__(self, | ||
| + | self.I2C | ||
| + | | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | sleep_ms(20) # Allow LCD time to powerup | ||
| + | # Send reset 3 times | ||
| + | self.hal_write_init_nibble(self.LCD_FUNCTION_RESET) | ||
| + | sleep_ms(5) | ||
| + | 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, | ||
| + | cmd = self.LCD_FUNCTION | ||
| + | if countrows > 1: | ||
| + | cmd |= self.LCD_FUNCTION_2LINES | ||
| + | self.hal_write_command(cmd) | ||
| + | # | ||
| + | def hal_write_init_nibble(self, | ||
| + | byte = ((nibble >> 4) & 0x0f) << SHIFT_DATA | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | # | ||
| + | def hal_backlight_on(self): | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | # | ||
| + | def hal_backlight_off(self): | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | # | ||
| + | def hal_write_command(self, | ||
| + | """ | ||
| + | byte = ((self.Backlight << SHIFT_BACKLIGHT) | (((cmd >> 4) & 0x0f) << SHIFT_DATA)) | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | byte = ((self.Backlight << SHIFT_BACKLIGHT) | ((cmd & 0x0f) << SHIFT_DATA)) | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | 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, | ||
| + | byte = (MASK_RS | (self.Backlight << SHIFT_BACKLIGHT) | (((data >> 4) & 0x0f) << SHIFT_DATA)) | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | byte = (MASK_RS | (self.Backlight << SHIFT_BACKLIGHT) | ((data & 0x0f) << SHIFT_DATA)) | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | self.I2C.writeto(self.I2CAddress, | ||
| + | # | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | ==== Library-Module: | ||
| + | <code python> | ||
| + | import time | ||
| + | import _thread as THR | ||
| # | # | ||
| - | #----------------------------------------- | + | # States |
| - | # | + | stIdle |
| - | # | + | stBusy |
| - | PIN_KEYBOARD_IN0 = 39 | + | stEnd = 2 |
| - | PIN_KEYBOARD_IN1 = 36 | + | |
| - | PIN_KEYBOARD_IN2 = 34 | + | |
| - | PIN_KEYBOARD_IN3 = 35 | + | |
| - | PIN_KEYBOARD_OUT0 = 13 | + | |
| - | PIN_KEYBOARD_OUT1 | + | |
| - | PIN_KEYBOARD_OUT2 | + | |
| - | PIN_KEYBOARD_OUT3 | + | |
| # | # | ||
| - | SIZE_KEYCODEBUFFER | + | class CThread(): |
| - | TIME_KEYPRESSED | + | # |
| - | TIME_KEYREPETITION | + | def __init__(self, |
| + | self.State | ||
| + | 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, | ||
| + | return | ||
| + | # | ||
| + | def Abort(self): | ||
| + | self.State = stEnd | ||
| + | if (None != self.OnAbort): | ||
| + | self.OnAbort(self) | ||
| + | return | ||
| + | # | ||
| + | def CBOnExecute(self, | ||
| + | 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) | ||
| + | | ||
| + | def OnAbort(thread): | ||
| + | return | ||
| + | def OnEnd(thread): | ||
| + | return | ||
| + | # | ||
| + | if (' | ||
| + | print(' | ||
| + | # | ||
| + | Thread = CThread(OnStart, | ||
| + | Thread.Start() | ||
| + | time.sleep(5.0) | ||
| + | Thread.Abort() | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| + | </ | ||
| + | |||
| + | ==== Library-Module: | ||
| + | <code python> | ||
| + | # | ||
| + | import machine | ||
| + | import time | ||
| + | # | ||
| + | import Define as DEF | ||
| + | import Thread as THR | ||
| + | import LCDisplayI2C as LCD | ||
| + | # | ||
| + | class CCommandDisplay(): | ||
| + | def __init__(self, | ||
| + | self.LCDisplay = lcdisplay | ||
| + | return | ||
| + | # | ||
| + | def Execute(self): | ||
| + | return | ||
| + | # | ||
| + | class CClear(CCommandDisplay): | ||
| + | def __init__(self, | ||
| + | super(CClear, | ||
| + | return | ||
| + | def Execute(self): | ||
| + | self.LCDisplay.Clear() | ||
| + | return | ||
| + | # | ||
| + | class CWrite(CCommandDisplay): | ||
| + | def __init__(self, | ||
| + | super(CWrite, | ||
| + | self.Row = row | ||
| + | self.Col = col | ||
| + | self.Text = text | ||
| + | return | ||
| + | def Execute(self): | ||
| + | self.LCDisplay.MoveTo(self.Row, | ||
| + | self.LCDisplay.PutText(self.Text) | ||
| + | return | ||
| + | # | ||
| + | class CThreadDisplay(THR.CThread): | ||
| + | # | ||
| + | def __init__(self, | ||
| + | super(CThreadDisplay, | ||
| + | self.I2CDisplay = i2cdisplay | ||
| + | self.LCDisplay = lcdisplay | ||
| + | self.CommandList = [] | ||
| + | return | ||
| + | # | ||
| + | def OnStart(self, | ||
| + | return | ||
| + | def OnBusy(self, | ||
| + | while (THR.stBusy == thread.State): | ||
| + | self.Clear() | ||
| + | Col = 0 | ||
| + | while (Col <= 19): | ||
| + | self.Write(0, | ||
| + | self.Write(1, Col, ' Hello1' | ||
| + | | ||
| + | self.Write(3, | ||
| + | while (0 < len(self.CommandList)): | ||
| + | Command | ||
| + | Command.Execute() | ||
| + | time.sleep(0.5) | ||
| + | Col += 1 | ||
| + | if (THR.stBusy != thread.State): | ||
| + | break | ||
| + | return | ||
| + | def OnAbort(self, | ||
| + | return | ||
| + | def OnEnd(self, thread): | ||
| + | return | ||
| + | # | ||
| + | def Open(self): | ||
| + | self.Start() | ||
| + | return | ||
| + | def Close(self): | ||
| + | self.Abort() | ||
| + | return | ||
| + | # | ||
| + | def Clear(self): | ||
| + | self.CommandList.append(CClear(self.LCDisplay)) | ||
| + | return | ||
| + | def Write(self, row, col, text): | ||
| + | self.CommandList.append(CWrite(self.LCDisplay, | ||
| + | return | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | if (' | ||
| + | print(' | ||
| + | # | ||
| + | I2CDisplay = machine.SoftI2C(scl=machine.Pin(22), | ||
| + | LCDisplay = LCD.CLCDisplayI2C(I2CDisplay, | ||
| + | DEF.LCDISPLAY_COUNT_ROWS, | ||
| + | TD = CThreadDisplay(I2CDisplay, | ||
| + | TD.Open() | ||
| + | # | ||
| + | time.sleep(10.0) | ||
| + | # | ||
| + | TD.Close() | ||
| + | # | ||
| + | print(' | ||
| + | # | ||
| # | # | ||
| </ | </ | ||
| Line 347: | Line 782: | ||
| + | |||
| + | ==== 211225 : " | ||
| + | * {{: | ||
| ==== 211224 : Versuch zur Thread-Entkopplung ==== | ==== 211224 : Versuch zur Thread-Entkopplung ==== | ||
module/micropython/esp32displaykey/esp32displaykey.1640437498.txt.gz · Last modified: 2021/12/25 15:04 (external edit)