Shop OBEX P1 Docs P2 Docs Learn Events
Adafruit's 16x32 RGB LED matrix c++ — Parallax Forums

Adafruit's 16x32 RGB LED matrix c++

Hi my name is chris and I am trying to program Adafruit's 16x32 RGB LED matrix in c++.

I am having trouble programming the matrix in c++.

I have looked at the links below and played around with some of the demo code

Ray's Logic

Forum Post


Attached is the original spin code and my attempt to translate it to c++.

Can someone tell me what I am doing wrong in translating the code.



attached is a cpp file i translated from the spin test file I attached

The most likely errors I probably have would be the clk because I am not sure if it is in the correct mode, (I believe the default frequency is 80MHz)

Or also, I'm not sure if I got this right in my c++ code.
'Need to toggle the A line or else the protection circuit will turn off the de-multiplexer.
repeat
OUTA[Pin_A..Pin_C]++

Any help would greatly appreciated,
-- chris

Comments

  • Other than the code:
    repeat
        OUTA[Pin_A..Pin_C]++
    

    Which I modified to:
    repeat
        OUTA[Pin_A..Pin_C]~~
    

    Spin2Cpp translates your spin code to these CPP files (Adafruit_Test2.cpp & Adafruit2_Test.h):
    //
    // automatically generated by spin2cpp v1.06 on Tue Oct 11 14:38:44 2016
    // spin2cpp Adafruit_Test2.spin 
    //
    
    #include <propeller.h>
    #include "Adafruit_Test2.h"
    
    #ifdef __GNUC__
    #define INLINE__ static inline
    #define PostEffect__(X, Y) __extension__({ int32_t tmp__ = (X); (X) = (Y); tmp__; })
    #else
    #define INLINE__ static
    static int32_t tmp__;
    #define PostEffect__(X, Y) (tmp__ = (X), (X) = (Y), tmp__)
    #endif
    
    int32_t Adafruit_Test2::Main(void)
    {
      OUTA &= ~(4095<<Pin_r1);
      DIRA |= (4095<<Pin_r1);
      OUTA &= ~(1<<Pin_a);
      OUTA &= ~(1<<Pin_b);
      OUTA &= ~(1<<Pin_c);
      OUTA = ((OUTA & 0xffff7fff) | 0x8000);
      OUTA = ((OUTA & 0xffffffbf) | 0x40);
      OUTA = ((OUTA & 0xffffff7f) | 0x80);
      {
        int32_t _idx__0000;
        for(_idx__0000 = 1; _idx__0000 <= 1; (_idx__0000 = (_idx__0000 + 1))) {
          OUTA = ((OUTA & 0xffffdfff) | 0x2000);
          OUTA &= ~(1<<Pin_clk);
        }
      }
      OUTA &= ~(1<<Pin_b1);
      OUTA &= ~(1<<Pin_r2);
      {
        int32_t _idx__0001;
        for(_idx__0001 = 1; _idx__0001 <= 31; (_idx__0001 = (_idx__0001 + 1))) {
          OUTA = ((OUTA & 0xffffdfff) | 0x2000);
          OUTA &= ~(1<<Pin_clk);
        }
      }
      {
        int32_t _idx__0002;
        for(_idx__0002 = 1; _idx__0002 <= 1; (_idx__0002 = (_idx__0002 + 1))) {
          OUTA = ((OUTA & 0xffffbfff) | 0x4000);
          OUTA &= ~(1<<Pin_le);
        }
      }
      OUTA &= ~(1<<Pin_en);
      while (1) {
        OUTA |= (7<<Pin_a);
      }
      return 0;
    }
    

    //
    // automatically generated by spin2cpp v1.06 on Tue Oct 11 14:41:27 2016
    // spin2cpp --ccode Adafruit_Test2.spin 
    //
    
    #ifndef Adafruit_Test2_Class_Defined__
    #define Adafruit_Test2_Class_Defined__
    
    #include <stdint.h>
    
    #define _Clkmode (1032)
    #define _Clkfreq (80000000)
    #define Pin_r1 (4)
    #define Pin_g1 (5)
    #define Pin_b1 (6)
    #define Pin_r2 (7)
    #define Pin_g2 (8)
    #define Pin_b2 (9)
    #define Pin_a (10)
    #define Pin_b (11)
    #define Pin_c (12)
    #define Pin_clk (13)
    #define Pin_le (14)
    #define Pin_en (15)
    
    typedef struct Adafruit_Test2 {
      char dummy__;
    } Adafruit_Test2;
    
      int32_t	Adafruit_Test2_Main(void);
    #endif
    

    Do they work (I don't think I have the LED matrix from Adafruit to test here...)?

    dgately
  • I get this error
    Adafruit_Test2.cpp:18:34: error: no 'int32_t Adafruit_Test2::Main()' member function declared in class 'Adafruit_Test2'
    Done. Build Failed!
  • Cree wrote: »
    I get this error
    Adafruit_Test2.cpp:18:34: error: no 'int32_t Adafruit_Test2::Main()' member function declared in class 'Adafruit_Test2'
    Done. Build Failed!

    Sorry, I needed to include the "--main" option to spin2cpp...

    These slightly changed files should now build:
    //
    // automatically generated by spin2cpp v1.06 on Tue Oct 11 19:13:44 2016
    // spin2cpp --main Adafruit_Test2.spin 
    //
    
    #include <propeller.h>
    #include "Adafruit_Test2.h"
    
    #ifdef __GNUC__
    #define INLINE__ static inline
    #define PostEffect__(X, Y) __extension__({ int32_t tmp__ = (X); (X) = (Y); tmp__; })
    #else
    #define INLINE__ static
    static int32_t tmp__;
    #define PostEffect__(X, Y) (tmp__ = (X), (X) = (Y), tmp__)
    #endif
    
    int32_t Adafruit_Test2::Main(void)
    {
      OUTA &= ~(4095<<Pin_r1);
      DIRA |= (4095<<Pin_r1);
      OUTA &= ~(1<<Pin_a);
      OUTA &= ~(1<<Pin_b);
      OUTA &= ~(1<<Pin_c);
      OUTA = ((OUTA & 0xffff7fff) | 0x8000);
      OUTA = ((OUTA & 0xffffffbf) | 0x40);
      OUTA = ((OUTA & 0xffffff7f) | 0x80);
      {
        int32_t _idx__0000;
        for(_idx__0000 = 1; _idx__0000 <= 1; (_idx__0000 = (_idx__0000 + 1))) {
          OUTA = ((OUTA & 0xffffdfff) | 0x2000);
          OUTA &= ~(1<<Pin_clk);
        }
      }
      OUTA &= ~(1<<Pin_b1);
      OUTA &= ~(1<<Pin_r2);
      {
        int32_t _idx__0001;
        for(_idx__0001 = 1; _idx__0001 <= 31; (_idx__0001 = (_idx__0001 + 1))) {
          OUTA = ((OUTA & 0xffffdfff) | 0x2000);
          OUTA &= ~(1<<Pin_clk);
        }
      }
      {
        int32_t _idx__0002;
        for(_idx__0002 = 1; _idx__0002 <= 1; (_idx__0002 = (_idx__0002 + 1))) {
          OUTA = ((OUTA & 0xffffbfff) | 0x4000);
          OUTA &= ~(1<<Pin_le);
        }
      }
      OUTA &= ~(1<<Pin_en);
      while (1) {
        OUTA |= (7<<Pin_a);
      }
      return 0;
    }
    
    
    Adafruit_Test2 MainObj__;
    
    int main() {
      return MainObj__.Main();
    }
    
    //
    // automatically generated by spin2cpp v1.06 on Tue Oct 11 19:13:44 2016
    // spin2cpp --main Adafruit_Test2.spin 
    //
    
    #ifndef Adafruit_Test2_Class_Defined__
    #define Adafruit_Test2_Class_Defined__
    
    #include <stdint.h>
    
    class Adafruit_Test2 {
    public:
      static const int _Clkmode = 1032;
      static const int _Clkfreq = 80000000;
      static const int Pin_r1 = 4;
      static const int Pin_g1 = 5;
      static const int Pin_b1 = 6;
      static const int Pin_r2 = 7;
      static const int Pin_g2 = 8;
      static const int Pin_b2 = 9;
      static const int Pin_a = 10;
      static const int Pin_b = 11;
      static const int Pin_c = 12;
      static const int Pin_clk = 13;
      static const int Pin_le = 14;
      static const int Pin_en = 15;
      int32_t	Main(void);
    private:
    };
    
    #endif
    

    dgately
  • I am able to run it now. Does almost the same as the spin version, but instead it flashes the lines of blue and red.

    --Chris
  • I was going to use this for a game I programmed in c++, I still haven't managed to code the LED matrix in c++.

    I think I'm going to re-code my game in spin if I don't get it to work in c++.

    Chris
  • I used a different test code from ray's logic in the spin2cpp with the main option and it worked.

    Here is the code for those that would like it in c++.

    --Chris
  • Hello again, I have another issue converting spin code again with the spin2cpp.

    This time i'm trying to convert the Adafruit_Test5 spin file from Ray's logic for display .bmp files.

    I managed to convert it to cpp, but I get an object error when I compile:
    bmp_matrix.cpp:317:98: error: cannot call member function 'void Adafruit_Test5::SetPixel(int32_t, int32_t, int32_t)' without object
    Done. Build Failed!

    Another option for me is using spinwrap, but I don't know how to use it from my searching.

    Can someone help me with this issue?

    Thanks,

    --Chris

  • Try this attached version of the project...


    dgately
  • It compiles, but it doesn't light up.
    I believe I know the problem.

    The Data section of the spin code:
    DAT 'Test 24bpp Windows bitmap to show
    WindowsBitmap1 byte
            file "Test.bmp"   'needs to be 32x16 and 24 bits per pixel in this example
    WindowsBitmap2 byte
            file "Test2.bmp"   'needs to be 32x16 and 24 bits per pixel in this example
    

    is turned into:
    static uint8_t dat[] = {
      0x42, 0x4d, 0x36, 0x06, 0x00, 0x00,.......and so on for about 200 lines}.
    

    Other than that, I am not sure what the issue would be.

    --Chris
  • dgatelydgately Posts: 1,630
    edited 2016-10-20 16:46
    The "static uint8_t dat[] = {...}", is a hexadecimal block of the Test.bmp & Test2.bmp files. Basically, this puts the content of those files into an array of bytes that gets accessed in the C++ code that follows.

    But, as you note, the code compiles but does not actually work... It appears that the code tries to access the array of bitmap bytes in a very out-of-bounds manner, "dat[460932]" which is far outside the scope of the prop's memory. I mistakenly used larger Test.bmp & Test2.bmp files (found in one of Ray's posts) than you had, which skewed the code.

    Your dat[] array appears much smaller and will probably allow creation of better code.

    If you could, try to create a C version of the Spin code with:

    spin2cpp --ccode --main Adafruit_Test5.spin
    

    The created Adafruit_Test5.c & Adafruit_Test5.h files should contain a correct access of your dat[] array. If that does not work, please attach the test.bmp & test2.bmp files that you are using and I will re-build the project.


    EDIT: see the next post for attached project files...

    dgately
  • Ah, found the correct test.bmp & test2.bmp files from Ray's website...

    I rebuilt the project as a std C project and it builds with reasonable results. Could you try the attached project files?

    dgately
  • CreeCree Posts: 132
    edited 2016-10-24 16:02
    Well I tried it, sadly I got the same result.
    No lights on the matrix lit up.

    -- Chris
  • Cree wrote: »
    Well I tried it, sadly I got the same result.
    N lights on the matrix lit up.

    Sorry, I can't test as I don't have the Adafruit 16x32 RGB LED matrix... I'll take a look at the code and see if I can get something set up to test that the pins are getting activity...
  • dgately wrote: »
    Cree wrote: »
    Well I tried it, sadly I got the same result.
    N lights on the matrix lit up.

    Sorry, I can't test as I don't have the Adafruit 16x32 RGB LED matrix... I'll take a look at the code and see if I can get something set up to test that the pins are getting activity...



    Sorry I didn't respond earlier, I had a busy weekend. If you do mange to get any good results, ill test it. Thanks for the help so far.

    But at the moment I am working on the sing led placement code that works to turn on more than just a single LED. The issue that I am working on is caused by a loop when I turn on x amount of LEDs, they flicker. If I take out the loop, the LEDs don't turn on.

    I'll post the code below, if someone wants to look at it.

    --Chris
  • RaymanRayman Posts: 14,646
    Spin2cpp usually works... Maybe it is something with your clock frequency.
    What kind of Prop board are you using? Are you using something like SimpleIDE where you can set the frequency?

    If not, looks like you have to do this:
    Q: How do I change the clock speed of the Propeller?
    
    You have several options. One is at load time with board configuration variables. You could put a clkfreq: option in your .cfg file or use the command line option to propeller-load:
    
    -D clkfreq=96000000             // Set to 96MHz clock speed
    
  • Rayman wrote: »
    Spin2cpp usually works... Maybe it is something with your clock frequency.
    What kind of Prop board are you using? Are you using something like SimpleIDE where you can set the frequency?

    If not, looks like you have to do this:
    Q: How do I change the clock speed of the Propeller?
    
    You have several options. One is at load time with board configuration variables. You could put a clkfreq: option in your .cfg file or use the command line option to propeller-load:
    
    -D clkfreq=96000000             // Set to 96MHz clock speed
    


    I don't think its a clock issue, I have used several spin examples from you're website and they worked fine.

    I am using the Propeller Activity Board(Rev A, ID: 32910)

    I am able to change the clk speed in the H file:
    //
    // automatically generated by spin2cpp v3.2.0 on Mon Oct 17 23:52:28 2016
    // spin2cpp --main Adafruit_Test4.spin 
    //
    
    #ifndef Adafruit_Test4_Class_Defined__
    #define Adafruit_Test4_Class_Defined__
    
    #include <stdint.h>
    
    class Adafruit_Test4 {
    public:
    // Crystal settings
      static const int _clkmode = (8 + 1024);
      static const int _clkfreq = 80000000;
    
    
  • RaymanRayman Posts: 14,646
    Hmm...

    Just so it's clear... You're saying that you have Spin code that works but when you run Spin2Cpp on it, then it doesn't work?

    Are you using SimpleIDE or command line or something else for C?
  • CreeCree Posts: 132
    edited 2016-10-28 03:47
    Rayman wrote: »
    Hmm...

    Just so it's clear... You're saying that you have Spin code that works but when you run Spin2Cpp on it, then it doesn't work?

    Are you using SimpleIDE or command line or something else for C?

    I'm using simple ide. I converted ur spin code of example 4 for the adafruit 16x32 led matrix with the spin2copper. I have used the bit map spin example(5) that doesn't flicker.
  • Cree wrote: »
    I am using the Propeller Activity Board(Rev A, ID: 32910)

    I am able to change the clk speed in the H file:
    //
    // automatically generated by spin2cpp v3.2.0 on Mon Oct 17 23:52:28 2016
    // spin2cpp --main Adafruit_Test4.spin 
    //
    
    #ifndef Adafruit_Test4_Class_Defined__
    #define Adafruit_Test4_Class_Defined__
    
    #include <stdint.h>
    
    class Adafruit_Test4 {
    public:
    // Crystal settings
      static const int _clkmode = (8 + 1024);
      static const int _clkfreq = 80000000;
    
    

    Unfortunately, changing the _clkfreq definition in the .H file won't do anything. The clock frequency is hard coded elsewhere in the .cpp file (in the __asm__ definition for _clkfreqval). If you want to change the clock frequency you'll have to change it in the .spin file and then re-run spin2cpp on it. That;s a known issue with spin2cpp, and I haven't had a chance to fix it yet (although in theory with the --gas flag it would be possible to make the clock frequency editable).
  • Unfortunately, changing the _clkfreq definition in the .H file won't do anything. The clock frequency is hard coded elsewhere in the .cpp file (in the __asm__ definition for _clkfreqval). If you want to change the clock frequency you'll have to change it in the .spin file and then re-run spin2cpp on it. That;s a known issue with spin2cpp, and I haven't had a chance to fix it yet (although in theory with the --gas flag it would be possible to make the clock frequency editable).

    Thanks ersmith, Ill try that. I think I figured a way without changing the clock speed. Ill tell you guys how it turns out.

    --Chris
  • Cree wrote: »
    Unfortunately, changing the _clkfreq definition in the .H file won't do anything. The clock frequency is hard coded elsewhere in the .cpp file (in the __asm__ definition for _clkfreqval). If you want to change the clock frequency you'll have to change it in the .spin file and then re-run spin2cpp on it. That;s a known issue with spin2cpp, and I haven't had a chance to fix it yet (although in theory with the --gas flag it would be possible to make the clock frequency editable).

    Thanks ersmith, Ill try that. I think I figured a way without changing the clock speed. Ill tell you guys how it turns out.

    --Chris

    Results: Increasing the clock speed in the spin file before converting it partially worked.
    I believe the flicker is caused by this function used multiple times:
    void placebit(int section, int half,int bit, int col)
    {
    if ((i == section) && (j == bit)) 
           {
            // if this is the right bit position
              OUTA |= (col << half);
                         
            }
     }
    

Sign In or Register to comment.