Skip to content
Snippets Groups Projects
Commit 8403422d authored by Rock Qu's avatar Rock Qu Committed by Graham Schelle
Browse files

add rpi touchpad notebook

parent 88ba16fa
No related branches found
No related tags found
No related merge requests found
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
%% Cell type:markdown id: tags:
## Reading Values from Touch Keypad
This demonstration shows how to interact with the Raspberry Pi Touch Keypad.
The Raspberry Pi Touch Keypad is required (https://www.seeedstudio.com/Raspberry-Pi-Touch-Keypad-p-2772.html).
![](data/rpi_touchpad.jpg)
The Raspberry Pi touch keypad supports up to 16 keys with adjustable
sensitivity and built-in LD0.
Touch keypad is read only, and has IIC interface
connected to SDA1 and SCL1 on the Raspberry Pi interface.
The I2C will read 2 bytes of data: `Data_0` and `Data_1`.
* `Data_0`: B7 ~ B0 is TP0 ~ TP7 on/off status. 0 is key off, 1 is key on.
* `Data_1`: B7 ~ B0 is TP8 ~ TP15 on/off status. 0 is key off, 1 is key on.
### 1. Prepare the overlay
Download the overlay first, then select the shared pin to be connected to
RPI header (by default, the pins will be connected to PMODA instead).
%% Cell type:code id: tags:
``` python
from pynq.overlays.base import BaseOverlay
base = BaseOverlay("base.bit")
base.select_rpi()
```
%% Output
%% Cell type:markdown id: tags:
### 2. Instantiate the Microblaze
The Microblaze will control the pins on the RASPBERRYPI header.
%% Cell type:code id: tags:
``` python
%%microblaze base.RPI
#include "xio_switch.h"
#include "circular_buffer.h"
#include "i2c.h"
// Device constants
#define TOUCHPAD_DEV_ADDRESS 0x57
unsigned int get_touchpad_reg_value(){
uint8_t data[2];
i2c device = i2c_open_device(1);
set_pin(2, SDA1);
set_pin(3, SCL1);
i2c_read(device, TOUCHPAD_DEV_ADDRESS, data, 2);
return (unsigned int) ((data[0] << 8) + data[1]);
}
```
%% Cell type:markdown id: tags:
### 3. Read the key values
The available pin names are listed below.
%% Cell type:code id: tags:
``` python
PIN_MAPPING = {'circle': 0,
'cross': 1,
'square': 2,
'r': 3,
'home': 4,
'+': 5,
'-': 6,
'l': 7,
'down': 8,
'right': 9,
'up': 10,
'left': 11,
'power': 12,
'rpi': 13,
'logo': 14,
'triangle': 15
}
```
%% Cell type:markdown id: tags:
To convert the raw data into the value for each key, we define the following
functions.
%% Cell type:code id: tags:
``` python
def reg2int(reg_value, key_number):
return "{0:b}".format(reg_value).zfill(16)[-(key_number+1)]
def get_touchpad(key_name):
reg_value = get_touchpad_reg_value()
key_number = PIN_MAPPING[key_name]
return reg2int(reg_value, key_number)
```
%% Cell type:markdown id: tags:
Run the following code without pressing any button.
%% Cell type:code id: tags:
``` python
get_touchpad('home')
```
%% Output
'0'
%% Cell type:markdown id: tags:
While pressing gently on the home button of the touch keypad, run the following code.
%% Cell type:code id: tags:
``` python
get_touchpad('home')
```
%% Output
'1'
%% Cell type:markdown id: tags:
While pressing the right arrow and square at the same time,
run the following code. Note that there are 2 read commands issued,
although 1 read command can get values for all the keys.
%% Cell type:code id: tags:
``` python
for key in ['right', 'square']:
print('Key {} reads value {}.'.format(key, get_touchpad(key)))
```
%% Output
Key right reads value 1.
Key square reads value 1.
%% Cell type:markdown id: tags:
### 4. Cleanup
Switch back the connection on the shared pin to PMODA header.
%% Cell type:code id: tags:
``` python
base.select_pmoda()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment