initial commit

This commit is contained in:
Lucas Pleß 2017-02-22 00:50:55 +01:00
commit 9e4a28db2e
4 changed files with 224 additions and 0 deletions

BIN
docs/74HC_HCT595.pdf Normal file

Binary file not shown.

BIN
docs/M62429.pdf Normal file

Binary file not shown.

View File

@ -0,0 +1,157 @@
#define PIN_DATA_VOL_A 5
#define PIN_DATA_VOL_B 6
#define PIN_DATA_VOL_C 7
#define PIN_DATA_VOL_D 8
#define PIN_DATA_CLOCK 9
#define PIN_SR_DATA 2
#define PIN_SR_LTCH 3
#define PIN_SR_CLOCK 4
#define PIN_LED 13
long tick;
uint16_t relaisData = 0xffff;
void setup() {
pinMode(PIN_DATA_VOL_A, OUTPUT);
pinMode(PIN_DATA_VOL_B, OUTPUT);
pinMode(PIN_DATA_VOL_C, OUTPUT);
pinMode(PIN_DATA_VOL_D, OUTPUT);
pinMode(PIN_DATA_CLOCK, OUTPUT);
pinMode(PIN_SR_DATA, OUTPUT);
pinMode(PIN_SR_LTCH, OUTPUT);
pinMode(PIN_SR_CLOCK, OUTPUT);
pinMode(PIN_LED, OUTPUT);
// shift out one time 0xffff to set all pins high. our relais boards
// are active-low
shiftRelais(relaisData);
}
void shiftRelais(uint16_t data) {
digitalWrite(PIN_SR_LTCH, LOW);
shiftOut(PIN_SR_DATA, PIN_SR_CLOCK, MSBFIRST, (data >> 8));
shiftOut(PIN_SR_DATA, PIN_SR_CLOCK, MSBFIRST, data);
digitalWrite(PIN_SR_LTCH, HIGH);
}
// channel is our "room" 1-5 / state for switching between A or B input
void switchInputs(uint8_t channel, bool state) {
// 16 . . . . . . . . . . . . . . . . . . . 0
// we use a simple switch case with some if instead of
// tricky bitmagic to allow a more visual programming
// of our relais configuration
switch(channel) {
case 1:
if(state) {
relaisData |= _BV(7) | _BV(6);
} else {
relaisData &= ~(_BV(7) | _BV(6));
}
break;
case 2:
if(state) {
relaisData |= _BV(5) | _BV(4);
} else {
relaisData &= ~(_BV(5) | _BV(4));
}
break;
case 3:
if(state) {
relaisData |= _BV(3) | _BV(2);
} else {
relaisData &= ~(_BV(3) | _BV(2));
}
break;
case 4:
if(state) {
relaisData |= _BV(1) | _BV(0);
} else {
relaisData &= ~(_BV(1) | _BV(0));
}
break;
case 5:
if(state) {
relaisData |= _BV(9) | _BV(8);
} else {
relaisData &= ~(_BV(9) | _BV(8));
}
break;
}
shiftRelais(relaisData);
}
// control a M62429 Chip for Volume Control
// function take from here: http://forum.arduino.cc/index.php?topic=244152.0
void setVolume(uint8_t volume, uint8_t dataPin) {
uint8_t bits;
uint16_t data = 0; // control word is built by OR-ing in the bits
// convert attenuation to volume / remember 0 is full volume!
volume = (volume > 100) ? 0 : (((volume * 83) / -100) + 83);
data |= (0 << 0);
data |= (0 << 1);
data |= ((21 - (volume / 4)) << 2);
data |= ((3 - (volume % 4)) << 7);
data |= (0b11 << 9);
for (bits = 0; bits < 11; bits++) {
_delay_us(2);
digitalWrite(dataPin, 0);
_delay_us (2);
digitalWrite(PIN_DATA_CLOCK, 0);
_delay_us (2);
digitalWrite(dataPin, (data >> bits) & 0x01);
_delay_us (2);
digitalWrite(PIN_DATA_CLOCK, 1);
}
_delay_us(2);
digitalWrite(dataPin, 1); // final clock latches data in
_delay_us (2);
digitalWrite(PIN_DATA_CLOCK, 0);
}
uint8_t volume = 0;
uint8_t room = 0;
bool flag = false;
void loop() {
long currentMillis = millis();
if(currentMillis - tick > 500) {
setVolume(volume, PIN_DATA_VOL_A);
setVolume(volume, PIN_DATA_VOL_B);
setVolume(volume, PIN_DATA_VOL_C);
setVolume(volume, PIN_DATA_VOL_D);
volume++;
if(volume > 100) volume = 0;
switchInputs(room++, flag);
if(room == 5) {
room = 0;
flag = !flag;
}
tick = currentMillis;
}
}

View File

@ -0,0 +1,67 @@
#include <Homie.h>
#include <ArduinoOTA.h>
long lastTickMillis;
HomieNode volumeNode("volume", "volume");
HomieNode switchNode("switches", "switch");
bool nodeInputHandlerVolume(const HomieRange& range, const String& value) {
Homie.getLogger() << "VOL " << range.index << " set to " << value << endl;
int r = value.toInt();
if(r >= 0 && r <= 127) {
volumeNode.setProperty("output").setRange(range).send(value);
return true;
}
return false;
}
bool nodeInputHandlerVolume(const HomieRange& range, const String& value) {
Homie.getLogger() << "VOL " << range.index << " set to " << value << endl;
switchNode.setProperty("switch").setRange(range).send(value);
return true;
}
void setup() {
Serial.begin(115200);
Serial << endl << endl;
Homie_setFirmware("audiocontroller", "1.0.0");
volumeNode.advertiseRange("volume", 1, 4).settable(nodeInputHandlerVolume);
switchNode.advertiseRange("switch", 1, 4).settable(nodeInputHandlerVolume);
Homie.setup();
Homie.getLogger() << "started" << endl;
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
}
void loop() {
Homie.loop();
ArduinoOTA.handle();
long currentMillis = millis();
if(currentMillis - lastTickMillis >= 100) {
lastTickMillis = currentMillis;
}
}