Merge I2C command handler into main #18
@ -54,7 +54,7 @@ void parse_command(uint8_t command[], uint8_t command_len) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate that we have a first parameter, else requirements for command are
|
// Validate that we have a second parameter, else requirements for command are
|
||||||
// not fulfilled. return unknown command.
|
// not fulfilled. return unknown command.
|
||||||
if (command_len < 2) {
|
if (command_len < 2) {
|
||||||
context.command = UNKNOWN_COMMAND;
|
context.command = UNKNOWN_COMMAND;
|
||||||
@ -69,7 +69,11 @@ void parse_command(uint8_t command[], uint8_t command_len) {
|
|||||||
// Configuration parameters
|
// Configuration parameters
|
||||||
case 0x11: // Read config
|
case 0x11: // Read config
|
||||||
case 0x21: // Write config
|
case 0x21: // Write config
|
||||||
// TODO: Handle parameters for config
|
if (param == 0x01) {
|
||||||
|
context.conf = SAMPLE_TIME;
|
||||||
|
} else {
|
||||||
|
context.conf = CNF_NONE;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Voltage parameters
|
// Voltage parameters
|
||||||
@ -104,9 +108,9 @@ void parse_command(uint8_t command[], uint8_t command_len) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////
|
/////////////////////////////////
|
||||||
// Second parameter selection //
|
// Third parameter selection //
|
||||||
////////////////////////////////
|
/////////////////////////////////
|
||||||
|
|
||||||
// Check if the command does not require a second parameter. If it does not,
|
// Check if the command does not require a second parameter. If it does not,
|
||||||
// return. Only config write requires a second parameter.
|
// return. Only config write requires a second parameter.
|
||||||
@ -114,7 +118,7 @@ void parse_command(uint8_t command[], uint8_t command_len) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate that we have a first parameter, else requirements for command are
|
// Validate that we have a third parameter, else requirements for command are
|
||||||
// not fulfilled. return unknown command.
|
// not fulfilled. return unknown command.
|
||||||
if (command_len < 3) {
|
if (command_len < 3) {
|
||||||
context.command = UNKNOWN_COMMAND;
|
context.command = UNKNOWN_COMMAND;
|
||||||
@ -124,7 +128,31 @@ void parse_command(uint8_t command[], uint8_t command_len) {
|
|||||||
// Store the parameter
|
// Store the parameter
|
||||||
param = command[2];
|
param = command[2];
|
||||||
|
|
||||||
// TODO: Handle the config parameters
|
context.conf = param;
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////
|
||||||
|
// Fourth parameter selection //
|
||||||
|
////////////////////////////////
|
||||||
|
|
||||||
|
// Validate that we have a fourth parameter, else requirements for command are
|
||||||
|
// not fulfilled. return unknown command. Second parameter is u16, thus two bytes.
|
||||||
|
if (command_len < 5) {
|
||||||
|
context.command = UNKNOWN_COMMAND;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the value
|
||||||
|
union {
|
||||||
|
uint16_t value;
|
||||||
|
uint8_t bytes[2];
|
||||||
|
} config_value;
|
||||||
|
|
||||||
|
config_value.bytes[0] = command[3];
|
||||||
|
config_value.bytes[1] = command[4];
|
||||||
|
|
||||||
|
// Store the value
|
||||||
|
context.conf_val = config_value.value;
|
||||||
|
|
||||||
// exit
|
// exit
|
||||||
return;
|
return;
|
||||||
@ -135,7 +163,25 @@ uint8_t route_command(int pos) {
|
|||||||
case WRITE_CONFIG:
|
case WRITE_CONFIG:
|
||||||
return WRITE_CONFIG;
|
return WRITE_CONFIG;
|
||||||
case READ_CONFIG:
|
case READ_CONFIG:
|
||||||
return READ_CONFIG;
|
{
|
||||||
|
// Validate that pos is within the valid range
|
||||||
|
if (pos >= 2) { return 0x00; }
|
||||||
|
|
||||||
|
// Config only has one parameter so we sent that parameter
|
||||||
|
// Create a union to store the data
|
||||||
|
union {
|
||||||
|
uint16_t value;
|
||||||
|
uint8_t bytes[2];
|
||||||
|
} config_value;
|
||||||
|
config_value.value = config.ms_fanspeed_sample_rate;
|
||||||
|
|
||||||
|
uint8_t data = config_value.bytes[1-pos];
|
||||||
|
|
||||||
|
// Return the corresponding data byte
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case READ_VOLTAGE:
|
case READ_VOLTAGE:
|
||||||
{
|
{
|
||||||
// Create a union to store the data
|
// Create a union to store the data
|
||||||
|
|||||||
@ -42,7 +42,8 @@ typedef enum {
|
|||||||
// Enum of all valid config options
|
// Enum of all valid config options
|
||||||
// TODO: Move into config header file
|
// TODO: Move into config header file
|
||||||
typedef enum {
|
typedef enum {
|
||||||
CNF_NONE, // No config option
|
SAMPLE_TIME = 0x01, // Time between each fan speed sample
|
||||||
|
CNF_NONE, // No config option
|
||||||
} config_option_t;
|
} config_option_t;
|
||||||
|
|
||||||
// Enum of all valid fans
|
// Enum of all valid fans
|
||||||
@ -59,13 +60,17 @@ typedef struct {
|
|||||||
src_voltage_t src_voltage; // The selected voltage source
|
src_voltage_t src_voltage; // The selected voltage source
|
||||||
fans_t fan; // The selected fan
|
fans_t fan; // The selected fan
|
||||||
config_option_t conf; // The configuration option to cange
|
config_option_t conf; // The configuration option to cange
|
||||||
// TODO: Add config value field for writing
|
uint16_t conf_val; // The value of the config option to change
|
||||||
} command_context_t;
|
} command_context_t;
|
||||||
|
|
||||||
// Fan history variables
|
// Fan history variables
|
||||||
extern volatile uint16_t fan1_history[512];
|
extern volatile uint16_t fan1_history[512];
|
||||||
extern volatile uint16_t fan2_history[512];
|
extern volatile uint16_t fan2_history[512];
|
||||||
|
|
||||||
|
// Config
|
||||||
|
extern volatile config_t config;
|
||||||
|
extern volatile bool store_config;
|
||||||
|
|
||||||
// Parses the input string and outputs one of the valid commands
|
// Parses the input string and outputs one of the valid commands
|
||||||
void parse_command(uint8_t *command, uint8_t command_len);
|
void parse_command(uint8_t *command, uint8_t command_len);
|
||||||
|
|
||||||
|
|||||||
@ -40,7 +40,8 @@ int main() {
|
|||||||
stdout = &USART_stream;
|
stdout = &USART_stream;
|
||||||
|
|
||||||
// Read the stored config struct
|
// Read the stored config struct
|
||||||
config = read_struct_from_EEPROM();
|
//config = read_struct_from_EEPROM();
|
||||||
|
config.ms_fanspeed_sample_rate = 500;
|
||||||
|
|
||||||
PORTB.DIRSET = PIN3_bm;
|
PORTB.DIRSET = PIN3_bm;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user