add led set topic and reduce code size by removing serial prints
This commit is contained in:
parent
ad13789b12
commit
fe83cff969
|
@ -1,12 +1,13 @@
|
|||
/*
|
||||
* Ideas/TODO:
|
||||
* POT_MIN, POT_MAX as variable with calibration procedure. Drive slowly to both ends until value does not get lower.
|
||||
* Motor error checking. Timeout overall (if regulation fails or stuck). Timeout movement (motor is tunring but no change in poti value detected). Move right direction.
|
||||
* Hardware: motorentstörkondensatoren einbauen direkt an motor. 47nF + zu - und zwei 10nF + zu case und - zu case
|
||||
* PI Optimieren. aktuell overshoot
|
||||
* Implement knob menu structure
|
||||
*/
|
||||
|
||||
//#define DEBUG
|
||||
//#define INFOMSG //print info messages
|
||||
|
||||
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <Adafruit_NeoPixel.h>
|
||||
|
@ -31,12 +32,16 @@ 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 changeRelaisByNumberTopic(uint8_t pn, String pTopicPrefix, String pTopic, String pspayload, void (*psetXChannel) (uint8_t, boolean));
|
||||
void changeRelaisByNumberTopic(uint8_t pn, String pTopicPrefix, uint8_t pindex, String pspayload, void (*psetXChannel) (uint8_t, boolean));
|
||||
void setRelaisByNumber(uint8_t pn, String pTopicPrefix, uint8_t pnumber, bool pstate, void (*psetXChannel) (uint8_t, boolean));
|
||||
float getSetVolume();
|
||||
uint8_t getIndex(uint8_t pn, String pTopic);
|
||||
|
||||
#define LEDPIN 9 //PB1 = D9 = Pin15
|
||||
Adafruit_NeoPixel leds = Adafruit_NeoPixel(9, LEDPIN, NEO_GRB + NEO_KHZ800);
|
||||
#define NUMLED 9
|
||||
Adafruit_NeoPixel leds = Adafruit_NeoPixel(NUMLED, LEDPIN, NEO_GRB + NEO_KHZ800);
|
||||
uint32_t led_colors_standby[NUMLED];
|
||||
|
||||
|
||||
uint8_t wheelpos=0;
|
||||
#include "Ethernet.h"
|
||||
|
@ -160,6 +165,8 @@ long last_ledupdate=0;
|
|||
#define INTERVAL_LEDUPDATE 50
|
||||
|
||||
void setup() {
|
||||
led_colors_standby[NUMLED-1] = Wheel(100); //set default color for volume led
|
||||
|
||||
pinMode(PIN_BUTTON,INPUT_PULLUP);
|
||||
button_knob = Button();
|
||||
|
||||
|
@ -170,35 +177,48 @@ void setup() {
|
|||
pinMode(SRCLOCK, OUTPUT);
|
||||
pinMode(SRDATA, OUTPUT);
|
||||
|
||||
leds.begin();
|
||||
leds.clear();
|
||||
/*for(uint8_t i=0;i<leds.numPixels();i++){ //set color of all leds
|
||||
|
||||
}*/
|
||||
leds.setPixelColor(0, leds.Color(20,20,0)); leds.setPixelColor(NUMLED-1, leds.Color(20,20,20)); leds.show();
|
||||
|
||||
|
||||
#if defined(INFOMSG) || defined(DEBUG)
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {};
|
||||
Serial.println("Boot");
|
||||
#endif
|
||||
|
||||
leds.begin();
|
||||
leds.clear();
|
||||
for(uint8_t i=0;i<leds.numPixels();i++){ //set color of all leds
|
||||
leds.setPixelColor(i, leds.Color(20,20,0));
|
||||
}
|
||||
|
||||
leds.show();
|
||||
|
||||
|
||||
if (useethernet)
|
||||
{
|
||||
leds.setPixelColor(1, leds.Color(0,20,0)); leds.show();
|
||||
#ifdef INFOMSG
|
||||
Serial.println("DHCP..");
|
||||
#endif
|
||||
if (Ethernet.begin(mac) == 0) { // setup ethernet communication using DHCP
|
||||
leds.setPixelColor(2, leds.Color(20,0,0)); leds.show();
|
||||
useethernet=false;
|
||||
//Unable to configure Ethernet using DHCP
|
||||
#ifdef INFOMSG
|
||||
Serial.println("DHCP Err");
|
||||
#endif
|
||||
delay(200);
|
||||
//for (;;);
|
||||
|
||||
|
||||
}else{
|
||||
leds.setPixelColor(2, leds.Color(0,20,0)); leds.show();
|
||||
useethernet=true;
|
||||
#ifdef INFOMSG
|
||||
Serial.print("IP: ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
Serial.println();
|
||||
#endif
|
||||
|
||||
ip = String (Ethernet.localIP()[0]);
|
||||
ip = ip + ".";
|
||||
|
@ -207,17 +227,28 @@ void setup() {
|
|||
ip = ip + String (Ethernet.localIP()[2]);
|
||||
ip = ip + ".";
|
||||
ip = ip + String (Ethernet.localIP()[3]);
|
||||
|
||||
//Serial.println(ip);
|
||||
|
||||
// setup mqtt client
|
||||
#ifdef INFOMSG
|
||||
Serial.println("Conf MQTT");
|
||||
#endif
|
||||
mqttClient.setClient(ethClient);
|
||||
leds.setPixelColor(3, leds.Color(0,20,0)); leds.show();
|
||||
mqttClient.setServer(MQTTBROKER, MQTTPORT);
|
||||
leds.setPixelColor(4, leds.Color(0,20,0)); leds.show();
|
||||
#ifdef INFOMSG
|
||||
Serial.println("MQTT ok");
|
||||
#endif
|
||||
mqttClient.setCallback(callback);
|
||||
leds.setPixelColor(5, leds.Color(0,20,0)); leds.show();
|
||||
}
|
||||
}else{
|
||||
leds.setPixelColor(1, leds.Color(20,0,0)); leds.show();
|
||||
#ifdef INFOMSG
|
||||
Serial.println("Eth disabled");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
@ -229,15 +260,21 @@ void setup() {
|
|||
|
||||
last_send = millis();
|
||||
|
||||
leds.setPixelColor(6, leds.Color(0,20,0)); leds.show();
|
||||
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
// Loop until reconnected
|
||||
if (!mqttClient.connected()) {
|
||||
#ifdef INFOMSG
|
||||
Serial.print("MQTT ..");
|
||||
#endif
|
||||
// Attempt to connect
|
||||
if (mqttClient.connect(CLIENT_ID)) {
|
||||
#ifdef INFOMSG
|
||||
Serial.println("connected");
|
||||
#endif
|
||||
publishCurrentSetVolume();
|
||||
mqttClient.publish("audiomixer/ip", ip.c_str()); //Publish own ip
|
||||
mqttClient.subscribe("audiomixer/volume/set"); //subscribe to /set, republish without /set
|
||||
|
@ -252,9 +289,16 @@ void reconnect() {
|
|||
mqttClient.subscribe((char*) sub_topic.c_str());
|
||||
}
|
||||
|
||||
for (uint8_t i=0;i<NUMLED;i++) { //with range
|
||||
String sub_topic="audiomixer/led_"+String(i)+"/set";
|
||||
mqttClient.subscribe((char*) sub_topic.c_str());
|
||||
}
|
||||
|
||||
} else {
|
||||
#ifdef INFOMSG
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(mqttClient.state());
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -265,7 +309,9 @@ void loop() {
|
|||
if (useethernet){
|
||||
if (!mqttClient.connected()) {
|
||||
if (loopmillis-last_mqttreconnectattempt > MQTTRECONNECTDELAY) {
|
||||
#ifdef INFOMSG
|
||||
Serial.println("Recon. mqtt");
|
||||
#endif
|
||||
reconnect();
|
||||
last_mqttreconnectattempt=loopmillis;
|
||||
}
|
||||
|
@ -488,8 +534,9 @@ void loop() {
|
|||
switch(menu_mode) {
|
||||
case 0: //volume
|
||||
if (poti_reachedposition) { //idle
|
||||
for(uint8_t i=0;i<leds.numPixels()-1;i++){ //set color of all front leds
|
||||
leds.setPixelColor(i, Wheel(wheelpos+i*10));
|
||||
for(uint8_t i=0;i<leds.numPixels();i++){ //set color of all front leds
|
||||
//leds.setPixelColor(i, Wheel(wheelpos+i*10));
|
||||
leds.setPixelColor(i, led_colors_standby[i]);
|
||||
}
|
||||
}else{ //motor is currently moving
|
||||
leds.clear();
|
||||
|
@ -539,7 +586,7 @@ void loop() {
|
|||
|
||||
|
||||
//Volume LED
|
||||
leds.setPixelColor(leds.numPixels()-1, Wheel(wheelpos)); //volume poti led
|
||||
//leds.setPixelColor(leds.numPixels()-1, Wheel(wheelpos)); //volume poti led
|
||||
|
||||
leds.show();
|
||||
}
|
||||
|
@ -567,7 +614,9 @@ void loop() {
|
|||
*/
|
||||
|
||||
if (loopmillis%5001==0) { //TODO: remove when working
|
||||
#ifdef INFOMSG
|
||||
Serial.println(loopmillis); //alive print. for debugging
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
@ -577,18 +626,19 @@ void loop() {
|
|||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
payload[length] = '\0'; //add end of string character
|
||||
String spayload = String((char*)payload);
|
||||
#ifdef INFOMSG
|
||||
Serial.print("msg:");
|
||||
Serial.print(topic);
|
||||
for (unsigned int i = 0; i < length; i++) {
|
||||
Serial.print((char)payload[i]);
|
||||
}
|
||||
Serial.println();
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
if (String(topic).equals("audiomixer/volume/set")){
|
||||
|
||||
float _floatvalue = spayload.toFloat();
|
||||
_floatvalue=constrain(_floatvalue,0.0,100.0);
|
||||
|
||||
|
@ -624,14 +674,29 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|||
publishAllStates(NUMSELECTCHANNELS,"select_", &getSelection);
|
||||
|
||||
}else if (String(topic).startsWith("audiomixer/enable_")) { //with range
|
||||
changeRelaisByNumberTopic(NUMMUTECHANNELS,"audiomixer/enable_", topic, spayload, &setMuteChannel);
|
||||
changeRelaisByNumberTopic(NUMMUTECHANNELS,"audiomixer/enable_", getIndex(NUMMUTECHANNELS, topic), spayload, &setMuteChannel);
|
||||
|
||||
}else if (String(topic).startsWith("audiomixer/select_")) {
|
||||
changeRelaisByNumberTopic(NUMSELECTCHANNELS,"audiomixer/select_", topic, spayload, &setSelectionChannel);
|
||||
changeRelaisByNumberTopic(NUMSELECTCHANNELS,"audiomixer/select_", getIndex(NUMSELECTCHANNELS, topic), spayload, &setSelectionChannel);
|
||||
|
||||
}else if (String(topic).startsWith("audiomixer/led_")) {
|
||||
//Serial.print("Mute string="); Serial.println(spayload);
|
||||
uint8_t _index=getIndex(NUMLED, topic);
|
||||
if (_index<255) { //index ok
|
||||
led_colors_standby[_index] = spayload.toInt();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t getIndex(uint8_t pn, String pTopic) {
|
||||
uint8_t _index=255;
|
||||
for (uint8_t i=0; i<pn && _index==255; i++) {
|
||||
if (String(pTopic).endsWith(String(i)+"/set")) {
|
||||
_index=i;
|
||||
}
|
||||
}
|
||||
return _index;
|
||||
}
|
||||
|
||||
|
||||
|
@ -702,7 +767,9 @@ void publishCurrentSetVolume()
|
|||
char pub_payload[8]; // Buffer big enough for 7-character float
|
||||
dtostrf(_setpercentage, 1, 2, pub_payload);
|
||||
mqttClient.publish("audiomixer/volume", pub_payload);
|
||||
#ifdef INFOMSG
|
||||
Serial.print("pub="); Serial.println(_setpercentage);
|
||||
#endif
|
||||
}
|
||||
|
||||
void publishAllStates(int pn, String pTopicname, boolean (*pgetBit) (uint8_t)){
|
||||
|
@ -717,22 +784,24 @@ void publishAllStates(int pn, String pTopicname, boolean (*pgetBit) (uint8_t)){
|
|||
}
|
||||
}
|
||||
|
||||
void changeRelaisByNumberTopic(uint8_t pn, String pTopicPrefix, String pTopic, String pspayload, void (*psetXChannel) (uint8_t, boolean))
|
||||
void changeRelaisByNumberTopic(uint8_t pn, String pTopicPrefix, uint8_t pindex, String pspayload, void (*psetXChannel) (uint8_t, boolean))
|
||||
{
|
||||
uint8_t _index=255;
|
||||
/*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");
|
||||
}*/
|
||||
if (pindex==255) {
|
||||
#ifdef INFOMSG
|
||||
Serial.println("idx OOR");
|
||||
#endif
|
||||
}else{ //index ok
|
||||
bool _state=false;
|
||||
if (pspayload.equalsIgnoreCase("true")) {
|
||||
_state=true;
|
||||
}
|
||||
setRelaisByNumber(pn, pTopicPrefix, _index, _state, psetXChannel);
|
||||
setRelaisByNumber(pn, pTopicPrefix, pindex, _state, psetXChannel);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue