Using the Flick HAT

Flick HAT comes in the form of a shield, which you can simply plug into your Raspberry Pi and start using. However, once you do that, you will not be left with any GPIO pins. Hence, to save ourselves from that problem, we will be connecting it using male-to-female wires. This will give us access to the other GPIO pins and then we can have fun.

So, go ahead and connect it as follows. The following is a pin diagram of the Flick board:

Thereafter, make the connections as follows: 

Once the connection is done, simply upload this code and see what happens: 

import signal
import flicklib
import time
def message(value):
print value
@flicklib.move()
def move(x, y, z):
global xyztxt
xyztxt = '{:5.3f} {:5.3f} {:5.3f}'.format(x,y,z)
@flicklib.flick()
def flick(start,finish):
global flicktxt
flicktxt = 'FLICK-' + start[0].upper() + finish[0].upper()
message(flicktxt)
def main():
global xyztxt
global flicktxt
xyztxt = ''
flicktxt = ''
flickcount = 0
while True:

xyztxt = ''
if len(flicktxt) > 0 and flickcount < 5:
flickcount += 1
else:
flicktxt = ''
flickcount = 0
main()

Now once you have uploaded  the code, lets go ahead and understand what this code is actually doing.

We are using a library called  import flicklib this is provided by the manufacturer of this board. The functions of this library would be used all over in this chapter for communicating with the flick board and getting the data 

def message(value):
print value

Here, we are defining a function named message(value) what this would do is simply print whatever value would be passed on to the function in the argument:

@flicklib.move()

This has a special concept of decorators. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it. In the preceding line of code, we are declaring that it is a decorator @.

This has a special job: dynamically defines any function in a program. What this means in plain English is that the function defined using this methodology can work differently depending on how the user defines it. 

The function move() will further be complimented by the function, which is getting defined after it. These kind of functions are named nested functions. That is functions inside a function:

def move(x, y, z):
global xyztxt
xyztxt = '{:5.3f} {:5.3f} {:5.3f}'.format(x,y,z)

Here, we are defining a function named move(), which has arguments as x, y, and z. Inside the function, we have defined a global variable named xyztxt; now, the value of xyztxt would be in a form of five digit, with a decimal after three places. How did we know that? As you can see, we are using a function named format(). What this function does is format the values of a given variable according to the way the user has requested it for. We have declared here the value as {:5.3f}:5 represents that it would be of five digits, and 3f represents that the decimal places would be after three digits. Hence, the format would be xxx.xx:

def flick(start,finish):
global flicktxt
flicktxt = 'FLICK-' + start[0].upper() + finish[0].upper()
message(flicktxt)

Here, we have defined a function named flick(start, finish). It has two arguments: start and finish. Using the line flicktxt = 'FLICK-' + start[0].upper() + finish[0].upper()this is slicing the characters as recognized by the gesture board. If a south–north swipe is detected, then the start will get south and finish is north. Now we are only using the first characters of the words:

    global xyztxt
global flicktxt

We are again defining the variables named xyztxt and flicktxt globally. Earlier, what we have done is that we have defined it in the function. Hence, it is important for us to define it in the main program:

if len(flicktxt) > 0 and flickcount < 5:
flickcount += 1
else:
flicktxt = ''
flickcount = 0

The flicktxt variable would get a value corresponding to the gesture  when the gesture is detected. In case there is no gesture then flicktxt would be left empty. A variable named flickcount will count how many times its swiped. If the values are out of the range specified then the flicktxt would be cleared to empty string using the line flicktxt = '' and flickcount would be made 0.

The final output of this would be a text given to user providing in which direction the hand is flicked. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.114.142