Control LED's using C++ and SPI - Respeaker 4 Mic Array

info about ReSpeaker series products

Moderators: lily.li, violet, seth.welday

Post Reply
pjtewkesbury
Pre-kindergarten
Pre-kindergarten
Posts: 2
Joined: Wed Jan 09, 2019 6:29 pm

Control LED's using C++ and SPI - Respeaker 4 Mic Array

Post by pjtewkesbury » Fri May 31, 2019 4:44 pm

Hi

I have just purchased a Respeaker 4 Mic Array for the Raspberry PI (Mine is Model 3B+) and i have installed the drivers and got the microphones working correctly. I then when on to install the Alexa for Raspberry PI (https://github.com/alexa/avs-device-sdk/wiki/Raspberry-Pi-Quick-Start-Guide-with-Script) which works well with the ReSpeaker 4Mic array.

I now want to extend the Alexa App which is written in C++ to control the LED's on the ReSpeaker. I have run the python examples and the Raspberry PI can control the LEDs as expected.

I understand that the LED's are controlled via the SPI interface. Do you have any example code in C++ to control the LED's or any resources to do this.

Many thanks.

Peter Tewkesbury

guillaume.saudemont
Pre-kindergarten
Pre-kindergarten
Posts: 2
Joined: Sat Apr 06, 2019 11:31 pm

Re: Control LED's using C++ and SPI - Respeaker 4 Mic Array

Post by guillaume.saudemont » Sat Jun 01, 2019 12:15 am

Hello,

I've got exactly the same problem.

I would like to use led in C++ from Respeaker 4 Mic Array but I don't find any samples.

I need help too.

Regards,

carsten
Pre-kindergarten
Pre-kindergarten
Posts: 4
Joined: Sat May 18, 2019 10:27 pm

Re: Control LED's using C++ and SPI - Respeaker 4 Mic Array

Post by carsten » Mon Jun 03, 2019 1:57 pm


bill.yu
Elementary-5
Elementary-5
Posts: 597
Joined: Mon Apr 02, 2018 11:05 am

Re: Control LED's using C++ and SPI - Respeaker 4 Mic Array

Post by bill.yu » Wed Jun 05, 2019 3:28 pm

Hi there,

1. You have to turn on the spi first.

sudo raspi-config
Go to "Interfacing Options"
Go to "SPI"
Enable SPI
Exit the tool

2. use gpio -v to verify if you install wiringpi library correctly.

http://wiringpi.com/download-and-install/

3. then you can create led.cpp file. you can understand the apa102 logic by reading https://cpldcpu.wordpress.com/2014/11/3 ... -superled/

Code: Select all

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

int main() {
  wiringPiSetup();
  if(wiringPiSPISetup(0, 6000000) < 0) {
    std::cerr << "wiringPiSPISetup failed" << std::endl;
  }

  pinMode (21, OUTPUT);
  digitalWrite (21, HIGH);

  uint8_t buf[1];
  for(int i = 0; i < 4; i++) {
    buf[0] = 0x00;
    wiringPiSPIDataRW(0, buf, 1);
  }

  uint8_t r, g, b, brightness;
  r = 0xFF;
  g = 0x00;
  b = 0x00;
  brightness = 100;
  uint8_t led_frame[4];

  for(int i = 0; i < 12; i++) {
    led_frame[0] = 0b11100000 | (0b00011111 & brightness);
    led_frame[1] = b;
    led_frame[2] = g;
    led_frame[3] = r;

    wiringPiSPIDataRW(0, led_frame, 4);
  }

  for(int i = 0; i < 4; i++) {
    buf[0] = 0xFF;
    wiringPiSPIDataRW(0, buf, 1);
  }
  
  delay(5000);

  return 0;


}

4. run below command to compile the file

g++ -Wall -o led led.cpp -lwiringPi


5. run below command

sudo ./led

thanks.

Post Reply