How it works...

Initially, we defined addresses for each of the control registers used by the MAX7219 device. View the datasheet at for more information:

https://datasheets.maximintegrated.com/en/ds/MAX7219-MAX7221.pdf.

We created a class called matrix that will allow us to control the module. The __init__() function sets up the SPI of Raspberry Pi (using SPI_CS as pin 26 CS1 and SPI_SPEED as 100 kHz).

The key function in our matrix class is the sendCmd() function; it uses wiringpi.wiringPiSPIDataRW(SPI_CS,buff) to send buffer (which is the raw byte data that we want to send) over the SPI bus (while also setting the SPI_CS pin to low when the transfer occurs). Each command consists of two bytes: the first specifies the address of the register, and the second sets the data that needs to be put into it. To display a row of lights, we send the address of one of the ROW registers (MC.MAX7219_DIGIT) and the bit-pattern we want to display (as a byte).

After the wiringpi.wiringPiSPIDataRW() function is called, buffer contains the result of whatever is received on the MISO pin (which is read simultaneously as the data is sent via the MOSI pin). If connected, this will be the output of the LED module (a delayed copy of the data that was sent). Refer to the following There's more... section regarding daisy-chained SPI configurations to learn how the chip output can be used.

To initialize the MAX7219, we need to ensure that it is configured in the correct mode. First, we set the Scan Limit field to 7 (which enables all the DIG0 - DIG7 outputs). Next, we disable the built-in digit decoding since we are using the raw output for the display (and don't want it to try to display digits). We also want to ensure that the MAX7219_DISPLAYTEST register is disabled (if enabled, it would turn on all the LEDs).

We ensure the display is cleared by calling our own clear() function, which sends 0
to each of the MAX7219_DIGIT registers to clear each of the rows. Finally, we use the MAX7219_INTENSITY register to set the brightness of the LEDs. The brightness is controlled using a PWM output to make the LEDs appear brighter or darker according to the brightness that is required.

Within the main() function, we perform a quick test to display the letter K on the grid by sending a set of 8 bytes (0x0066763e1e366646):

Each 8 x 8 pattern consists of 8 bits in 8 bytes (one bit for each column, making each byte a row in the display)

The matrixGUI class creates a canvas object that is populated with a grid of rectangle objects to represent the 8 x 8 grid of LEDs we want to control (these are kept in self.light). We also add a text entry box to display the resulting bytes that we will send to the LED matrix module. We then bind the <Button-1> mouse event to the canvas so that mouseClick is called whenever a mouse click occurs within the area of the canvas.

We attach a function called changedCode() to the codeText variable using trace, a special Python function, which allows us to monitor specific variables or functions. If we use the 'w' value with the trace function, the Python system will call the callback function whenever the value is written to.

When the mouseClick() function is called, we use the event.x and event.y coordinates to identify the object that is located there. If an item is detected, then the ID of the item is used (via toggleLight()) to toggle the corresponding bit in the self.lightStatus value, and the color of the light in the display changes accordingly (via setLight()). The codeText variable is also updated with the new hexadecimal representation of the lightStatus value.

The changeCode() function allows us to use the codeText variable and translate it into an integer. This allows us to check whether it is a valid value. Since it is possible to enter text here freely, we must validate it. If we are unable to convert it to an integer, the codeValue text is refreshed using the lightStatus value. Otherwise, we check if it is too large, in which case we perform a bit-shift by 4 to divide it by 16 until it is within a valid range. We update the lightStatus value, the GUI lights, the codeText variable, and also the hardware (by calling updateHardware()).

The updateHardware() function makes use of the myMatrixHW object that was created using the MC.matrix class. We send the bytes that we want to display to the matrix hardware one byte at a time (along with the corresponding MAX7219_DIGIT value to specify the row).

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

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