esp-wemos-audioswitcher/firmware_arduino_mini/firmware_arduino_mini.ino

158 lines
3.4 KiB
Arduino
Raw Normal View History

2017-02-21 23:50:55 +00:00
#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;
}
}