Hi,
I’ve worked on a small weekend project. I connected a 8x8 led matrix with a MAX7219 controller to my photon. My software is exposing functions to display different symbols (heart, invader, etc.) on this matrix. I call those functions via particle cloud API to trigger the symbols. Everything worked out fine but I wonder if there are some creative out there, helping to optimise the following code. I want to make it more generic and flexible.
If I follow my current design (one function creates one symbol) I’m limited to 15 symbols, because the cloud allows to expose 15 functions.
To solve this I think about one generic function with a switch/case that receives a parameter. This brings me to one problem. Cloud functions expecting Strings datatypes and switch/cases work with integers. I found one possible solution with a mapping: Switch case & strings is this for my case the only way or are there some other ideas?
// This #include statement was automatically added by the Particle IDE.
#include <LedControl-MAX7219-MAX7221.h>
LedControl *led;
int displayNone(String command);
int displayHeart(String command);
int displayInvader1(String command);
void setup() {
led = new LedControl(A0,A2,A1,1); //DIN,CLK,CS,HowManyDisplays
led-> shutdown(0,false); //Turn it on
Particle.function("none", displayNone);
Particle.function("heart", displayHeart);
Particle.function("inv1", displayInvader1);
}
void loop() {
}
int displayHeart(String command){
int i = 15;
int j = 0;
if(command == "heart"){
led->setRow(0,0,28);
led->setRow(0,1,62);
led->setRow(0,2,126);
led->setRow(0,3,252);
led->setRow(0,4,252);
led->setRow(0,5,126);
led->setRow(0,6,62);
led->setRow(0,7,28);
led->setIntensity(0,15);
while(j<=1000){
while (i!=0){
led->setIntensity(0,i = i-1);
delay(50);
}
while (i<=15){
led->setIntensity(0,i = i+1);
delay(50);
}
j++;
}
return 1;
}
else return -1;
}
int displayInvader1(String command){
int j = 0;
if(command == "inv1"){
while(j<=1000){
led->setRow(0,0,24);
led->setRow(0,1,220);
led->setRow(0,2,54);
led->setRow(0,3,95);
led->setRow(0,4,95);
led->setRow(0,5,54);
led->setRow(0,6,220);
led->setRow(0,7,24);
led->setIntensity(0,7);
delay(1000);
led->setRow(0,0,152);
led->setRow(0,1,92);
led->setRow(0,2,182);
led->setRow(0,3,95);
led->setRow(0,4,95);
led->setRow(0,5,182);
led->setRow(0,6,92);
led->setRow(0,7,152);
led->setIntensity(0,7);
delay(1000);
j++;
}
return 1;
}
else return -1;
}
int displayNone(String command){
if(command == "none"){
led->setRow(0,0,0);
led->setRow(0,1,0);
led->setRow(0,2,0);
led->setRow(0,3,0);
led->setRow(0,4,0);
led->setRow(0,5,0);
led->setRow(0,6,0);
led->setRow(0,7,0);
led->setIntensity(0,7);
return 1;
}
else return -1;
}