reduce code size

This commit is contained in:
interfisch 2020-04-12 14:53:42 +02:00
parent 6f257ebbd8
commit 097506d8c5
1 changed files with 86 additions and 40 deletions

View File

@ -28,6 +28,8 @@ boolean getMute(uint8_t pbit);
void setSelectionChannel(uint8_t i, boolean state); void setSelectionChannel(uint8_t i, boolean state);
void setMuteChannel(uint8_t i, boolean state); void setMuteChannel(uint8_t i, boolean state);
void publishCurrentSetVolume(); void publishCurrentSetVolume();
void publishAllStates(int pn, String pTopicname, boolean (*pgetBit) (uint8_t));
void changeRelaisByNumber(uint8_t pn, String pTopicPrefix, String pTopic, String pspayload, void (*psetXChannel) (uint8_t, boolean));
#define LEDPIN 9 //PB1 = D9 = Pin15 #define LEDPIN 9 //PB1 = D9 = Pin15
Adafruit_NeoPixel leds = Adafruit_NeoPixel(9, LEDPIN, NEO_GRB + NEO_KHZ800); Adafruit_NeoPixel leds = Adafruit_NeoPixel(9, LEDPIN, NEO_GRB + NEO_KHZ800);
@ -45,6 +47,7 @@ uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x06};
#define CLIENT_ID "Mixer" #define CLIENT_ID "Mixer"
EthernetClient ethClient; EthernetClient ethClient;
PubSubClient mqttClient; PubSubClient mqttClient;
String mqttdevname="audiomixer/";
long last_send=0; long last_send=0;
@ -196,7 +199,9 @@ void setup() {
poti_set=analogRead(PIN_POT); poti_set=analogRead(PIN_POT);
#ifdef DEBUG
Serial.println("Ready"); Serial.println("Ready");
#endif
last_send = millis(); last_send = millis();
@ -236,7 +241,7 @@ void loop() {
if (useethernet){ if (useethernet){
if (!mqttClient.connected()) { if (!mqttClient.connected()) {
if (loopmillis-last_mqttreconnectattempt > MQTTRECONNECTDELAY) { if (loopmillis-last_mqttreconnectattempt > MQTTRECONNECTDELAY) {
Serial.println("Reconnecting to mqtt"); Serial.println("Recon. mqtt");
reconnect(); reconnect();
last_mqttreconnectattempt=loopmillis; last_mqttreconnectattempt=loopmillis;
} }
@ -415,8 +420,6 @@ void loop() {
if ( loopmillis > last_serialdebug+INTERVAL_SERIALDEBUG){ if ( loopmillis > last_serialdebug+INTERVAL_SERIALDEBUG){
last_serialdebug=loopmillis; last_serialdebug=loopmillis;
Serial.print(" set="); Serial.print(" set=");
Serial.print(poti_set); Serial.print(poti_set);
Serial.print(" is="); Serial.print(" is=");
@ -436,9 +439,6 @@ void loop() {
wheelpos+=5; wheelpos+=5;
leds.show(); leds.show();
} }
if (loopmillis%5001==0) { if (loopmillis%5001==0) {
@ -479,16 +479,12 @@ void callback(char* topic, byte* payload, unsigned int length) {
//} //}
//if (strncmp((const char*)topic, "audiomixer/volume/set",sizeof(topic)) == 0) { //if (strncmp((const char*)topic, "audiomixer/volume/set",sizeof(topic)) == 0) {
if (String(topic).equals("audiomixer/volume/set")){ if (String(topic).equals("audiomixer/volume/set")){
//Serial.println("republish");
float _floatvalue = spayload.toFloat(); float _floatvalue = spayload.toFloat();
_floatvalue=constrain(_floatvalue,0.0,100.0); _floatvalue=constrain(_floatvalue,0.0,100.0);
/*Serial.print("Volume string=");
Serial.println(spayload);
Serial.print("setvalue=");
Serial.println(_floatvalue);*/
poti_set=constrain(map(_floatvalue,0.0,100.0,POT_MIN,POT_MAX),POT_MIN,POT_MAX); //set new poti position poti_set=constrain(map(_floatvalue,0.0,100.0,POT_MIN,POT_MAX),POT_MIN,POT_MAX); //set new poti position
poti_reachedposition=false; //aim for new position poti_reachedposition=false; //aim for new position
@ -507,6 +503,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
} }
//publish all states //publish all states
/*
for (uint8_t i=0;i<NUMMUTECHANNELS;i++) { for (uint8_t i=0;i<NUMMUTECHANNELS;i++) {
String pub_topic = "audiomixer/mute_"+String(i); String pub_topic = "audiomixer/mute_"+String(i);
boolean _mutestate=getMute(i); boolean _mutestate=getMute(i);
@ -515,30 +512,9 @@ void callback(char* topic, byte* payload, unsigned int length) {
}else{ }else{
mqttClient.publish((char*) pub_topic.c_str(), "false"); mqttClient.publish((char*) pub_topic.c_str(), "false");
} }
} }*/
publishAllStates(NUMMUTECHANNELS,"mute_", &getMute);
}else if (String(topic).startsWith("audiomixer/mute_")) { //with range
//Serial.print("Mute range string="); Serial.println(spayload);
uint8_t _index=255;
for (uint8_t i=0; i<NUMMUTECHANNELS && _index==255; i++) {
if (String(topic).equals("audiomixer/mute_"+String(i)+"/set")) {
_index=i;
}
}
Serial.print("Found index:"); Serial.println(_index);
if (_index==255) {
Serial.println("Index out of range");
}else{ //index ok
String pub_topic = "audiomixer/mute_"+String(_index);
if (spayload.equalsIgnoreCase("false")) {
setMuteChannel(_index,false);
mqttClient.publish((char*) pub_topic.c_str(), "false");
}else if (spayload.equalsIgnoreCase("true")) {
setMuteChannel(_index,true);
mqttClient.publish((char*) pub_topic.c_str(), "true");
}
}
}else if (String(topic).equals("audiomixer/select/set")) { //withouth range }else if (String(topic).equals("audiomixer/select/set")) { //withouth range
//Serial.print("Select string="); Serial.println(spayload); //Serial.print("Select string="); Serial.println(spayload);
uint16_t ipayload=spayload.toInt(); uint16_t ipayload=spayload.toInt();
@ -551,7 +527,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
} }
//publish all states //publish all states
for (uint8_t i=0;i<NUMSELECTCHANNELS;i++) { /*for (uint8_t i=0;i<NUMSELECTCHANNELS;i++) {
String pub_topic = "audiomixer/select_"+String(i); String pub_topic = "audiomixer/select_"+String(i);
boolean _selectstate=getSelection(i); boolean _selectstate=getSelection(i);
if (_selectstate) { if (_selectstate) {
@ -559,11 +535,40 @@ void callback(char* topic, byte* payload, unsigned int length) {
}else{ }else{
mqttClient.publish((char*) pub_topic.c_str(), "false"); mqttClient.publish((char*) pub_topic.c_str(), "false");
} }
} }*/
publishAllStates(NUMSELECTCHANNELS,"select_", &getSelection);
}else if (String(topic).startsWith("audiomixer/mute_")) { //with range
//Serial.print("Mute range string="); Serial.println(spayload);
/*uint8_t _index=255;
for (uint8_t i=0; i<NUMMUTECHANNELS && _index==255; i++) {
if (String(topic).equals("audiomixer/mute_"+String(i)+"/set")) {
_index=i;
}
}
Serial.print("Found index:"); Serial.println(_index);
if (_index==255) {
Serial.println("Index OOR");
}else{ //index ok
String pub_topic = "audiomixer/mute_"+String(_index);
if (spayload.equalsIgnoreCase("false")) {
setMuteChannel(_index,false);
mqttClient.publish((char*) pub_topic.c_str(), "false");
}else if (spayload.equalsIgnoreCase("true")) {
setMuteChannel(_index,true);
mqttClient.publish((char*) pub_topic.c_str(), "true");
}
}*/
changeRelaisByNumber(NUMMUTECHANNELS,"audiomixer/mute_", topic, spayload, &setMuteChannel);
}else if (String(topic).startsWith("audiomixer/select_")) { }else if (String(topic).startsWith("audiomixer/select_")) {
//Serial.print("Select string="); Serial.println(spayload); //Serial.print("Select string="); Serial.println(spayload);
uint8_t _index=255; /*uint8_t _index=255;
for (uint8_t i=0; i<NUMSELECTCHANNELS && _index==255; i++) { for (uint8_t i=0; i<NUMSELECTCHANNELS && _index==255; i++) {
if (String(topic).equals("audiomixer/select_"+String(i)+"/set")) { if (String(topic).equals("audiomixer/select_"+String(i)+"/set")) {
_index=i; _index=i;
@ -571,7 +576,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
} }
//Serial.print("Found index:"); Serial.println(_index); //Serial.print("Found index:"); Serial.println(_index);
if (_index==255) { if (_index==255) {
Serial.println("Index out of range"); Serial.println("Index OOR");
}else{ //index ok }else{ //index ok
String pub_topic = "audiomixer/select_"+String(_index); String pub_topic = "audiomixer/select_"+String(_index);
if (spayload.equalsIgnoreCase("false")) { if (spayload.equalsIgnoreCase("false")) {
@ -581,8 +586,15 @@ void callback(char* topic, byte* payload, unsigned int length) {
setSelectionChannel(_index,true); setSelectionChannel(_index,true);
mqttClient.publish((char*) pub_topic.c_str(), "true"); mqttClient.publish((char*) pub_topic.c_str(), "true");
} }
} }*/
changeRelaisByNumber(NUMSELECTCHANNELS,"audiomixer/select_", topic, spayload, &setSelectionChannel);
} }
} }
@ -657,4 +669,38 @@ void publishCurrentSetVolume()
dtostrf(_setpercentage, 1, 2, pub_payload); dtostrf(_setpercentage, 1, 2, pub_payload);
mqttClient.publish("audiomixer/volume", pub_payload); mqttClient.publish("audiomixer/volume", pub_payload);
Serial.print("pub="); Serial.println(_setpercentage); Serial.print("pub="); Serial.println(_setpercentage);
}
void publishAllStates(int pn, String pTopicname, boolean (*pgetBit) (uint8_t)){
for (uint8_t i=0;i<pn;i++) {
String pub_topic = mqttdevname+pTopicname+String(i);
boolean _state=pgetBit(i);
if (_state) {
mqttClient.publish((char*) pub_topic.c_str(), "true");
}else{
mqttClient.publish((char*) pub_topic.c_str(), "false");
}
}
}
void changeRelaisByNumber(uint8_t pn, String pTopicPrefix, String pTopic, String pspayload, void (*psetXChannel) (uint8_t, boolean))
{
uint8_t _index=255;
for (uint8_t i=0; i<pn && _index==255; i++) {
if (String(pTopic).equals(pTopicPrefix+String(i)+"/set")) {
_index=i;
}
}
if (_index==255) {
Serial.println("Index OOR");
}else{ //index ok
String pub_topic = pTopicPrefix+String(_index);
if (pspayload.equalsIgnoreCase("false")) {
psetXChannel(_index,false);
mqttClient.publish((char*) pub_topic.c_str(), "false");
}else if (pspayload.equalsIgnoreCase("true")) {
psetXChannel(_index,true);
mqttClient.publish((char*) pub_topic.c_str(), "true");
}
}
} }