Week 16
Interface and application programming.
For this week i'm going to interface a capacitive touch sesnor with my computer.
The board uses serial interface to send the data of 4 sensors to the computer using FTDI cable. The data formatted as in the following:
0b0000DCBA
Where A is the value of sensor one, B is the value of sensor two, C is the value of sensor three, and D is the value of sensor four. The value is 1 if the sensor is touched or 0 if not. For example if sensor 1 is touched, the value recieved will be 1, and if sensor 4 is touched, the value will be 8. If multiple sensors is touched, the value recieved will be the sum of the value of the two sensors.
I used TKinter on python to communicate with the board and display the touched plates. I used Neil's code as a base and developed my interface.
The first part of the code is imorting the modules, and set some variables that define the window size and colors ,, etc.
from Tkinter import * import serial lastByte = 0 WINDOW = 800 # window size squaresNumber = 4 tsquare = WINDOW / (squaresNumber + (squaresNumber - 1) + 2) #square size tsquareColor = '#d6d6c2' tsquareColor_active = '#000033' backgroundColor = '#7a7a52'
The idle function is the routine where i read the serial, check if the sensor is touched, and update the graphics. After reading the value, i perfome logical AND on the sensor bit position and shift it to the 0 bit. So no matter the sensor's number, when it is touched the value of its variable will be 1. Finally i loop over the sensors and change the colors based on the value, then update the canvas.
def idle(parent,canvas): # # idle routine # global tsquareColor global tsquareColor_active ser.flush() sbyte = ord(ser.read()) touchSensors = [(sbyte & 1,'rect1'), ((sbyte & 2) >> 1, 'rect2'), ((sbyte & 4) >> 2, 'rect3'), ((sbyte & 8) >> 3, 'rect4')] for x, n in touchSensors: if x == 1: canvas.itemconfigure(n, fill=tsquareColor_active) else: canvas.itemconfigure(n, fill=tsquareColor) canvas.update() parent.after_idle(idle,parent,canvas)
The rest of the code is to setup the canvas, create the rectangles, and setup the serial communication.
Download the source code:
TouchsensorGUI.pyFab Academy - Week 11 - Input devices from Mohamed Kamel on Vimeo.
This time i'm going to use the board as a mouse to scroll up and down when i touch the plate in different directions. I found a tutorial that helped me get started. So this time i'm going to read the data from the serial port as before but i'm making some anlaysis on the data to detect the order of touching the plates and use pyautogui library to automate a scroll up or down.
The library is so easy, i'm just calling pyautogui.scoll(value) where value can be a positive number - scroll up - or negative number - scroll down -. You can use the library to do moere complex stuff even controlling the keyboard.
Download the source code, and don't forget to change the serial port name.
scrollTouchpad.pyScroll Touchpad from Mohamed Kamel on Vimeo.
Interfacing is super FUN !