| 
      
        | Posted: 04:11pm 15 Nov 2022 |  Copy link to clipboard |   Print this post | 
                                               |  Hello,
 I have written a MMBasic program to control the great RGB keypad from Pimoroni.
 
 
  
 Feel free to improve and optimize it, because I am not an experienced MMBasic programmer. My knowledge is in C and Python. But it works fine so far.
 Have fun with it,
 
 Peter
 
 
 '##################################
 '# Driver for Pimoroni RGB Keypad #
 '# V1.0 by Peter Doerwald         #
 '# 14.11.2022                     #
 '##################################
 
 '# Init of SPI and I2C    ###############################################
 'Set SPI for APA102 and PICO RGB Keypad
 Pin(17) = 0 : SetPin GP17, DOUT  ' pin 17 will be used as the enable signal
 SetPin GP16,GP19,GP18,SPI
 SPI OPEN 15000000, 0, 8          ' speed is 15MHz and the data size is 8 bits
 Dim KeypadRGB%(16)               'RGB888 color of LED 0-15
 'Set SPI forTCA9555 Key Input
 SetPin GP3, INTL, ButtonPressed  'INT-Pin
 SetPin GP4,GP5, I2C
 I2C OPEN 100, 1000               '100kHz I2C
 Row1%=0                          'Button 0-7  from TCA9555, Reg.0
 Row2%=0                          'Button 8-15 from TCA9555, Reg.1
 Key%=255                         'pressed key (0-15)
 Temp%=255
 KeyReleased%=0                   ' Is key released ?
 Keypressed%=0                    '1 if key is pressed or released
 
 'Main Demo Program
 CLR_RGB
 Timer =0
 Do
 If Keypressed%=1 Then
 Temp%=WhichKey()
 If Temp%<255 Then
 Key%=Temp%
 KeyReleased%=0
 Else
 KeyReleased%=1
 EndIf
 If KeyReleased%=0 Then
 Print "Key:",Key%
 SetKeyRGB(Key%,Rnd()*&HFFFFFF)
 Timer =0
 EndIf
 Keypressed%=0
 EndIf
 If Timer>5000 Then CLR_RGB():EndIf
 Loop
 End
 
 ' SUBs and FUNCTIONs'
 
 Sub PrintPad(bright)          'print all LEDs with colours in KeypadRGB%(16), bright:0-31
 Pin(17)=0
 junk = SPI(&H00)              ' send the command and ignore the return
 junk = SPI(&H00)
 junk = SPI(&H00)
 junk = SPI(&H00)
 For a=0 To 15
 junk = SPI(&H80+bright)
 junk = SPI(KeypadRGB%(a) And &HFF)
 junk = SPI((KeypadRGB%(a) And &HFF00)\256)
 junk = SPI((KeypadRGB%(a) And &HFF0000)\65536)
 Next a
 junk = SPI(&HFF)
 junk = SPI(&HFF)
 junk = SPI(&HFF)
 junk = SPI(&HFF)
 Pin(17)=0
 End Sub
 
 Sub SetKeyRGB(Key,ColorRGB)   'Set RGB888 color of Key (0-15)
 KeypadRGB%(Key)= ColorRGB
 PrintPad(31)
 End Sub
 
 Sub CLR_RGB                   'Clears all LEDs to black
 For x=0 To 15
 KeypadRGB%(x)=&H000000
 Next x
 PrintPad(31)
 End Sub
 
 Sub ButtonPressed             'Interrupt-Routine if key is pressed or released
 Keypressed%=1
 I2C WRITE &H20, 1, 1, 0
 I2C READ &H20, 0, 1, Row1%
 I2C WRITE &H20, 1, 1, 1
 I2C READ &H20, 0, 1, Row2%
 End Sub
 
 Function WhichKey() As INTEGER 'Calculates which key is pressed, 255=No Key or released
 Local But%=0
 Local Brk%=0
 Brk% =0
 Do
 If Row1%<255 Then
 If (Row1% And &H01) = &H00 Then
 WhichKey=But%
 Brk% =1
 Else
 Row1%=Fix(Row1%/2)
 But%=But%+1
 EndIf
 Else
 If Row2%<255 Then
 If (Row2% And &H01) = &H00 Then
 WhichKey=But%+8
 Brk% =1
 Else
 Row2%=Fix(Row2%/2)
 But%=But%+1
 EndIf
 Else
 WhichKey=255
 Brk% =1
 EndIf
 EndIf
 If But%>7 Then
 Brk% =1
 WhichKey=255
 EndIf
 Loop Until Brk%=1
 End Function
 
 
 |