}
else if(cmd==CMD_LOK)
{
- unsigned char err;
- read_all(&err, 1);
+ Error err;
+ read_status(&err);
if(err==ERR_NO_ERROR)
{
Locomotive &loco = locos[addr];
signal_loco_speed.emit(addr, loco.speed, loco.reverse);
}
+ else
+ error(cmd, err);
}
else if(cmd==CMD_TURNOUT)
{
- unsigned char err;
- read_all(&err, 1);
+ Error err;
+ read_status(&err);
unsigned addr = queue.front().addr;
Turnout &turnout = turnouts[addr];
else if(err==ERR_NO_I2C_SPACE)
queue.push_back(queue.front());
else
+ {
turnout.pending = turnout.state;
+ error(cmd, err);
+ }
}
else if(cmd==CMD_TURNOUT_STATUS)
{
- unsigned char err;
- read_all(&err, 1);
+ Error err;
+ read_status(&err);
if(err==ERR_NO_ERROR)
{
signal_turnout.emit(addr, turnout.state);
}
}
+ else
+ error(cmd, err);
}
else if(cmd==CMD_LOK_STATUS)
{
- unsigned char err;
- read_all(&err, 1);
+ Error err;
+ read_status(&err);
if(err==ERR_NO_ERROR)
{
signal_loco_function.emit(addr, i, loco.funcs&(1<<i));
}
}
+ else
+ error(cmd, err);
}
else
{
if(cmd==CMD_LOK_CONFIG)
expected_bytes = 4;
- unsigned char err;
- read_all(&err, 1);
+ Error err;
+ read_status(&err);
if(err==ERR_NO_ERROR)
{
unsigned char data[8];
read_all(data, expected_bytes);
}
+ else
+ error(cmd, err);
}
}
return pos;
}
+unsigned Intellibox::read_status(Error *err)
+{
+ unsigned char c;
+ unsigned ret = read_all(&c, 1);
+ *err = static_cast<Error>(c);
+ return ret;
+}
+
+void Intellibox::error(Command cmd, Error err)
+{
+ const char *cmd_str = 0;
+ switch(cmd)
+ {
+ case CMD_LOK: cmd_str = "CMD_LOK"; break;
+ case CMD_LOK_STATUS: cmd_str = "CMD_LOK_STATUS"; break;
+ case CMD_LOK_CONFIG: cmd_str = "CMD_LOK_CONFIG"; break;
+ case CMD_FUNC: cmd_str = "CMD_FUNC"; break;
+ case CMD_FUNC_STATUS: cmd_str = "CMD_FUNC_STATUS"; break;
+ case CMD_TURNOUT: cmd_str = "CMD_TURNOUT"; break;
+ case CMD_TURNOUT_FREE: cmd_str = "CMD_TURNOUT_FREE"; break;
+ case CMD_TURNOUT_STATUS: cmd_str = "CMD_TURNOUT_STATUS"; break;
+ case CMD_TURNOUT_GROUP_STATUS: cmd_str = "CMD_TURNOUT_GROUP_STATUS"; break;
+ case CMD_SENSOR_STATUS: cmd_str = "CMD_SENSOR_STATUS"; break;
+ case CMD_SENSOR_REPORT: cmd_str = "CMD_SENSOR_REPORT"; break;
+ case CMD_SENSOR_PARAM_SET: cmd_str = "CMD_SENSOR_PARAM_SET"; break;
+ case CMD_STATUS: cmd_str = "CMD_STATUS"; break;
+ case CMD_POWER_OFF: cmd_str = "CMD_POWER_OFF"; break;
+ case CMD_POWER_ON: cmd_str = "CMD_POWER_ON"; break;
+ case CMD_NOP: cmd_str = "CMD_NOP"; break;
+ case CMD_EVENT: cmd_str = "CMD_EVENT"; break;
+ case CMD_EVENT_LOK: cmd_str = "CMD_EVENT_LOK"; break;
+ case CMD_EVENT_TURNOUT: cmd_str = "CMD_EVENT_TURNOUT"; break;
+ case CMD_EVENT_SENSOR: cmd_str = "CMD_EVENT_SENSOR"; break;
+ default: cmd_str = "(unknown command)";
+ }
+
+ const char *err_str = 0;
+ switch(err)
+ {
+ case ERR_NO_ERROR: err_str = "ERR_NO_ERROR"; break;
+ case ERR_SYS_ERROR: err_str = "ERR_SYS_ERROR"; break;
+ case ERR_BAD_PARAM: err_str = "ERR_BAD_PARAM"; break;
+ case ERR_POWER_OFF: err_str = "ERR_POWER_OFF"; break;
+ case ERR_NO_LOK_SPACE: err_str = "ERR_NO_LOK_SPACE"; break;
+ case ERR_NO_TURNOUT_SPACE: err_str = "ERR_NO_TURNOUT_SPACE"; break;
+ case ERR_NO_DATA: err_str = "ERR_NO_DATA"; break;
+ case ERR_NO_SLOT: err_str = "ERR_NO_SLOT"; break;
+ case ERR_BAD_LOK_ADDR: err_str = "ERR_BAD_LOK_ADDR"; break;
+ case ERR_LOK_BUSY: err_str = "ERR_LOK_BUSY"; break;
+ case ERR_BAD_TURNOUT_ADDR: err_str = "ERR_BAD_TURNOUT_ADDR"; break;
+ case ERR_BAD_SO_VALUE: err_str = "ERR_BAD_SO_VALUE"; break;
+ case ERR_NO_I2C_SPACE: err_str = "ERR_NO_I2C_SPACE"; break;
+ case ERR_LOW_TURNOUT_SPACE: err_str = "ERR_LOW_TURNOUT_SPACE"; break;
+ case ERR_LOK_HALTED: err_str = "ERR_LOK_HALTED"; break;
+ case ERR_LOK_POWER_OFF: err_str = "ERR_LOK_POWER_OFF"; break;
+ default: cmd_str = "(unknown error)";
+ }
+
+ IO::print("Error: %s: %s\n", cmd_str, err_str);
+}
+
Intellibox::Locomotive::Locomotive():
speed(0),