14.1 Independent application

The RPi-Spark application we completed in the previous chapters needs be runs with the command: rspk, because we only implemented the RPi-Spark module and it did not launch code. In this section we complete a HelloWorld application that it runs independently.

#!/usr/bin/python
import sys
from JMRPiFoundations.Skeleton.RPiSparkProvider import initSpark
from JMRPiFoundations.Devices.rpi_spark_z_1_0_0 import RPiSparkConfig as mySparkConfig
from JMRPiFoundations.Skeleton.RPiSparkModule import RPiSparkModule

class HelloWorldModule(RPiSparkModule):
    def setup(self):
        # Open thermometer 
        self.RPiSpark.Attitude.openWith(temp=True, accel=False, gyro=False)

    def run(self):
        # Show "Hello World !" and current temperature of RPi-Spark on screen of RPi-Spark
        text = "Hello World !\nTemp: {:6.2f}".format(self.RPiSpark.Attitude.getTemp())
        self.RPiSpark.Screen.write( text, xy=(0, 16), spacing=10, screenCenter=True )
        # Draw a line
        self.RPiSpark.Screen.Canvas.line( (24, 32, 98, 32), 1, 2 )
        self.RPiSpark.Screen.refresh()

def main(argv):
    mySpark = initSpark()
    myModule = HelloWorldModule(mySparkConfig, mySpark)
    myModule.run()

if __name__ == "__main__":
   main(sys.argv[1:])

Save as hello.py and execute the following command in the terminal:

$> python hello.py

You can also change hello.py to an executable file without having to start it with python, so you can run the application directly. Type following command in the terminal:

$> mv hello.py hello
$> chmod +x hello
$> ./hello

 

Was this article helpful?

YES     |     NO

 

Enjoying the project? Spotted a mistake? Any opinions on the website? Let us know!