View unanswered posts | View active topics It is currently Thu Dec 12, 2019 4:26 pm



Reply to topic  [ 2 posts ] 
 FitPC, GPIO and Python 
Author Message

Joined: Thu Oct 29, 2015 10:21 am
Posts: 5
Post FitPC, GPIO and Python
Having spent a little while getting Python to talk to the GPIO on a Fitlet, I thought I'd share.

The first thing is to make sure the kernel is up to date. I downloaded and installed fitlet_kernel_3.19_2015-12-16 following the instructions on this site.

A user needs superuser privileges in order to talk to the hardware, so you need to launch the development environment as a super user, so I started with the desktop, brought up a terminal prompt and type "sudo spyder" to get editing.

The following code turns on and off an LED connected between pin gpio89 and ground via a 300 ohm resistor. All interaction with the gpio driver is done by writing (and reading) files as shown in the example. Unfortunately the forum seems to strip indentation before display, so the for loop in the example will need fixing before you try and run the code.

I'd be interested to know how other people get on and if anyone has success in getting interrupts etc working from within Python.

dave

# -*- coding: utf-8 -*-
"""
@author: Dave Langstaff dpl@aber.ac.uk
Turn LED on gpio89 on and off
"""
import time # needed for time.sleep to time loop
import os # needed for permissions fudge on files

# first set up gpio pin 89
f=open('/sys/class/gpio/export','w')
f.write('89')
f.close()

# fudge to get write permission
# This fudge could/should be fixed in a future driver release
os.system('chmod a+rw /sys/class/gpio/gpio89/*')

# define pin 89 as output
f=open('/sys/class/gpio/gpio89/direction','w')
f.write('out')
f.close()

# open file to write values to pin
f=open('/sys/class/gpio/gpio89/value','w')

# turn LED on
f.write('1') # note it is the character 1 we are writing
f.flush() # need to use flush() or close() after each write

# now loop around to flash off and on
for i in range(10):
time.sleep(0.1)
f.write('0'); f.flush() # LED off.
time.sleep(0.1)
f.write('1'); f.flush() # LED on
# end of loop

# turn LED off at end
f.write('0');f.flush()

# close file
f.close()

# release gpio line
f=open('/sys/class/gpio/unexport','w')
f.write('89')
f.close()

# Enjoy!!


Fri Jan 08, 2016 5:10 pm
Profile
Post Re: FitPC, GPIO and Python
Thanks for sharing. Your example is shared on fitlet FAQ page:
http://fit-pc.com/wiki/index.php/FAQ:fitlet#Using_fitlet_GPIOs_under_Python


Mon Jan 11, 2016 7:45 am
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.