PingPong/Core/Src/IR_CMD_HandlerLogic.c
2024-10-03 02:37:05 +03:00

314 lines
7.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* IR_CMD_HandlerLogic.c
*
* Created on: Aug 26, 2024
* Author: DashyFox
*/
#include "IR_CMD_Handler.h"
#include "IR.h"
#include "ShiftReg.h"
#include "RobotFunctions.h"
#include "Indicator.h"
#include "SoundMelody.h"
#include "Print.h"
enum IR_MENU {
IR_MENU_Home,
IR_MENU_SHOT, IR_MENU_PROGR, IR_MENU_MACRO, IR_MENU_,
};
// <<
extern void (*InputHandler)(void); // ProcessFunc
extern void (*onHoldRepeat)(void);
extern uint16_t inputParam; // current input parameter
// >>
extern IRData data;
// ()
extern void NullFunc(); // null func for paramEnter(NullFunc);
extern void paramEnter(void (*onEnter_)()); // setParamFunc for enter
void IR_ShootingStart();
void onSelectShot() {
if (prepareShot(inputParam)) {
InputHandler = IR_ShootingStart;
} else {
paramEnter(onSelectShot);
print("Shot not found\n");
// Shot not found
}
}
void onSelectProgram() {
if (prepareProgramm(inputParam)) {
InputHandler = IR_ShootingStart;
} else {
paramEnter(onSelectShot);
print("Shot not found\n");
// Shot not found
}
}
void onSelectMacro() {
if (prepareMacro(inputParam)) {
InputHandler = IR_ShootingStart;
} else {
paramEnter(onSelectShot);
print("Shot not found\n");
// Shot not found
}
}
unsigned char b1 = 1;
unsigned char b2 = 1;
unsigned char b3 = 1;
uint8_t screwSpeed;
uint8_t speedUP = 100;
uint8_t speedDown = 100;
// onHoldRepeat = IR_Home_Process;
void IR_Home_Process() {
InputHandler = IR_Home_Process;
uint8_t isshotRcOverride = currentInfo.mode == ShotMode
&& currentInfo.state == RUN;
switch (data.command) {
case IR_SHOT:
paramEnter(onSelectShot);
break;
case IR_PROG: {
paramEnter(onSelectProgram);
break;
}
case IR_MACRO: {
paramEnter(onSelectMacro);
break;
}
case IR_F_BTN: {
break;
}
case IR_PAUSE:
if (currentInfo.state == RUN) {
pauseShooting();
}
break;
case IR_START:
if (currentInfo.state == PAUSE) {
currentInfo.state = RUN;
switch (currentInfo.mode) {
case DebugShot:
case ShotMode:
shotApply(&currentInfo.shot);
break;
case ProgramMode:
shotApply(&currentInfo.program.currentShot);
break;
case MacroMode:
shotApply(&currentInfo.macro.currentProgram.currentShot);
break;
default:
break;
}
}
break;
case IR_ESC: {
IR_CMD_Clear();
break;
}
case IR_DEBUG: {
if (!(currentInfo.state == STOP || currentInfo.state == PAUSE))
break;
switch (currentInfo.mode) {
case ShotMode:
currentInfo.mode = DebugShot;
currentInfo.state = RUN;
melody(melody_start);
shotApply(&currentInfo.shot);
break;
default:
break;
}
break;
}
case IR_STOP: {
print("***********************");
stopShooting();
break;
}
case IR_TEMPO_INC:
if (isshotRcOverride && currentInfo.shot.shot.speedScrew < 100) {
currentInfo.shot.shot.speedScrew++;
shotApply(&currentInfo.shot);
// led_progressbar(10, 19, currentInfo.shot.shot.speedScrew); //todo: led_progressbarTMP
onHoldRepeat = IR_Home_Process;
}
break;
case IR_TEMPO_DEC:
if (isshotRcOverride && currentInfo.shot.shot.speedScrew > 0) {
currentInfo.shot.shot.speedScrew--;
shotApply(&currentInfo.shot);
onHoldRepeat = IR_Home_Process;
}
break;
case IR_ENGINE_UP_INC:
if (isshotRcOverride && currentInfo.shot.shot.speedRollerTop < 200) {
currentInfo.shot.shot.speedRollerTop++;
shotApply(&currentInfo.shot);
onHoldRepeat = IR_Home_Process;
}
break;
case IR_ENGINE_UP_DEC:
if (isshotRcOverride && currentInfo.shot.shot.speedRollerTop > 0) {
currentInfo.shot.shot.speedRollerTop--;
shotApply(&currentInfo.shot);
onHoldRepeat = IR_Home_Process;
}
break;
case IR_ENGINE_DOWM_INC:
if (isshotRcOverride && currentInfo.shot.shot.speedRollerBottom < 200) {
currentInfo.shot.shot.speedRollerBottom++;
shotApply(&currentInfo.shot);
onHoldRepeat = IR_Home_Process;
}
break;
case IR_ENGINE_DOWM_DEC:
if (isshotRcOverride && currentInfo.shot.shot.speedRollerBottom > 0) {
currentInfo.shot.shot.speedRollerBottom--;
shotApply(&currentInfo.shot);
onHoldRepeat = IR_Home_Process;
}
break;
/////////////////////////////////////////////////////////
case IR_FONT_RIGHT:
for (int i = 0; i <= 100; ++i) {
led_progressbar(0, 19, i);
HAL_Delay(10);
}
led_clear();
break;
case IR_FRONT_MID: {
uint8_t i2c_address;
HAL_StatusTypeDef result;
print("Scan\n");
// Перебираем все возможные адреса на шине I2C (7 бит, от 0x08 до 0x77)
for (i2c_address = 0x08; i2c_address <= 0x77; i2c_address++) {
// Отправляем запрос на указанный адрес (HAL_I2C_Master_Transmit без данных)
result = HAL_I2C_IsDeviceReady(&hi2c1, (i2c_address << 1), 1, 100);
if (result == HAL_OK) {
// Если устройство отвечает, выводим его адрес
print("Found I2C at: ");
printNumber(i2c_address);
print("\n");
} else {
// Если устройство не отвечает, продолжаем сканирование
print(".");
}
HAL_Delay(10); // Задержка для стабильности сканирования
}
print("\nScanning completed.\n");
}
break;
case IR_FRONT_LEFT: {
uint8_t buf[1024]; // Буфер для чтения данных размером 128 байт
uint16_t blockAddr16 = START_ADR_MACRO; // Начальный адрес EEPROM
uint8_t blockAddr[2] = { (uint8_t) (blockAddr16 >> 8),
(uint8_t) (blockAddr16 & 0xFF) }; // Адрес в формате 2 байта
int max_attempts = 15; // Максимальное количество попыток для операции
int attempts = 0; // Счетчик попыток
HAL_StatusTypeDef status;
do {
// Отправляем адрес в EEPROM
status = HAL_I2C_Master_Transmit(&hi2c1, (AT24C_ADRESS << 1),
blockAddr, 2, 1000);
if (status != HAL_OK) {
HAL_Delay(1); // Задержка перед повтором
attempts++;
continue; // Переход к следующей попытке
}
HAL_Delay(1); // Небольшая задержка
// Читаем 128 байт данных из EEPROM
status = HAL_I2C_Master_Receive(&hi2c1, (AT24C_ADRESS << 1) | 1,
buf, sizeof(buf), 1000);
if (status == HAL_OK) {
// Данные успешно считаны, выводим их
char buffer[16];
for (int i = 0; i < sizeof(buf); ++i) {
if (i % 8 == 0)
print(" ");
if (i % 32 == 0)
print("\n");
snprintf(buffer, sizeof(buffer), "%02X ", buf[i]); // Преобразуем байт в шестнадцатеричную строку
CDC_Transmit_FS((uint8_t*) buffer, strlen(buffer));
}
break; // Выход из попыток, если чтение успешно
} else {
print("Read Error EEPROM\n");
HAL_Delay(1); // Задержка перед повтором
attempts++;
}
} while (attempts < max_attempts);
if (status != HAL_OK) {
print("Failed to read EEPROM after multiple attempts\n");
}
print("\n\n\n");
}
break;
default:
break;
}
}
void IR_ShootingStart() {
InputHandler = IR_ShootingStart;
switch (data.command) {
case IR_START: {
InputHandler = IR_Home_Process;
startShooting(infoBlock.hwInfo.timings.preRun);
break;
}
default: {
InputHandler = IR_Home_Process;
IR_Home_Process();
break;
}
}
}