11.3 Build "Menu" module

We build a simple "Menu" module that draw "A" and "B" rectangles on the Canvas, press button A to print "Press A ..." in screen of computer, press button B to print "Press B ..." in screen of computer and press JOY_UP + button A to exit.

from JMRPiFoundations.Skeleton.RPiSparkModule import RPiSparkModule
from time import sleep

class Menu(RPiSparkModule):

    def drawMenuItem(self, x,y, menu_title, menu_desc):
        width = 16
        self.RPiSpark.Screen.Canvas.rectangle( (x, y, x + width, y + width), 0, 1 )
        self.RPiSpark.Screen.write(menu_title, xy=(x+6, y+3))
        self.RPiSpark.Screen.write(menu_desc, xy=(x + width + 8, y+3))

    def drawMenu(self):
        self.RPiSpark.Screen.clear()
        self.drawMenuItem(5, 10, "A", "One Wave")
        self.drawMenuItem(5, 32, "B", "Twin Waves")
        self.RPiSpark.Screen.refresh()

    def setup(self):
        self.initKeyButtons("QUERY")

    def run(self):
        self.drawMenu()

        while True:

            #################################
            # Exit button status read
            #
            if self.readExitButtonStatus(): break

            #################################
            # Run Module
            #
            if self.readKeyButton(self.RPiSparkConfig.BUTTON_ACT_A):
                print("Press A ...")

            if self.readKeyButton(self.RPiSparkConfig.BUTTON_ACT_B):
                print("Press B ...")

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

$> rspk Menu -f

At this time you can see the "A" and "B" options on the OLED display. You can press the button A or B and the name of the button you pressed will be show on the computer display.


 

Was this article helpful?

YES     |     NO

 

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