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 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.
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.
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..