chow716
Posts: 3
Joined: Thu Dec 07, 2017 2:27 am

Python Photobooth with 8x8 RGB LED matrix

Thu Jan 04, 2018 9:41 pm

Hello all,
So I am creating a photobooth in python. I've sourced a couple of different python photobooths that I've found to combine all of the features I want. Now I've finally received the 8x8 LED matrix I ordered from China (http://wiki.52pi.com/index.php/RPI-RGB- ... KU:EP-0075). Not having done a whole bunch of research before I ordered it, I've found that it's based on a 74HC595 chip. I realize that I can make it behave how I want if I were to write the code in C (part of which is shown below). But I'm really looking for some advice on how I can make this matrix work with my python app. The matrix is only serving as a 3 second countdown (3 static numbers), but I just haven't found a reliable way to achieve the multiplexing in python. (From what I can tell, a MAX7219 matrix would have been much easier, but I'm trying to work with what I have now)

Code: Select all

#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <stdint.h>

#define RED_DATA 0
#define BLUE_DATA 1
#define GREEN_DATA 2

int main(void)
{
  static uint8_t data[4] = {0x0,0x0,0x0,0x0};
  
  wiringPiSetup();
  wiringPiSPISetup(0,500000);
  while(1)
     {
         static uint8_t three[8] = {0x00,0x3c,0x66,0x60,0x38,0x60,0x66,0x3c};
         int j;
         int x=2;
    for ( j=0;j<8;j++)
        {
          data[0] = 0xFF;
          data[2] = ~three[j];
          data[1] = 0xFF;
          data[3] = 0x01 << j ;
          wiringPiSPIDataRW(0,data,sizeof(data));
          delay(x);
        };
    };
}
Any help would be greatly appreciated!

chow716
Posts: 3
Joined: Thu Dec 07, 2017 2:27 am

Re: Python Photobooth with 8x8 RGB LED matrix

Fri Jan 05, 2018 9:11 pm

I wasn't aware that there was a python-wrapped version of wiringpi. The issue that's holding me up now is working with the hex characters in python. I want to have an output that looks like this (updated with each loop of course)

Code: Select all

wiringpi.wiringPiSPIDataRW(0, "\x00\x00\x00\x00")
but I can't seem to figure out how to properly handle the hex values in Python without having them get converted to strings.

chow716
Posts: 3
Joined: Thu Dec 07, 2017 2:27 am

Re: Python Photobooth with 8x8 RGB LED matrix

Sat Jan 06, 2018 1:58 pm

My apologies for not being clear. The issue is I'm having is storing the hex values as literals. When using the loop:

Code: Select all

while True:
    three = [0x00,0x3c,0x66,0x60,0x38,0x60,0x66,0x3c]
    j= 0
    x = 0.01
    for j in range(0, 8):
        data[0] = three[j]
        data[1] = 0xFF
        data[2] = 0xFF
        data[3] = 0x01 << j 
        buf = byte(data)
        wiringpi.wiringPiSPIDataRW(0, buf)
buf comes out as an array with decimal values. I need them to be hex values.

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Python Photobooth with 8x8 RGB LED matrix

Sat Jan 06, 2018 9:13 pm

OK maybe I can help with some explanation here.

Python and C handle variables a bit different. C is very structured and has lots of variable types (signed char(1 byte), unsigned char, short int(2 byte), long int (4 byte)and many other types). Python stores everything in a 4 byte signed long integer.

So python will store some_varible = 0xff in a 4 byte signed integer not in a C 1 byte unsigned char.

If you are using the standard python libraries for I2C (smbus) and SPI (spidev) then they will trim off the excess 3 bytes and just send 1 byte but if your using a C library with a python wrapper then you need the correct bytes. If you want to convert python types in C byte array's then there is struct library for that see https://docs.python.org/3.5/library/str ... ule-struct

I use struct to pack my data when using micropython as it is just C wrappers for python and data needs to be packed in a C compatible format

Return to “Python”