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