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 setMuteChannel(uint8_t i, boolean state);
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
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"
EthernetClient ethClient;
PubSubClient mqttClient;
String mqttdevname="audiomixer/";
long last_send=0;
@ -196,7 +199,9 @@ void setup() {
poti_set=analogRead(PIN_POT);
#ifdef DEBUG
Serial.println("Ready");
#endif
last_send = millis();
@ -236,7 +241,7 @@ void loop() {
if (useethernet){
if (!mqttClient.connected()) {
if (loopmillis-last_mqttreconnectattempt > MQTTRECONNECTDELAY) {
Serial.println("Reconnecting to mqtt");
Serial.println("Recon. mqtt");
reconnect();
last_mqttreconnectattempt=loopmillis;
}
@ -415,8 +420,6 @@ void loop() {
if ( loopmillis > last_serialdebug+INTERVAL_SERIALDEBUG){
last_serialdebug=loopmillis;
Serial.print(" set=");
Serial.print(poti_set);
Serial.print(" is=");
@ -436,9 +439,6 @@ void loop() {
wheelpos+=5;
leds.show();
}
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 (String(topic).equals("audiomixer/volume/set")){
//Serial.println("republish");
if (String(topic).equals("audiomixer/volume/set")){
float _floatvalue = spayload.toFloat();
_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_reachedposition=false; //aim for new position
@ -507,6 +503,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
}
//publish all states
/*
for (uint8_t i=0;i<NUMMUTECHANNELS;i++) {
String pub_topic = "audiomixer/mute_"+String(i);
boolean _mutestate=getMute(i);
@ -515,29 +512,8 @@ void callback(char* topic, byte* payload, unsigned int length) {
}else{
mqttClient.publish((char*) pub_topic.c_str(), "false");
}
}
}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");
}
}
}*/
publishAllStates(NUMMUTECHANNELS,"mute_", &getMute);
}else if (String(topic).equals("audiomixer/select/set")) { //withouth range
//Serial.print("Select string="); Serial.println(spayload);
@ -551,7 +527,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
}
//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);
boolean _selectstate=getSelection(i);
if (_selectstate) {
@ -559,11 +535,40 @@ void callback(char* topic, byte* payload, unsigned int length) {
}else{
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_")) {
//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++) {
if (String(topic).equals("audiomixer/select_"+String(i)+"/set")) {
_index=i;
@ -571,7 +576,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
}
//Serial.print("Found index:"); Serial.println(_index);
if (_index==255) {
Serial.println("Index out of range");
Serial.println("Index OOR");
}else{ //index ok
String pub_topic = "audiomixer/select_"+String(_index);
if (spayload.equalsIgnoreCase("false")) {
@ -581,10 +586,17 @@ void callback(char* topic, byte* payload, unsigned int length) {
setSelectionChannel(_index,true);
mqttClient.publish((char*) pub_topic.c_str(), "true");
}
}
}*/
changeRelaisByNumber(NUMSELECTCHANNELS,"audiomixer/select_", topic, spayload, &setSelectionChannel);
}
}
@ -658,3 +670,37 @@ void publishCurrentSetVolume()
mqttClient.publish("audiomixer/volume", pub_payload);
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");
}
}
}