add mute and select topics

This commit is contained in:
interfisch 2020-04-03 15:38:59 +02:00
parent 5f1665c2d7
commit 143d218760
1 changed files with 171 additions and 21 deletions

View File

@ -19,6 +19,13 @@ boolean srRead(uint8_t pbit);
void srWrite(uint8_t pbit, boolean state); void srWrite(uint8_t pbit, boolean state);
void callback(char* topic, byte* payload, unsigned int length); void callback(char* topic, byte* payload, unsigned int length);
void sendData(); void sendData();
void srShiftOut();
void setMuteInt(uint8_t i);
void setSelectionInt(uint8_t i);
boolean getSelection(uint8_t pbit);
boolean getMute(uint8_t pbit);
void setSelectionChannel(uint8_t i, boolean state);
void setMuteChannel(uint8_t i, boolean state);
#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);
@ -27,7 +34,7 @@ uint8_t wheelpos=0;
#include "Ethernet.h" #include "Ethernet.h"
#include "PubSubClient.h" #include "PubSubClient.h"
boolean useethernet=false; boolean useethernet=true;
//Ethernet and MQTT //Ethernet and MQTT
String ip = ""; String ip = "";
@ -37,8 +44,10 @@ uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x06};
EthernetClient ethClient; EthernetClient ethClient;
PubSubClient mqttClient; PubSubClient mqttClient;
#define PUBLISH_DELAY 10000
long last_send=0; long last_send=0;
#define MQTTRECONNECTDELAY 5000
unsigned long last_mqttreconnectattempt=0;
@ -66,6 +75,10 @@ long last_button_released=0; //last time button has been released (for debounce)
#define SRDATA PD2 //D2 = PD2 #define SRDATA PD2 //D2 = PD2
uint16_t srbits=0; uint16_t srbits=0;
#define NUMSELECTCHANNELS 8
#define NUMMUTECHANNELS 8
#include <Encoder.h> #include <Encoder.h>
Encoder volEnc(PIN_ENCA,PIN_ENCB); Encoder volEnc(PIN_ENCA,PIN_ENCB);
float encoderMultiplier=4.0; float encoderMultiplier=4.0;
@ -135,6 +148,7 @@ void setup() {
Serial.println("Starting"); Serial.println("Starting");
leds.begin(); leds.begin();
leds.clear();
for(uint8_t i=0;i<leds.numPixels();i++){ //set color of all leds for(uint8_t i=0;i<leds.numPixels();i++){ //set color of all leds
leds.setPixelColor(i, leds.Color(100,100,100)); leds.setPixelColor(i, leds.Color(100,100,100));
} }
@ -170,41 +184,46 @@ void setup() {
//Serial.println(ip); //Serial.println(ip);
// setup mqtt client // setup mqtt client
Serial.println("Configuring MQTT client"); Serial.println("Configuring MQTT");
mqttClient.setClient(ethClient); mqttClient.setClient(ethClient);
mqttClient.setServer("10.0.0.1", 1883); mqttClient.setServer("10.0.0.1", 1883);
Serial.println("MQTT client configured"); Serial.println("MQTT configured");
mqttClient.setCallback(callback); mqttClient.setCallback(callback);
} }
}else{ }else{
Serial.println("Ethernet disabled"); Serial.println("Eth disabled");
} }
poti_set=analogRead(PIN_POT); poti_set=analogRead(PIN_POT);
Serial.print("Poti value="); Serial.println(poti_set);
Serial.println("Ready"); Serial.println("Ready");
last_send = millis(); last_send = millis();
} }
void reconnect() { void reconnect() {
// Loop until reconnected // Loop until reconnected
while (!mqttClient.connected()) { if (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection..."); Serial.print("Attempting MQTT connection...");
// Attempt to connect // Attempt to connect
if (mqttClient.connect(CLIENT_ID)) { if (mqttClient.connect(CLIENT_ID)) {
Serial.println("connected"); Serial.println("connected");
mqttClient.publish("audiomixer/ip", ip.c_str()); //Publish own ip mqttClient.publish("audiomixer/ip", ip.c_str()); //Publish own ip
mqttClient.subscribe("audiomixer/main/volume/set"); //subscribe to /set, republish without /set mqttClient.subscribe("audiomixer/volume/set"); //subscribe to /set, republish without /set
mqttClient.subscribe("audiomixer/mute/set"); //without range
for (uint8_t i=0;i<NUMMUTECHANNELS;i++) { //with range
String sub_topic="audiomixer/mute_"+String(i)+"/set";
mqttClient.subscribe((char*) sub_topic.c_str());
}
mqttClient.subscribe("audiomixer/select/set"); //without range
for (uint8_t i=0;i<NUMSELECTCHANNELS;i++) { //with range
String sub_topic="audiomixer/select_"+String(i)+"/set";
mqttClient.subscribe((char*) sub_topic.c_str());
}
} else { } else {
Serial.print("failed, rc="); Serial.print("failed, rc=");
Serial.print(mqttClient.state()); Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
delay(5000);// Wait 5 seconds before retrying
} }
} }
} }
@ -214,8 +233,11 @@ void loop() {
if (useethernet){ if (useethernet){
if (!mqttClient.connected()) { if (!mqttClient.connected()) {
Serial.println("Reconnecting to mqtt"); if (loopmillis-last_mqttreconnectattempt > MQTTRECONNECTDELAY) {
reconnect(); Serial.println("Reconnecting to mqtt");
reconnect();
last_mqttreconnectattempt=loopmillis;
}
} }
mqttClient.loop(); mqttClient.loop();
} }
@ -347,6 +369,7 @@ void loop() {
int _motormove=0; //negative: move left, positive: move right. abs value: speed. 0 <= abs(_motormove) <= 255 int _motormove=0; //negative: move left, positive: move right. abs value: speed. 0 <= abs(_motormove) <= 255
potidifference_integral+=potidifference*motorI; potidifference_integral+=potidifference*motorI;
potidifference_integral=constrain(potidifference_integral,-MOTORI_ANTIWINDUP,MOTORI_ANTIWINDUP); //constrain
_motormove=potidifference*motorP+potidifference_integral; _motormove=potidifference*motorP+potidifference_integral;
motorspeed=constrain(abs(_motormove), 0,255); motorspeed=constrain(abs(_motormove), 0,255);
if (poti_read<=POT_MIN && _motormove<0) { //stop motor if soft endstops reached and wants to turn that way if (poti_read<=POT_MIN && _motormove<0) { //stop motor if soft endstops reached and wants to turn that way
@ -436,10 +459,11 @@ void sendData() {
void callback(char* topic, byte* payload, unsigned int length) { void callback(char* topic, byte* payload, unsigned int length) {
payload[length] = '\0'; //add end of string character payload[length] = '\0'; //add end of string character
String spayload = String((char*)payload);
Serial.print("Message arrived ["); Serial.print("Message arrived [");
Serial.print(topic); Serial.print(topic);
Serial.print("] ");//MQTT_BROKER Serial.print("] ");//MQTT_BROKER
for (int i = 0; i < length; i++) { for (unsigned int i = 0; i < length; i++) {
Serial.print((char)payload[i]); Serial.print((char)payload[i]);
} }
Serial.println(); Serial.println();
@ -449,19 +473,112 @@ void callback(char* topic, byte* payload, unsigned int length) {
//if (strncmp((const char*)payload, "ON", 2) == 0) { //if (strncmp((const char*)payload, "ON", 2) == 0) {
//} //}
if (strncmp((const char*)topic, "audiomixer/main/volume/set",sizeof(topic)) == 0) { //if (strncmp((const char*)topic, "audiomixer/volume/set",sizeof(topic)) == 0) {
if (String(topic).equals("audiomixer/volume/set")){
//Serial.println("republish"); //Serial.println("republish");
String s = String((char*)payload); float _floatvalue = spayload.toFloat();
Serial.print("Stringreceived="); _floatvalue=constrain(_floatvalue,0.0,100.0);
Serial.println(s); Serial.print("Volume string=");
float _floatvalue = s.toFloat(); Serial.println(spayload);
Serial.print("setvalue="); Serial.print("setvalue=");
Serial.println(_floatvalue); 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
mqttClient.publish("audiomixer/main/volume", payload, length );
char pub_payload[8]; // Buffer big enough for 7-character float
dtostrf(_floatvalue, 1, 2, pub_payload);
mqttClient.publish("audiomixer/volume", pub_payload);
}else if (String(topic).equals("audiomixer/mute/set")) { //withouth range
Serial.print("Mute string="); Serial.println(spayload);
uint16_t ipayload=spayload.toInt();
if (spayload.equalsIgnoreCase("false")) {
setMuteInt(0); //all unmuted
}else if (spayload.equalsIgnoreCase("true")) {
setMuteInt(pow(2,NUMMUTECHANNELS)-1); //all muted
}else if (ipayload>=0 && ipayload<((uint16_t)1<<NUMMUTECHANNELS)){ //in range
setMuteInt(ipayload); //set bits directly
}
//publish all states
for (uint8_t i=0;i<NUMMUTECHANNELS;i++) {
String pub_topic = "audiomixer/mute_"+String(i);
boolean _mutestate=getMute(i);
if (_mutestate) {
mqttClient.publish((char*) pub_topic.c_str(), "true");
}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");
}
}
}else if (String(topic).equals("audiomixer/select/set")) { //withouth range
Serial.print("Select string="); Serial.println(spayload);
uint16_t ipayload=spayload.toInt();
if (spayload.equalsIgnoreCase("false")) {
setSelectionInt(0); //all select to NC
}else if (spayload.equalsIgnoreCase("true")) {
setSelectionInt(pow(2,NUMSELECTCHANNELS)-1); //all select to NO
}else if (ipayload>=0 && ipayload<((uint16_t)1<<NUMSELECTCHANNELS)){ //in range
setSelectionInt(ipayload); //set bits directly
}
//publish all states
for (uint8_t i=0;i<NUMSELECTCHANNELS;i++) {
String pub_topic = "audiomixer/select_"+String(i);
boolean _selectstate=getSelection(i);
if (_selectstate) {
mqttClient.publish((char*) pub_topic.c_str(), "true");
}else{
mqttClient.publish((char*) pub_topic.c_str(), "false");
}
}
}else if (String(topic).startsWith("audiomixer/select_")) {
Serial.print("Select string="); Serial.println(spayload);
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;
}
}
Serial.print("Found index:"); Serial.println(_index);
if (_index==255) {
Serial.println("Index out of range");
}else{ //index ok
String pub_topic = "audiomixer/select_"+String(_index);
if (spayload.equalsIgnoreCase("false")) {
setSelectionChannel(_index,false);
mqttClient.publish((char*) pub_topic.c_str(), "false");
}else if (spayload.equalsIgnoreCase("true")) {
setSelectionChannel(_index,true);
mqttClient.publish((char*) pub_topic.c_str(), "true");
}
}
} }
@ -475,6 +592,9 @@ void srWrite(uint8_t pbit, boolean state){ //change bit to state
}else{ }else{
srbits &= ~(1UL << pbit); //clear bit srbits &= ~(1UL << pbit); //clear bit
} }
srShiftOut();
}
void srShiftOut(){
digitalWrite(SRLATCH, LOW); digitalWrite(SRLATCH, LOW);
shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits>>8); shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits>>8);
shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits); shiftOut(SRDATA, SRCLOCK, MSBFIRST, srbits);
@ -497,3 +617,33 @@ uint32_t Wheel(byte WheelPos) {
WheelPos -= 170; WheelPos -= 170;
return leds.Color(WheelPos * 3, 255 - WheelPos * 3, 0); return leds.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} }
void setMuteInt(uint8_t i) {
uint16_t mask=(( (uint16_t)1<<(NUMMUTECHANNELS))-1 )<<NUMSELECTCHANNELS;
srbits = ((i<<NUMSELECTCHANNELS) & mask) | (srbits & ~mask);
srShiftOut();
Serial.println(srbits);
}
boolean getMute(uint8_t pbit) {
return srbits & (1<<(pbit+NUMSELECTCHANNELS)); //check bit at position
}
void setSelectionInt(uint8_t i) {
uint16_t mask=(( (uint16_t)1<<(NUMMUTECHANNELS))-1 )<<NUMSELECTCHANNELS;
srbits = (srbits & mask) | (i & ~mask);
srShiftOut();
}
boolean getSelection(uint8_t pbit) {
return srbits & (1<<pbit); //check bit at position
}
void setSelectionChannel(uint8_t i, boolean state){
if (i<NUMSELECTCHANNELS) {
srWrite(i, state);
}
}
void setMuteChannel(uint8_t i, boolean state){
if (i<NUMMUTECHANNELS) {
srWrite(i+NUMSELECTCHANNELS, state); //offset. selection is first shift register
}
}