# Using CardKB from M5Stack without reprogramming it!

**URL:** <https://forum.clockworkpi.com/t/using-cardkb-from-m5stack-without-reprogramming-it/6106>\
**Category:** Firmware and Apps\
**Tags:** kernel, hardware\
**Created:** [December 9, 2020, 10:41am UTC](https://forum.clockworkpi.com/t/using-cardkb-from-m5stack-without-reprogramming-it/6106 "2020-12-09T10:41:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pooky-s](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/pooky-s/32/4506_2.png) [@Pooky-s](https://forum.clockworkpi.com/u/Pooky-s)\
**Post date:** [December 9, 2020, 10:41am UTC](https://forum.clockworkpi.com/t/using-cardkb-from-m5stack-without-reprogramming-it/6106/1 "2020-12-09T10:41:06Z")

</div>

# **What is the CardKB ?**

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/clockworkpi/original/2X/4/48abac61f7085f9bb37ebded70c14aefe1e678f2.jpeg)  
The [CardKB](https://m5stack.com/products/cardkb-mini-keyboard) is a simple and cute keyboard made by M5Stack, owner of modular stackable product development toolkits based on ESP32.  
It uses I²C to communicate, wich means it has a clock line and a data line (you can check documentation [here](https://docs.m5stack.com/#/en/unit/cardkb)). It nearly exactly fits the size of our GameShell, and costs nearly nothing, wich makes it a great choice.

## Enabling I²C on the GameShell :

I took many informations to make I²C enabled [here.](https://p3dt.net/post/2019/12/26/gameshell-gpios.html) If not precised, execute tasks on any computer. I assume you’re using a Linux distribution (I use [Pop!\_OS](https://pop.system76.com/) but it should work on any distros, you’ll just need to adapt it to yours ! If you don’t have one installed, consider using [WSL](https://docs.microsoft.com/en-us/windows/wsl/install-win10)).  
First of all :

- Download and extract the toolchain which we will use to compile and build our Kernel:

```bash
wget https://releases.linaro.org/components/toolchain/binaries/7.2-2017.11/arm-linux-gnueabihf/gcc-linaro- 
7.2.1-2017.11-x86_64_arm-linux-gnueabihf.tar.xz
tar xf gcc-linaro-7.2.1-2017.11-x86_64_arm-linux-gnueabihf.tar.xz

```

- Add it to the PATH :

```bash
cd gcc-linaro-7.2.1-2017.11-x86_64_arm-linux-gnueabihf/bin/
export PATH=$PATH:$(pwd)

```

- Download dependencies if required :

```bash
apt-get install flex bison libssl-dev u-boot-tools

```

- Download Kernel sources (choose depending on your environment). I’ll choose the [5.3.6](https://forum.clockworkpi.com/t/gameshell-os-update-to-kernel-5-3-6-with-source-code/4729/2) as it’s the one I use on my GS.

- From sources’ root, go to `./arch/arm/boot/dts` and add the following to the end of `sun8i-r16-clockworkpi-cpi3.dts` :

```auto
&i2c1 {
    status = "okay";
};

```

- Build the Kernel using this command :

```bash
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-

```

- Copy the newly generated .dtb file to your GS :

```bash
scp arch/arm/boot/dts/sun8i-r16-clockworkpi-cpi3.dtb cpi@clockworkpi:/home/cpi/

```

- As a super-user, mount your boot partition _ **on the GS** _ (you can use `ssh cpi@clockworkpi` to access a CLI) :

```bash
sudo su
cd /mnt
mkdir boot
mount /dev/mmcblk0p1 /mnt/boot/

```

- Make a backup as it may break your OS if something goes wrong _ **on the GS** _ :

```bash
cp /mnt/boot/sun8i-r16-clockworkpi-cpi3.dtb /mnt/boot/sun8i-r16-clockworkpi-cpi3.dtb.bak

```

- Install the new .dtb file by copying it into the boot partition _ **on the GS** _ :

```bash
cp /home/cpi/sun8i-r16-clockworkpi-cpi3.dtb /mnt/boot/sun8i-r16-clockworkpi-cpi3.dtb

```

- Reboot and test your installation simply using _ **on the GS** _ :

```bash
i2cdetect -y 0

```

If everything worked as intended it should probe every I²C adresses, showing an array like this :

```auto
    0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 

```

## Enabling uinput Kernel module :

The [keyboard library](https://pypi.org/project/keyboard/), wich is the library we will be using in our script , uses uinput to emulate a keyboard. It has not been included in the GS Kernel modules, we will then need to build our own modified Kernel and add it to the GS.

- Fist of all we will need to fix something. In the file `./scripts/dtc/dtc-lexer.l` find :

```auto
YYLTYPE yylloc;

```

- And add `extern` keyword before it :

```auto
extern YYLTYPE yylloc;

```

- Using previously downloaded Kernel sources include `User level driver support` using menuconfig :

```bash
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig

```

- You can find it under :

```auto
Device Drivers --->
    Input Device Support --->
        Miscellaneous devices --->

```

To include it you’ll need to press Y (a `*` will appear).  
Press `exit` or Escape until you’re prompted to save. Save.

- Build the Kernel image :

```bash
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
mkimage -A arm -O linux -T kernel -C none -a 0x40008000 -e 0x40008000 -n "Linux Kernel" -d arch/arm/boot/zImage uImage

```

- On your GS, make a backup file of your uImage (as root):

```bash
cd /mnt/boot/
cp uImage uImage.bak

```

- Replace the `uImage` file and reboot:

```bash
cp /home/cpi/uImage ./uImage
reboot

```

You’re nearly done !

## Python script :

To ensure the script will work, you’ll need to download smbus and keyboard Python libraries :

> **[keyboard](https://pypi.org/project/keyboard/)**
>
> Hook and simulate keyboard events on Windows and Linux

  

> **[smbus](https://pypi.org/project/smbus/)**
>
> Python bindings for Linux SMBus access through i2c-dev

  
We will use the keyboard library to simulate a keyboard via Python as mentionned earlier. The smbus one will be used to interact with the I²C interface.
- To install those, simply type _ **on the GS** _ :

```bash
pip install smbus
pip install keyboard

```

- Make a file named `<filename>.py` and paste the following code in it :

```python
#importing smbus for i2c, and keyboard for injecting keystrokes.
import smbus,keyboard

#Selecting i2c channel (0 by default for GameShell, may be different for RPi
channel = 0

#CardKB i2c adress to listen on 
address = 0x5f

bus = smbus.SMBus(channel)

while True:
# test = ""
# try :
# test = bus.read_byte_data(address, 0)
# except:
# pass
# if test != "":
# print(test)
    try :
        #reads value passed by the keyboard
        msg = bus.read_byte(address)
        #ensures no empty messages are passed
        if msg != 0 :
            #180,181,182,183 prints weird chars instead of what they're meant to : key_up/down/left/right
            if msg == 180 :
                keyboard.press_and_release("left")
                #print(msg)
            elif msg == 181:
                keyboard.press_and_release("up")
                #print(msg)
            elif msg == 182:
                keyboard.press_and_release("down")
                #print(msg)
            elif msg == 183:
                keyboard.press_and_release("right")
                #print(msg)
            else:
                #print(chr(msg))
                #print(msg)
                keyboard.press_and_release(chr(msg))
    except :
        pass

```

- Connect your CardKB following it’s [pinmap](https://docs.m5stack.com/#/en/unit/cardkb?id=pinmap) and [GameShell’s one](https://p3dt.net/post/2019/12/26/gameshell-gpios.html#expansion-pins). Run it. _ **You’re finally done !** _

Here’s what it looks like :

 ![image](https://cdn.discordapp.com/attachments/785518733581090846/786176338016862208/IMG_20201209_112324.jpg)

As you can see it’s absolutely not practical, it still needs a case, and I use some [illegal lego techniques](https://www.youtube.com/watch?v=PpZjHV1n6Hg&t=2s&ab_channel=TheJackHammer). I should also shorten the cables. Feel free to ask questions, share what you did etc… See ya ! 😃

---

<div class="post-metadata">

**Author:** ![pilot](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/pilot/32/4471_2.png) [@pilot](https://forum.clockworkpi.com/u/pilot)\
**Post date:** [December 9, 2020, 10:04pm UTC](https://forum.clockworkpi.com/t/using-cardkb-from-m5stack-without-reprogramming-it/6106/2 "2020-12-09T22:04:09Z")

</div>

Solid tutorial! Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![Pooky-s](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/pooky-s/32/4506_2.png) [@Pooky-s](https://forum.clockworkpi.com/u/Pooky-s)\
**Post date:** [December 9, 2020, 11:12pm UTC](https://forum.clockworkpi.com/t/using-cardkb-from-m5stack-without-reprogramming-it/6106/3 "2020-12-09T23:12:37Z")

</div>

Thanks ! I’m planning to buy a 3D printer to make it more practical. 😛
