Lab_interaccio/2012/Edgard/Vdossier03_MEGA/SerialProtocol.ino

310 lines
8 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
char* model = "MoDeL";
char* ip_remote = "IpHoSt";
char* rec = "ReC";
char* stop_cam = "StOp";
char vol_message[] = { // Message template commands
'/', 'V', 'o', 'L',
B0 , B0 , B0, B0,
',', 'i', B0, B0,
B0 , B0 , B0, B0
};
char incomming_message[] = { // Message template commands
'/', 'V', 'o', 'L',
B0 , B0 , B0, B0,
',', 'i', B0, B0,
B0 , B0 , B0, B0
};
uint8_t modelCount =0;
uint8_t ipCount =0;
uint8_t recCount =0;
uint8_t stopCount =0;
uint8_t volCount =0;
boolean ok_model = false;
boolean ok_ip = false;
uint8_t pos_mod_x=0;
uint8_t pos_mod_y=0;
uint8_t pos_ip=0;
void test_vol(byte inByte)
{
if (inByte == vol_message[0])
{
volCount = 0;
incomming_message[volCount] = inByte;
volCount++;
}
else if((inByte == vol_message[volCount])&&(volCount<=11))
{
volCount++;
}
else if ((volCount>11)&&(volCount<16))
{
incomming_message[volCount] = inByte;
volCount++;
if (volCount>15)
{
system_vol = map((byte)incomming_message[15], 0, 255, 0, 100);
writeEEPROM(eeprom, EEI2C_ADDR_system_vol, system_vol);
writeMCP(mcp, 0x00, map(system_vol, 0, 100, 0, 255));
volCount = 0;
}
}
}
void test_model(char inByte)
{
if ((inByte == model[modelCount])&&(!ok_model))
{
modelCount++;
if (modelCount==strlen(model))
{
modelCount=0;
ok_model=true;
}
}
else if (ok_model) save_model(inByte);
else modelCount=0;
}
void test_rec(char inByte)
{
if ((inByte == rec[recCount])&&(!ok_rec))
{
recCount++;
if (recCount==strlen(rec))
{
recCount=0;
ok_rec=true;
frame = 0;
rec_count=0;
bit_count=0;
st_time = true;
}
}
else recCount=0;
}
void test_stop(char inByte)
{
if ((inByte == stop_cam[stopCount])&&(!ok_stop))
{
stopCount++;
if (stopCount==strlen(stop_cam))
{
stopCount=0;
ok_stop=true;
frame = 0;
st_time = false;
}
}
else stopCount=0;
}
void start_time()
{
timecode++;
frame++;
if (frame>sistema) {frame=0; timeseconds++; rec_count++; }
}
void mod_rec()
{
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
if ((frame==1)&&(rec_count==0))
{
if (system_mode)Serial3.println(lancrec,HEX);
else irsend.sendNEC(remoterec, 32);
}
else if ((frame==3)&&(rec_count==0)) Serial3.println();
else if ((bit_count<17)&&(rec_count==1))
{
if (bit_count<16)
{
uint16_t temp = (MORSE[morse_count]>>(15-bit_count))&0x0001;
digitalWrite(aud,temp);
digitalWrite(lamp,temp);
if (system_useSpeaker)digitalWrite(PINS_BUZZER,temp);
}
else
{
digitalWrite(aud,LOW);
digitalWrite(lamp,LOW);
if (system_useSpeaker)digitalWrite(PINS_BUZZER,LOW);
}
bit_count++;
}
else if(rec_count==1) {ok_stop=false; ok_rec=false; state_rec=true; bit_count=0; if (morse_count<27) morse_count++; else morse_count=0; rec_count=0;}
}
void mod_stop()
{
stop_count++;
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
if (stop_count==1)
{
if (system_mode)Serial3.println(lancstop,HEX);
else irsend.sendNEC(remotestop, 32);
}
else if (stop_count==3) Serial3.println();
else if (stop_count>=4) {ok_rec=false; ok_stop=false; state_rec=false; stop_count=0;}
}
void test_ip(char inByte)
{
if ((inByte == ip_remote[ipCount])&&(!ok_ip))
{
ipCount++;
if (ipCount==strlen(ip_remote))
{
ipCount=0;
ok_ip=true;
}
}
else if (ok_ip) save_ip(inByte);
else ipCount=0;
}
void save_model(byte inByte)
{
unsigned int temp_address;
temp_address = EEI2C_ADDR_MODELS + 80*system_num_models;
if (inByte=='/')
{
if(pos_mod_y>0)
{
writeEEPROM(eeprom, temp_address + pos_mod_x + (pos_mod_y - 1)*16 , 0x0D);
}
pos_mod_x=0;
pos_mod_y++;
}
else
{
if ((pos_mod_x<16)&&(pos_mod_y<6))
{
if (pos_mod_y<2)
{
writeEEPROM(eeprom, temp_address + pos_mod_x + (pos_mod_y - 1)*16 , inByte);
pos_mod_x++;
}
else
{
byte temp=0x00;
if (byte_count==1) temp=readEEPROM(eeprom, temp_address + pos_mod_x + (pos_mod_y - 1)*16);
if ((inByte>=0x30)&&(inByte<=0x39)) temp=(temp<<4) + (inByte - 0x30);
else if ((inByte>=0x41)&&(inByte<=0x46)) temp=(temp<<4) + (inByte - 0x37);
writeEEPROM(eeprom, temp_address + pos_mod_x + (pos_mod_y - 1)*16 , temp);
byte_count++;
if (byte_count==2)
{
pos_mod_x++;
byte_count=0;
}
}
}
else if (pos_mod_y>5)
{
ok_model=false;
pos_mod_y = 0;
system_num_models++;
writeEEPROM(eeprom, EEI2C_ADDR_system_num_models + 1, highByte(system_num_models));
writeEEPROM(eeprom, EEI2C_ADDR_system_num_models, lowByte(system_num_models));
}
}
}
void save_ip(byte inByte)
{
if (inByte=='/')
{
if(pos_ip>0)
{
writeEEPROM(eeprom, EEI2C_ADDR_HOST + pos_ip - 1, 0x00);
ip_host[pos_ip - 1] = 0x00;
ok_ip=false;
pos_ip = 0;
software_Reset();
}
else pos_ip=1;
}
else
{
if (pos_ip<17)
{
writeEEPROM(eeprom, EEI2C_ADDR_HOST + pos_ip - 1 , inByte);
ip_host[pos_ip - 1] = inByte;
pos_ip++;
}
}
}
void video_rec()
{
if (timeseconds==timeseconds_next)
{
timeseconds_next = timeseconds + 1;
if (timeseconds==(timetotalvideo-1)) {if(!frec) Serial3.println();}
else if (timeseconds==(timetotalvideo))
{
start_video=false;
digitalWrite(red, HIGH);
digitalWrite(green, HIGH);
digitalWrite(aud,LOW);
digitalWrite(lamp,LOW);
if (system_useSpeaker) digitalWrite(PINS_BUZZER,LOW);
if(!frec)
{
if (system_mode)Serial3.println(lancstop,HEX);
else irsend.sendNEC(remotestop, 32);
}
}
else if ((timeseconds==(timeant-1))&&(system_mode))Serial3.println(); //Un segundo antes de mandar el comando lanc, mando un enter
else if (timeseconds==timeant)
{
if (frec)
{
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
if(system_mode) Serial3.println(lancrec,HEX);
else irsend.sendNEC(remoterec, 32);
}
else
{
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
if (system_mode)Serial3.println(lancstop,HEX);
else irsend.sendNEC(remotestop, 32);
}
}
else if (timeseconds==(timeant+1))
{
timeant = timeant + (timepausevideo*(!frec) + timerecvideo*frec);
frec=!frec;
if(!frec)
{
frames_ant=timecode;
morse_count++;
bit_count = 0;
}
}
}
if ((!frec)&&(bit_count<16)&&((timecode-frames_ant)==1))
{
frames_ant=timecode;
uint16_t temp = (MORSE[morse_count]>>(15-bit_count))&0x0001;
digitalWrite(aud,temp);
digitalWrite(lamp,temp);
if (system_useSpeaker)digitalWrite(PINS_BUZZER,temp);
bit_count++;
}
}