fix and update save funcs

This commit is contained in:
2024-09-19 23:42:38 +03:00
parent 953593d32a
commit 6746f19cf0
6 changed files with 248 additions and 128 deletions

View File

@ -39,29 +39,70 @@ void UART3_SaveShot(uint8_t *dataPtr, uint8_t len) {
uint8_t shotIndx = dataPtr[1];
Shot shot;
shot.isExist = 1;
shot.countRepeatShot = dataPtr[2];
shot.speedRollerTop = dataPtr[3]+100;
shot.speedRollerBottom = dataPtr[4]+100;
shot.speedScrew = map(dataPtr[6], 0, 120, 0, 100);
shot.rotationAxial = map(dataPtr[5], -99, 99, 0, 180);
shot.rotationHorizontal = map(dataPtr[6], -99, 99, 90-45, 90+45);
shot.speedRollerTop = dataPtr[3] + 100;
shot.speedRollerBottom = dataPtr[4] + 100;
shot.speedScrew = map(dataPtr[5], 0, 120, 0, 100);
shot.rotationAxial = map(dataPtr[6], -99, 99, 0, 180);
shot.rotationHorizontal = map(dataPtr[7], -99, 99, 90 - 45, 90 + 45);
shot.rotationVertical = 180 - dataPtr[8];
saveShot(shotIndx, &shot);
SendResponse(dataPtr[0], 0, NULL, 0);
}
void UART3_SaveProgram(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 0;
const uint8_t MIN_PARAM_LENGTH = 5;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
Program prog;
uint8_t progIndx = dataPtr[1];
prog.header.shotCount = (len - 3) / sizeof(ProgramShot);
prog.header.countRepeat = dataPtr[2];
prog.header.options = dataPtr[3];
if (dataPtr[4] != 0xFF && dataPtr[5]) {
for (uint8_t i = 0; i < prog.header.shotCount; i++) {
uint8_t pos = 4 + i * sizeof(ProgramShot);
prog.shots[i].id = dataPtr[pos + 0];
prog.shots[i].speedScrew = dataPtr[pos + 1];
}
} else {
delProg(progIndx);
}
saveProg(progIndx, &prog);
SendResponse(dataPtr[0], 0, NULL, 0);
}
void UART3_SaveMacro(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 0;
const uint8_t MIN_PARAM_LENGTH = 5;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
Macro macro;
uint8_t macroIndx = dataPtr[1];
macro.header.programmCount = (len - 1) / sizeof(MacroProgram);
if (/**/dataPtr[2] != 0xFF && //
dataPtr[3] != 0xFF && //
dataPtr[4] != 0xFF && //
dataPtr[5] != 0xFF) {
for (uint8_t i = 0; i < macro.header.programmCount; i++) {
uint8_t pos = 2 + i * sizeof(MacroProgram);
macro.programs[i].id = dataPtr[pos+0];
macro.programs[i].speedScrew = dataPtr[pos+2];
macro.programs[i].countRepeat = dataPtr[pos+3];
macro.programs[i].options = dataPtr[pos+4];
}
} else {
delMacro(macroIndx);
}
SendResponse(dataPtr[0], 0, NULL, 0);
}
@ -82,10 +123,14 @@ void UART3_StartProgram(uint8_t *dataPtr, uint8_t len) {
}
void UART3_StartShot(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 0;
const uint8_t MIN_PARAM_LENGTH = 1;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
uint8_t shotIndx = dataPtr[1];
prepareShot(shotIndx);
startShooting();
SendResponse(dataPtr[0], 0, NULL, 0);
}
@ -98,10 +143,13 @@ void UART3_Stop(uint8_t *dataPtr, uint8_t len) {
}
void UART3_DeleteShot(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 0;
const uint8_t MIN_PARAM_LENGTH = 1;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
uint8_t shotIndex = dataPtr[1];
delShot(shotIndex);
SendResponse(dataPtr[0], 0, NULL, 0);
}
@ -110,6 +158,9 @@ void UART3_DeleteProgram(uint8_t *dataPtr, uint8_t len) {
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
uint8_t progIndex = dataPtr[1];
delProg(progIndex);
SendResponse(dataPtr[0], 0, NULL, 0);
}
@ -118,6 +169,9 @@ void UART3_DeleteMacro(uint8_t *dataPtr, uint8_t len) {
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
uint8_t macroIndex = dataPtr[1];
delMacro(macroIndex);
SendResponse(dataPtr[0], 0, NULL, 0);
}
@ -127,7 +181,10 @@ void UART3_DeleteAllData(uint8_t *dataPtr, uint8_t len) {
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
getInfoBlock();
EEPROM_EARSE();
saveInfoBlock();
SendResponse(dataPtr[0], 0, NULL, 0);
}
@ -145,97 +202,115 @@ void UART3_GetDeviceStatus(uint8_t *dataPtr, uint8_t len) {
//200
void UART3_SetServoOffset(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 3;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
const uint8_t MIN_PARAM_LENGTH = 3;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
ServoMap servo = dataPtr[1];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
int16_t newDef = (dataPtr[2] << 8) | dataPtr[3];
newDef+=90; // from center
ServoMap servo = dataPtr[1];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
int16_t newDef = (dataPtr[2] << 8) | dataPtr[3];
if (newDef < 0) newDef = 0;
if (newDef > 180) newDef = 180;
newDef += 90; // from center
if (newDef < 0)
newDef = 0;
if (newDef > 180)
newDef = 180;
int16_t maxDeviation = (currentServo->max > currentServo->def)
? currentServo->max - currentServo->def
: currentServo->def - currentServo->min;
currentServo->def = newDef;
currentServo->def = newDef;
int16_t newMax = currentServo->def + maxDeviation;
int16_t newMin = currentServo->def - maxDeviation;
if (newMax > 180) newMax = 180;
if (newMin < 0) newMin = 0;
currentServo->max = newMax;
currentServo->min = newMin;
saveInfoBlock();
saveInfoBlock();
SendResponse(dataPtr[0], 0, NULL, 0);
SendResponse(dataPtr[0], 0, NULL, 0);
}
//204
void UART3_GetServoOffset(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 1;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
const uint8_t MIN_PARAM_LENGTH = 1;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
ServoMap servo = dataPtr[1];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
ServoMap servo = dataPtr[1];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
int16_t def = currentServo->def - 90; // offset from center
int16_t def = currentServo->def - 90; // offset from center
uint8_t res[2];
res[0] = HIGHBIT(def);
res[1] = LOWBIT(def);
SendResponse(dataPtr[0], 0, res, sizeof(res));
uint8_t res[2];
res[0] = HIGHBIT(def);
res[1] = LOWBIT(def);
SendResponse(dataPtr[0], 0, res, sizeof(res));
}
//201
void UART3_SetServoMaxAngle(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 3;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
const uint8_t MIN_PARAM_LENGTH = 3;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
ServoMap servo = dataPtr[1];
uint16_t maxAngl = (dataPtr[2] << 8) | dataPtr[3];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
int16_t newMax = currentServo->def + maxAngl;
int16_t newMin = currentServo->def - maxAngl;
if (newMax > 180) newMax = 180;
if (newMin < 0) newMin = 0;
currentServo->max = newMax;
currentServo->min = newMin;
saveInfoBlock();
SendResponse(dataPtr[0], 0, NULL, 0);
ServoMap servo = dataPtr[1];
uint16_t maxAngl = (dataPtr[2] << 8) | dataPtr[3];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
if (maxAngl > 180)
maxAngl = 180;
if (maxAngl < 0)
maxAngl = 0;
currentServo->max = maxAngl;
saveInfoBlock();
SendResponse(dataPtr[0], 0, NULL, 0);
}
void UART3_GetServoMaxAngle(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 1;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
ServoMap servo = dataPtr[1];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
uint8_t maxAngl = currentServo->max;
uint8_t res[2];
res[0] = HIGHBIT(maxAngl);
res[1] = LOWBIT(maxAngl);
SendResponse(dataPtr[0], 0, res, sizeof(res));
}
//202
void UART3_GetServoMaxAngle(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 1;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
void UART3_SetServoMinAngle(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 3;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
ServoMap servo = dataPtr[1];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
ServoMap servo = dataPtr[1];
uint16_t minAngl = (dataPtr[2] << 8) | dataPtr[3];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
uint8_t deviationToMax = currentServo->max > currentServo->def
? currentServo->max - currentServo->def
: currentServo->def - currentServo->max;
if (minAngl > 180)
minAngl = 180;
if (minAngl < 0)
minAngl = 0;
currentServo->min = minAngl;
uint8_t deviationToMin = currentServo->def > currentServo->min
? currentServo->def - currentServo->min
: currentServo->min - currentServo->def;
uint8_t maxAngl = (deviationToMax > deviationToMin) ? deviationToMax : deviationToMin;
uint8_t res[2];
res[0] = HIGHBIT(maxAngl);
res[1] = LOWBIT(maxAngl);
SendResponse(dataPtr[0], 0, res, sizeof(res));
saveInfoBlock();
SendResponse(dataPtr[0], 0, NULL, 0);
}
void UART3_GetServoMinAngle(uint8_t *dataPtr, uint8_t len) {
const uint8_t MIN_PARAM_LENGTH = 1;
if (!checkLen(dataPtr[0], len, MIN_PARAM_LENGTH))
return;
ServoMap servo = dataPtr[1];
ServoSetting *currentServo = &infoBlock.hwInfo.servos[servo];
uint8_t minAngl = currentServo->min;
uint8_t res[2];
res[0] = HIGHBIT(minAngl);
res[1] = LOWBIT(minAngl);
SendResponse(dataPtr[0], 0, res, sizeof(res));
}
//203
void UART3_MoveServoToInitialPosition(uint8_t *dataPtr, uint8_t len) {