Код HTML:
#include <IRremote.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <MeetAndroid.h>
#define HEATER1 19
#define HEATER2 41
#define AMP_ON 16
#define AUDIO_MUTE 17
#define ONE_WIRE_BUS 25
const int RECV_PIN = 26;
#define AUDIO_ON 44
#define CAM 10
#define TDA7318_I2C_ADDRESS 0x44
#define TDA_SW1 0x58
#define TDA_SW2 0x59
#define TDA_SW3 0x5A
#define TDA_SW4 0x5B
// LM pins
#define LM_CE 14 //LM7001 PIN3
#define LM_CL 13 //LM7001 PIN4
#define LM_DA 12 //LM7001 PIN5
// LM delay in microsec
#define LM_DELAY 2
#define RADIO_MIN_FREQUENCY 880
#define RADIO_MAX_FREQUENCY 1080
#define RADIO_SOURCE 2
#define MUSIC_SOURCE 3
DeviceAddress intTempSensor = { 0x28, 0x44, 0x0A, 0xD8, 0x02, 0x00, 0x00, 0x58 };
DeviceAddress extTempSensor = { 0x28, 0xA8, 0xE4, 0x7D, 0x02, 0x00, 0x00, 0x5C };
DeviceAddress hlTempSensor = { 0x28, 0x7C, 0xDF, 0xD7, 0x02, 0x00, 0x00, 0x02 };
DeviceAddress hrTempSensor = { 0x28, 0xB6, 0x1A, 0x7E, 0x02, 0x00, 0x00, 0x98 };
byte volMap[] = {0x3F,0x3D,0x3B,0x39,0x37,0x35,0x33,0x31,
0x2F,0x2D,0x2B,0x29,0x27,0x25,0x23,0x21,
0x1F,0x1D,0x1B,0x19,0x17,0x15,0x13,0x11,
0x0F,0x0D,0x0B,0x09,0x07,0x05,0x03,0x00};
byte lfAttMap[] = {0x9F,0x9D,0x9B,0x99,0x97,0x95,0x93,0x91,
0x8F,0x8D,0x8B,0x89,0x87,0x85,0x83,0x80};
byte rfAttMap[] = {0xBF,0xBD,0xBB,0xB9,0xB7,0xB5,0xB3,0xB1,
0xAF,0xAD,0xAB,0xA9,0xA7,0xA5,0xA3,0xA0};
byte lrAttMap[] = {0xDF,0xDD,0xDB,0xD9,0xD7,0xD5,0xD3,0xD1,
0xCF,0xCD,0xCB,0xC9,0xC7,0xC5,0xC3,0xC0};
byte rrAttMap[] = {0xFF,0xFD,0xFB,0xF9,0xF7,0xF5,0xF3,0xF1,
0xEF,0xED,0xEB,0xE9,0xE7,0xE5,0xE3,0xE0};
byte bassMap[] = {0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x6F,
0x6E,0x6D,0x6C,0x6B,0x6A,0x69,0x68};
byte trebleMap[] = {0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x7F,
0x7E,0x7D,0x7C,0x7B,0x7A,0x79,0x78};
byte currentVolume = 16;
byte currentHeaterOff1 = 0;
byte currentHeaterOn1 = 0;
byte currentHeaterOff2 = 0;
byte currentHeaterOn2 = 0;
unsigned long heaterTimer1 = 150000;
unsigned long heaterTimer2 = 0;
unsigned long camTimer = 0;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
IRrecv irrecv(RECV_PIN);
decode_results results;
MeetAndroid meetAndroid;
HardwareSerial Uart = HardwareSerial();
void setup() {
Uart.begin(38400);
Serial.begin(9600);
setCallbacks();
initTempSensors();
initTda();
initLM();
initHeaters();
initIR();
pinMode(AMP_ON, OUTPUT);
pinMode(AUDIO_ON, OUTPUT);
pinMode(AUDIO_MUTE, OUTPUT);
pinMode(CAM, INPUT);
}
void loop() {
meetAndroid.receive();
receiveIR();
receiveCAM();
checkHeaters();
}
void setCallbacks() {
meetAndroid.registerFunction(getInternalTemperature, 'A'); //Internal temperature: 1
meetAndroid.registerFunction(setAudioBalance, 'B'); //Audio balance: 0-15
meetAndroid.registerFunction(setHeaterOnTemperature1, 'G'); //Heater temp 1 ON: 25-ON, 0-OFF
meetAndroid.registerFunction(setHeaterOnTemperature2, 'H'); //Heater temp 2 ON: 25-ON, 0-OFF
meetAndroid.registerFunction(getExternalTemperature, 'E'); //External temperature: 1
meetAndroid.registerFunction(setAudioBass, 'J'); //Audio bass: 0-14
meetAndroid.registerFunction(setAudioTreble, 'K'); //Audio treble: 0-14
meetAndroid.registerFunction(setAudioRearLeftVolume, 'L'); //Audio rear left volume: 0-15
meetAndroid.registerFunction(setAudioMute, 'M'); //Audio mute: 1-ON, 0-OFF
meetAndroid.registerFunction(setHeaterOffTemperature1, 'N'); //Heater temp 1 OFF: 25-OFF, 0-OFF
meetAndroid.registerFunction(setHeaterOffTemperature2, 'O'); //Heater temp 2 OFF: 25-OFF, 0-OFF
meetAndroid.registerFunction(setRadioFrequency, 'R'); //Radio frequency: (880-1080)-ON, 0-OFF
meetAndroid.registerFunction(setAudioSource, 'S'); //Audio source: 2-radio, 3-music
meetAndroid.registerFunction(setAudioVolume, 'V'); //Audio volume: 0-31
meetAndroid.registerFunction(setAudioRearRightVolume, 'X'); //Audio rear right volume: 0-15
meetAndroid.registerFunction(getHeaterTemperature1, 'Y'); //Heater 1
meetAndroid.registerFunction(getHeaterTemperature2, 'Z'); //Heater 2
}
void initTempSensors() {
sensors.begin();
// set the resolution to 9 bit
sensors.setResolution(intTempSensor, 9);
sensors.setResolution(extTempSensor, 9);
sensors.setResolution(hlTempSensor, 9);
sensors.setResolution(hrTempSensor, 9);
}
void initTda() {
sendAudioMute(1);
sendAudioOn(1);
delay(3000);
sendAmpOn(1);
Wire.begin(); // join i2c bus (address optional for master)
sendAudioVolume(16);
sendAudioLFAttenuator(15);
sendAudioRFAttenuator(15);
sendAudioLRAttenuator(15);
sendAudioRRAttenuator(15);
sendAudioSwitch(3);
sendAudioBass(7);
sendAudioTreble(7);
sendAudioMute(0);
}
void initLM() {
pinMode(LM_CE, OUTPUT);
pinMode(LM_CL, OUTPUT);
pinMode(LM_DA, OUTPUT);
}
void initHeaters() {
pinMode(HEATER1, OUTPUT);
pinMode(HEATER2, OUTPUT);
}
void initIR() {
irrecv.enableIRIn();
irrecv.blink13(true);
}
void receiveIR() {
if (irrecv.decode(&results)) {
char buf[50];
sprintf(buf, "IR:%u", results.value);
meetAndroid.send(buf);
irrecv.resume();
}
}
void receiveCAM() {
int value = digitalRead(CAM);
if (value == 1 && camTimer == 0) {
camTimer = 3000;
meetAndroid.send("CAM:1");
}
if (camTimer > 0) {
camTimer--;
}
}
void checkHeaters() {
if (currentHeaterOff1 == 0 && currentHeaterOn1 == 0) {
//Serial.println("HEATER1: OFF");
digitalWrite(HEATER1, LOW);
}
else {
if (heaterTimer1 == 300000) {
heaterTimer1 = 0;
sensors.requestTemperaturesByAddress(hlTempSensor);
int tempL = sensors.getTempC(hlTempSensor);
if (tempL != -127) {
if (tempL >= currentHeaterOff1) {
Serial.println("HEATER1: OFF");
digitalWrite(HEATER1, LOW);
}
if (tempL <= currentHeaterOn1) {
Serial.println("HEATER1: ON");
digitalWrite(HEATER1, HIGH);
}
}
}
heaterTimer1++;
}
if (currentHeaterOff2 == 0 && currentHeaterOn2 == 0) {
//Serial.println("HEATER2: OFF");
digitalWrite(HEATER2, LOW);
}
else {
if (heaterTimer2 == 300000) {
heaterTimer2 = 0;
sensors.requestTemperaturesByAddress(hrTempSensor);
int tempR = sensors.getTempC(hrTempSensor);
if (tempR != -127) {
if (tempR >= currentHeaterOff2) {
Serial.println("HEATER2: OFF");
digitalWrite(HEATER2, LOW);
}
if (tempR <= currentHeaterOn2) {
Serial.println("HEATER2: ON");
digitalWrite(HEATER2, HIGH);
}
}
}
heaterTimer2++;
}
}
void writeI2c(byte address, byte value) {
Wire.beginTransmission(address);
Wire.send(value);
Wire.endTransmission();
}
void sendAudioMute(byte value) {
digitalWrite(AUDIO_MUTE, value == 1 ? HIGH : LOW);
}
void sendAmpOn(byte value) {
digitalWrite(AMP_ON, value == 1 ? HIGH : LOW);
}
void sendAudioOn(byte value) {
digitalWrite(AUDIO_ON, value == 1 ? HIGH : LOW);
}
void sendAudioVolume(byte value) {
if (value > 31) return;
currentVolume = value;
writeI2c(TDA7318_I2C_ADDRESS, volMap[value]);
}
void sendAudioLFAttenuator(byte value) {
if (value > 15) return;
writeI2c(TDA7318_I2C_ADDRESS, lfAttMap[value]);
}
void sendAudioRFAttenuator(byte value) {
if (value > 15) return;
writeI2c(TDA7318_I2C_ADDRESS, rfAttMap[value]);
}
void sendAudioLRAttenuator(byte value) {
if (value > 15) return;
writeI2c(TDA7318_I2C_ADDRESS, lrAttMap[value]);
}
void sendAudioRRAttenuator(byte value) {
if (value > 15) return;
writeI2c(TDA7318_I2C_ADDRESS, rrAttMap[value]);
}
void sendAudioBass(byte value) {
if (value > 14) return;
writeI2c(TDA7318_I2C_ADDRESS, bassMap[value]);
}
void sendAudioTreble(byte value) {
if (value > 14) return;
writeI2c(TDA7318_I2C_ADDRESS, trebleMap[value]);
}
void sendAudioSwitch(byte value) {
switch (value) {
case 1:
writeI2c(TDA7318_I2C_ADDRESS, TDA_SW1);
break;
case 2:
writeI2c(TDA7318_I2C_ADDRESS, TDA_SW2);
break;
case 3:
writeI2c(TDA7318_I2C_ADDRESS, TDA_SW3);
break;
case 4:
writeI2c(TDA7318_I2C_ADDRESS, TDA_SW4);
break;
}
}
void sendRadioFrequency(int frequency, boolean power) {
frequency += 107;
digitalWrite(LM_CE, HIGH);
writeLM(byte(frequency));
writeLM(byte(frequency >> 8));
writeLM((power == true) ? 135 : 71);
digitalWrite(LM_CE, LOW);
}
void writeLM(byte bytes) {
int d;
int d1;
delayMicroseconds(LM_DELAY);
for (int x=0; x <= 7; x++) {
// Set DA
d = bytes >> 1;
d1 = d << 1;
digitalWrite(LM_DA, bytes == d1 ? LOW : HIGH);
// Make CL
digitalWrite(LM_CL, HIGH);
delayMicroseconds(LM_DELAY);
digitalWrite(LM_CL, LOW);
delayMicroseconds(LM_DELAY);
bytes = bytes >> 1;
}
delayMicroseconds(LM_DELAY);
}
void getInternalTemperature(byte flag, byte numOfValues) {
sensors.requestTemperaturesByAddress(intTempSensor);
float tempC = sensors.getTempC(intTempSensor);
if (tempC == -127.00) {
tempC = 0;
}
char buf[50];
sprintf(buf, "IT:%u", (int)tempC);
meetAndroid.send(buf);
}
void getExternalTemperature(byte flag, byte numOfValues) {
sensors.requestTemperaturesByAddress(extTempSensor);
float tempC = sensors.getTempC(extTempSensor);
if (tempC == -127.00) {
tempC = 0;
}
char buf[50];
sprintf(buf, "ET:%u", (int)tempC);
meetAndroid.send(buf);
}
void setAudioBalance(byte flag, byte numOfValues) {
int value = meetAndroid.getInt();
sendAudioLFAttenuator(value);
sendAudioRFAttenuator(15 - value);
}
void setHeaterOnTemperature1(byte flag, byte numOfValues) {
currentHeaterOn1 = meetAndroid.getInt();
}
void setHeaterOffTemperature1(byte flag, byte numOfValues) {
currentHeaterOff1 = meetAndroid.getInt();
}
void setHeaterOnTemperature2(byte flag, byte numOfValues) {
currentHeaterOn2 = meetAndroid.getInt();
}
void setHeaterOffTemperature2(byte flag, byte numOfValues) {
currentHeaterOff2 = meetAndroid.getInt();
}
void setAudioBass(byte flag, byte numOfValues) {
sendAudioBass(meetAndroid.getInt());
}
void setAudioTreble(byte flag, byte numOfValues) {
sendAudioTreble(meetAndroid.getInt());
}
void setAudioRearLeftVolume(byte flag, byte numOfValues) {
sendAudioLRAttenuator(meetAndroid.getInt());
}
void setAudioMute(byte flag, byte numOfValues) {
sendAudioMute(meetAndroid.getInt());
}
void setRadioFrequency(byte flag, byte numOfValues) {
int frequency = meetAndroid.getInt();
if (frequency == 0) {
sendRadioFrequency(frequency, false);
sendAudioSwitch(MUSIC_SOURCE);
}
else if (frequency >= RADIO_MIN_FREQUENCY && frequency <= RADIO_MAX_FREQUENCY){
sendRadioFrequency(frequency, true);
sendAudioSwitch(RADIO_SOURCE);
}
}
void setAudioSource(byte flag, byte numOfValues) {
sendAudioSwitch(meetAndroid.getInt());
}
void setAudioVolume(byte flag, byte numOfValues) {
sendAudioVolume(meetAndroid.getInt());
}
void setAudioRearRightVolume(byte flag, byte numOfValues) {
sendAudioRRAttenuator(meetAndroid.getInt());
}
void getHeaterTemperature1(byte flag, byte numOfValues) {
sensors.requestTemperaturesByAddress(hlTempSensor);
float tempC = sensors.getTempC(hlTempSensor);
if (tempC == -127.00) {
tempC = 0;
}
char buf[50];
sprintf(buf, "LT:%u", (int)tempC);
meetAndroid.send(buf);
}
void getHeaterTemperature2(byte flag, byte numOfValues) {
sensors.requestTemperaturesByAddress(hrTempSensor);
float tempC = sensors.getTempC(hrTempSensor);
if (tempC == -127.00) {
tempC = 0;
}
char buf[50];
sprintf(buf, "RT:%u", (int)tempC);
meetAndroid.send(buf);
}