SWRMeter/command_functions.c

179 lines
4.9 KiB
C

/*
* Author: klaute -Kai Lauterbach - @kailauterbach - me@klaute.de
* Date: 08/2016
* License: GPLv3
*/
/*****************************************************************************/
extern void USB_serialStreamWriteC(char*, uint16_t);
extern void USB_serialStreamWrite(char*);
/*****************************************************************************/
// send the key configuration to the USB host
void cc_getConfig()
{
char* sBody = " ";
sprintf(sBody, "%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_CONFIG);
USB_serialStreamWrite(sBody);
for (uint8_t key = 0; key < EEP_KEY_CNT; key++)
{
char* sKey = " ";
sprintf(sKey, "%c", key);
USB_serialStreamWriteC(sKey, 1);
for (uint8_t seqnum = 0; seqnum < EEP_KEY_SEQ_LEN; seqnum++)
{
char* sSeq = " ";
sprintf(sSeq, "%c%c", key_config[key][seqnum][0], key_config[key][seqnum][1]);
USB_serialStreamWriteC(sSeq, 2);
}
}
sBody = " ";
sprintf(sBody, "%c%c", MSG_EOM1, MSG_EOM2);
USB_serialStreamWrite(sBody);
}
/*****************************************************************************/
// receive the key configuration for one key
void cc_setKeyConfig()
{
uint8_t key = cc_read_data[0];
uint8_t seqnum = 0;
for (uint8_t pos = 1; pos < EEP_KEY_SEQ_LEN; pos += EEP_KEY_TUPLE_LEN)
{
key_config[key][seqnum][0] = cc_read_data[pos]; // first byte f the tuple
key_config[key][seqnum][1] = cc_read_data[pos+1]; // second byte f the tuple
seqnum++;
}
// TODO should be called by an other function
ch_writeConfig();
char* sBody = " ";
sprintf(sBody, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2);
USB_serialStreamWrite(sBody);
}
/*****************************************************************************/
void cc_setLEDState()
{
uint8_t led = cc_read_data[0]; // LED number
uint8_t state = cc_read_data[1]; // state
if (led < 0 || led >= EEP_KEY_CNT)
{
char* sBody = " ";
sprintf(sBody, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_ANSWER_NOK, MSG_EOM1, MSG_EOM2);
USB_serialStreamWrite(sBody);
return;
}
if (state == 0)
lm_ledOff(led);
else
lm_ledOn(led);
char* sBody = " ";
sprintf(sBody, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2);
USB_serialStreamWrite(sBody);
}
/*****************************************************************************/
void cc_setOSType()
{
cn_writeOSType(cc_read_data[0]);
char* sBody = " ";
sprintf(sBody, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2);
USB_serialStreamWrite(sBody);
}
/*****************************************************************************/
void cc_startBootloader()
{
// enable the watchdog without a time interval to use
wdt_enable(0);
// trigger watchdog
while(1) {};
}
/*****************************************************************************/
// Wert: | 0x80 | 0x40 | 0x20 | 0x10 | 0x08 | 0x04 | 0x02 | 0x01
// Wert: | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1
// Bit: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
// Descr: | delay | - | fn | meta | altgr | alt | ctrl | shift
// max 40 byte / 20 tupel
uint8_t sample_seq[] = {
0x01, 0x0b, // H
0x80, 0x0f, // delay 16 cycles
0x00, 0x04, // a
0x00, 0x0f, // l
0x80, 0x80, // delay 128 cycles
0x00, 0x0f, // l
0x00, 0x12, // o
0x80, 0x0f, // delay 16 cycles
0x10, 0x00, // meta modifier
0x80, 0x80, // delay 128
0x10, 0x00, // meta modifier
0xff, 0xff, // in case that length of the sequence is less than 40 byte add a (0xff, 0xff) tuple - only allowed firmware internal
0xff, 0xff, // one 0xff 0xff tuple is required, the other tuples are added to clean up the eeprom content
0xff, 0xff,
0xff, 0xff,
0xff, 0xff,
0xff, 0xff,
0xff, 0xff,
0xff, 0xff,
0xff, 0xff,
};
// Example hexadecimal data:
// 2 byte: message header
// 1 byte: 0x14 = set configuration command identifier
// 1 byte: 0x00 = key number to modify
// 40 byte: sequence data (tuples) full filled using (0xff, 0xff) tuples
// 2 byte: message footer
// 3C3E1400010B80FF000A000F8080000F001280FF100080801000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0D0A
void cc_saveFullSampleCfg()
{
for (uint8_t k = 0; k < EEP_KEY_CNT; k++)
{
for (uint8_t s = 0; s < 40; s++)
{
ch_writeKeyConfigMV(k, s, sample_seq[2*s], sample_seq[2*s+1]);
}
}
ch_readConfig();
char* sBody = " ";
sprintf(sBody, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2);
USB_serialStreamWrite(sBody);
}
/*****************************************************************************/
void cc_setKeyStrokeDelay()
{
ch_setKeyStrokeDelay(cc_read_data[0]);
char* sBody = " ";
sprintf(sBody, "%c%c%c%c%c", MSG_SOM1, MSG_SOM2, MSG_TYPE_ANSWER_OK, MSG_EOM1, MSG_EOM2);
USB_serialStreamWrite(sBody);
}
/*****************************************************************************/