How it works...

The wiringpi2 library has excellent support for I/O expander chips, like the one used for the Adafruit LCD character module. To use the Adafruit module, we need to set up the pin mapping for all the pins of MCP23017 Port A, as shown in the following table (then, we set up the I/O expander pins with an offset of 100):

Name

SELECT

RIGHT

DOWN

UP

LEFT

GREEN

BLUE

RED

MCP23017 Port A

A0

A1

A2

A3

A4

A6

A7

A8

WiringPi pin

100

101

102

103

104

106

107

108

 

The pin mapping for all of MCP23017 Port B's pins is as follows:

Name

DB7

DB6

DB5

DB4

E

RW

RS

MCP23017 Port B

B1

B2

B3

B4

B5

B6

B7

WiringPi pin

109

110

111

112

113

114

115

To set up the LCD screen, we initialize wiringPiSetup() and the I/O expander, mcp23017Setup(). We then specify the pin offset and bus address of the I/O expander. Next, we set all the hardware buttons as inputs (using pinMode(pin number,0)), and the RW pin of the LCD to an output. The wiringpi2 LCD library expects the RW pin to be set to LOW (forcing it into read-only mode), so we set the pin to LOW (using digitalWrite(AF_RW,0)).

We create an lcd object by defining the number of rows and columns of the screen and stating whether we are using a 4- or 8-bit data mode (we are using four of the eight data lines, so we will be using 4-bit mode). We also provide the pin mapping of the pins we are using (the last four are set to 0 since we are only using four data lines).

Now, we will create a function called PrintLCD(), which will allow us to send strings to show on each line of the display. We use lcdPosition() to set the cursor position on the lcd object for each line and then print the text for each line. We also add some blank spaces at the end of each line to ensure the full line is overwritten.

The next function, checkBtn(), briefly checks the left/right and select buttons to see if they have been pressed (using the digitalRead() function). If the left/right button has been pressed, then the index is set to the previous/next item in the array. If the SELECT button is pressed, then the run flag is set to False (this will exit the main loop, allowing the script to finish).

The main() function calls gpiosetup() to create our lcd object; then, we create our dataDevice object and fetch the data names. Within the main loop, we get new data; then, we use our printLCD() function to display the data name on the top line and the data value on the second line. Finally, we check to see whether the buttons have been pressed and set the index to our data as required.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.247.11