add external connection check

This commit is contained in:
2025-05-30 07:23:20 +02:00
parent 548cee71f4
commit ee136cc840

View File

@@ -10,103 +10,141 @@
void led_pwm(void);
void probe_int_on(ButtonKey_t *key);
void probe_int_off(ButtonKey_t *key);
void probe_ext_on(ButtonKey_t *key);
void probe_ext_off(ButtonKey_t *key);
void estop_on(ButtonKey_t *key);
void estop_off(ButtonKey_t *key);
ButtonKey_t probe_int = {
.instance = {1, &PINA, PROBE_INT_PIN},
.state = IDLE,
.last_state = IDLE,
.active_state = GPIO_PIN_SET,
.timer_debounce_on = 10, // 1000 ms
.timer_debounce_off = 10, // 1 ms
.buttonReleased = probe_int_off,
.buttonPressed = probe_int_on,
.instance = {1, &PINA, PROBE_INT_PIN},
.state = IDLE,
.last_state = IDLE,
.active_state = GPIO_PIN_SET,
.timer_debounce_on = 10, // 1000 ms
.timer_debounce_off = 10, // 1 ms
.buttonReleased = probe_int_off,
.buttonPressed = probe_int_on,
};
ButtonKey_t probe_ext = {
.instance = {2, &PINA, PROBE_EXT_PIN},
.state = IDLE,
.last_state = IDLE,
.active_state = GPIO_PIN_RESET,
.timer_debounce_on = 10, // 1000 ms
.timer_debounce_off = 10, // 1 ms
.buttonReleased = probe_ext_off,
.buttonPressed = probe_ext_on,
};
ButtonKey_t estop_in = {
.instance = {2, &PINA, ESTOP_IN_PIN},
.state = IDLE,
.last_state = IDLE,
.active_state = GPIO_PIN_RESET,
.timer_debounce_on = 10, // 1 ms
.timer_debounce_off = 10, // 1 ms
.buttonReleased = estop_off,
.buttonPressed = estop_on,
.instance = {3, &PINA, ESTOP_IN_PIN},
.state = IDLE,
.last_state = IDLE,
.active_state = GPIO_PIN_RESET,
.timer_debounce_on = 10, // 1 ms
.timer_debounce_off = 10, // 1 ms
.buttonReleased = estop_off,
.buttonPressed = estop_on,
};
ButtonKey_t *inputs[2] = {
&probe_int,
&estop_in,
ButtonKey_t *inputs[3] = {
&probe_int,
&probe_ext,
&estop_in,
};
uint8_t ext_divece_connected = 0;
uint8_t ext_probe_connected = 0;
uint8_t estop_triggered = 0;
int main(void)
{
LED12_SW_DDR |= (1 << LED12_SW_PIN);
LED34_SW_DDR |= (1 << LED34_SW_PIN);
LED12_SW_DDR |= (1 << LED12_SW_PIN);
LED34_SW_DDR |= (1 << LED34_SW_PIN);
led_pwm();
timer0_init();
adc_init();
led_pwm();
timer0_init();
adc_init();
sei(); // Enable global interrupts
sei(); // Enable global interrupts
while (1)
{
ext_divece_connected = is_device_connected();
for (uint8_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++)
{
buttonHandler(inputs[i]);
}
}
return 0;
while (1)
{
// if (estop_triggered)
// {
// LED12_SW_PORT |= (1 << LED12_SW_PIN);
// LED34_SW_PORT &= ~(1 << LED34_SW_PIN);
// LED12_SW_PORT &= ~(1 << LED12_SW_PIN);
// LED34_SW_PORT |= (1 << LED34_SW_PIN);
// }
if (ext_probe_connected != is_device_connected())
{
ext_probe_connected = is_device_connected();
if (ext_probe_connected)
{
LED34_SW_PORT |= (1 << LED34_SW_PIN);
}
else
{
LED34_SW_PORT &= ~(1 << LED34_SW_PIN);
}
}
for (uint8_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++)
{
buttonHandler(inputs[i]);
}
}
return 0;
}
void led_pwm(void)
{
// Ustaw pin PB1 (OC1A) jako wyjście
LED_PWM_DDR |= (1 << LED_PWM1_PIN) | (1 << LED_PWM2_PIN);
// Ustaw pin PB1 (OC1A) jako wyjście
LED_PWM_DDR |= (1 << LED_PWM1_PIN) | (1 << LED_PWM2_PIN);
// Ustaw Timer1 w trybie Fast PWM, 8-bit (WGM13:0 = 0b0101)
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10); // Clear OC1A on compare match
TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); // Prescaler 64
// Ustaw Timer1 w trybie Fast PWM, 8-bit (WGM13:0 = 0b0101)
TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM10); // Clear OC1A on compare match
TCCR1B = (1 << WGM12) | (1 << CS11) | (1 << CS10); // Prescaler 64
// Ustaw wartość w rejestrze OCR1A (wypełnienie PWM)
OCR1A = 2; // 50% wypełnienia (dla 8-bit: 128/255)
// Ustaw wartość w rejestrze OCR1A (wypełnienie PWM)
OCR1A = 2; // 50% wypełnienia (dla 8-bit: 128/255)
}
void probe_int_on(ButtonKey_t *key)
{
LED12_SW_PORT |= (1 << LED12_SW_PIN);
if (ext_divece_connected)
{
LED34_SW_PORT &= ~(1 << LED34_SW_PIN);
}
else
{
LED34_SW_PORT |= (1 << LED34_SW_PIN);
}
// LED34_SW_PORT &= ~(1 << LED34_SW_PIN);
LED12_SW_PORT |= (1 << LED12_SW_PIN);
if (!ext_probe_connected)
{
LED34_SW_PORT |= (1 << LED34_SW_PIN);
}
}
void probe_int_off(ButtonKey_t *key)
{
LED12_SW_PORT &= ~(1 << LED12_SW_PIN);
if (ext_divece_connected)
{
LED34_SW_PORT |= (1 << LED34_SW_PIN);
}
else
{
LED34_SW_PORT &= ~(1 << LED34_SW_PIN);
}
// LED34_SW_PORT |= (1 << LED34_SW_PIN);
LED12_SW_PORT &= ~(1 << LED12_SW_PIN);
if (!ext_probe_connected)
{
LED34_SW_PORT &= ~(1 << LED34_SW_PIN);
}
}
void probe_ext_on(ButtonKey_t *key)
{
LED34_SW_PORT &= ~(1 << LED34_SW_PIN);
}
void probe_ext_off(ButtonKey_t *key)
{
LED34_SW_PORT |= (1 << LED34_SW_PIN);
}
void estop_on(ButtonKey_t *key)
{
TCCR1A &= ~(1 << COM1A1); // Wyłącz PWM
LED_PWM_PORT &= ~(1 << LED_PWM1_PIN);
estop_triggered = 1;
// TCCR1A &= ~(1 << COM1A1); // Wyłącz PWM
// LED_PWM_PORT &= ~(1 << LED_PWM1_PIN);
}
void estop_off(ButtonKey_t *key)
{
TCCR1A |= (1 << COM1A1); // Włącz PWM
// LED_PWM_PORT |= (1 << LED_PWM_PIN);
// TCCR1A |= (1 << COM1A1); // Włącz PWM
// LED_PWM_PORT |= (1 << LED_PWM_PIN);
}