Now, you have your super small computer (Pi), and would like to do something with it. Programming does not need to be complicated and daunting, it is just the environment which needs to offer the right tools for the job.
Python is an easy to use, but powerful, programming language and together with a Raspberry Pi, forms a very flexible development environment and is also great for other ad-hoc programming tasks.
Sample code provided by HAMKit is formed on Python and uses a file extension of .py Some further reading on Python can be found at Raspberry Pi Python Basics
Example HAMKit Code
Examples of HAMKit VMAC PiHat test code can be found in our downloads area.
You can create, or edit, Python using a basic text editor such as notepad or an editor like Thonny or Geany, which is both easy to use and more importantly correctly structures, and colour codes the commands (making it even easier). The Raspberry Pi Raspbian comes preloaded with Thonny.
The small Thonny application can also be used on a computer, so allowing you to learn, update code and then copy to the Pi. The Variables window (View>Variables), is a very useful feature as it displays the variable values. http://thonny.org/
Hint: Python does not use curly braces but instead requires indentation for nesting code.
A Python ‘script’ file can be run from within Thonny, or simply from command line through the terminal.
To run a Python program from Thonny, simply load the file into the editor and click the run, as shown.
Advanced Code Editing
Python has a number of Editors, or IDE Applications, and at the end of the day it is a personal preference and balance of features depending on what you do.
Geany is a more powerful editor, featuring an automatic Symbol display which enables you to quickly find variables and functions, with easy to use Compiler and plug-ins.
To run a Python program from the command line, open the Terminal (CTL ALT T), go to the path of the file and enter python [file name]
This means you can easily script Python, so it auto loads/runs on the Raspberry Pi power up for example.
python VMAC\ PiHat\ -\ Test\ Loop\ Example\ Code\ to\ Setup\ GPIO\ and\ I2C\ v1.1.py
Hint: When entering text on the command line, to save typing, you can start whatever you entering and then press TAB. example. pyt and TAB will auto complete to python. Then press space and then repeat the first few characters of the part, complete the following text on the commend line.
Example: First go to the Desktop folder and then pyt TAB SPACE VM TAB – should give you the above.
Remember: Linux commands are Case Sensitive, so if the folder or filename is capitalised, like Desktop for example, you will need to enter capital D and not lower case d.
Renaming: Long filenames with spaces may look confusing. If you rename the file from ‘VMAC\ PiHat\ -\ Test\ Loop\ Example\ Code\ to\ Setup\ GPIO\ and\ I2C\ v1.1.py’ to ‘testcode.py’, the comand line would look like this. The syntax is mv oldfilename newfilname Once you have renamed you can then run the python script as shown underneath.
pi@raspberrypi:~/Desktop $ mv VMAC\ PiHat\ -\ Test\ Loop\ Example\ Code\ to\ Setup\ GPIO\ and\ I2C\ v1.1.py testcode.py pi@raspberrypi:~/Desktop $ python testcode.py
The important thing, is to enjoy learning..