This commit is contained in:
2025-09-30 20:59:26 +03:00
parent 1bfdecc5ba
commit 803e53544c
3 changed files with 63 additions and 40 deletions

View File

@@ -1,13 +1,13 @@
[env:ATmega328P] [env:ATmega328P]
platform = atmelavr platform = atmelavr
board = ATmega328P board = ATmega328P
board_build.mcu = atmega328 board_build.mcu = atmega328p
board_build.f_cpu = 8000000L board_build.f_cpu = 8000000L
framework = arduino framework = arduino
#upload_protocol = usbasp upload_protocol = usbasp
#board_fuses.hfuse = 0xDF board_fuses.hfuse = 0xDF
#board_fuses.lfuse = 0xE2 board_fuses.lfuse = 0xE2
#board_fuses.efuse = 0x07 board_fuses.efuse = 0x07
#upload_flags = upload_flags =
# -Pusb -Pusb
# -e -e

View File

@@ -6,6 +6,9 @@
#include "zh_avr_ac_dimmer.h" #include "zh_avr_ac_dimmer.h"
#include "ate0003.h" #include "ate0003.h"
// #define configMINIMAL_STACK_SIZE 70
// #define configCHECK_FOR_STACK_OVERFLOW 1
#define DEBUG #define DEBUG
const char *component_cmm[] = {component_1_cmm, component_2_cmm, component_3_cmm, component_4_cmm, component_5_cmm}; const char *component_cmm[] = {component_1_cmm, component_2_cmm, component_3_cmm, component_4_cmm, component_5_cmm};
@@ -54,6 +57,7 @@ int main(void)
{ {
//** Encoders init **/ //** Encoders init **/
zh_avr_encoder_init_config_t encoder_init_config = ZH_AVR_ENCODER_INIT_CONFIG_DEFAULT(); zh_avr_encoder_init_config_t encoder_init_config = ZH_AVR_ENCODER_INIT_CONFIG_DEFAULT();
encoder_init_config.stack_size = 150;
encoder_init_config.gpio_port = AVR_PORTC; encoder_init_config.gpio_port = AVR_PORTC;
encoder_init_config.a_gpio_number = PORTC0; encoder_init_config.a_gpio_number = PORTC0;
encoder_init_config.b_gpio_number = PORTC1; encoder_init_config.b_gpio_number = PORTC1;
@@ -67,6 +71,7 @@ int main(void)
encoder_init_config.b_gpio_number = PORTC3; encoder_init_config.b_gpio_number = PORTC3;
encoder_init_config.encoder_min_value = 0; encoder_init_config.encoder_min_value = 0;
encoder_init_config.encoder_max_value = (sizeof(component_cmm) / sizeof(component_cmm[0])) - 1; encoder_init_config.encoder_max_value = (sizeof(component_cmm) / sizeof(component_cmm[0])) - 1;
encoder_init_config.encoder_step = 1;
encoder_init_config.encoder_number = COMPONENT_ENCODER; encoder_init_config.encoder_number = COMPONENT_ENCODER;
zh_avr_encoder_init(&encoder_init_config, &component_encoder_handle); zh_avr_encoder_init(&encoder_init_config, &component_encoder_handle);
zh_avr_encoder_set(&component_encoder_handle, 0); zh_avr_encoder_set(&component_encoder_handle, 0);
@@ -84,7 +89,7 @@ int main(void)
UCSR0B = (1 << RXEN0) | (1 << TXEN0); UCSR0B = (1 << RXEN0) | (1 << TXEN0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
stdout = &uart; stdout = &uart;
xTaskCreate(system_setup_task, "system_setup", 150, NULL, tskIDLE_PRIORITY, &system_setup_task_handle); xTaskCreate(system_setup_task, "system_setup", 120, NULL, 4, &system_setup_task_handle);
#else #else
xTaskCreate(system_setup_task, NULL, 150, NULL, tskIDLE_PRIORITY, NULL); xTaskCreate(system_setup_task, NULL, 150, NULL, tskIDLE_PRIORITY, NULL);
#endif #endif
@@ -94,9 +99,10 @@ int main(void)
void system_setup_task(void *pvParameters) void system_setup_task(void *pvParameters)
{ {
char text_buffer[17];
zh_avr_i2c_master_init(false); zh_avr_i2c_master_init(false);
zh_avr_pcf8574_init_config_t pcf8574_init_config = ZH_AVR_PCF8574_INIT_CONFIG_DEFAULT(); zh_avr_pcf8574_init_config_t pcf8574_init_config = ZH_AVR_PCF8574_INIT_CONFIG_DEFAULT();
pcf8574_init_config.stack_size = 255; pcf8574_init_config.stack_size = 150;
//** LED 1 extender init. All LED off. DMM red on. FIX blue on. **/ //** LED 1 extender init. All LED off. DMM red on. FIX blue on. **/
pcf8574_init_config.i2c_address = LED1_I2C_ADDRESS; pcf8574_init_config.i2c_address = LED1_I2C_ADDRESS;
zh_avr_pcf8574_init(&pcf8574_init_config, &led1_handle); zh_avr_pcf8574_init(&pcf8574_init_config, &led1_handle);
@@ -126,16 +132,21 @@ void system_setup_task(void *pvParameters)
zh_avr_pcf8574_init(&pcf8574_init_config, &button_handle); zh_avr_pcf8574_init(&pcf8574_init_config, &button_handle);
//** Loading. Just for fun. **/ //** Loading. Just for fun. **/
zh_avr_160x_set_cursor(&lcd_handle, 0, 0); zh_avr_160x_set_cursor(&lcd_handle, 0, 0);
zh_avr_160x_print_char(&lcd_handle, pgm_read_ptr(text_company)); strcpy_P(text_buffer, text_company);
zh_avr_160x_print_char(&lcd_handle, text_buffer);
zh_avr_160x_set_cursor(&lcd_handle, 1, 0); zh_avr_160x_set_cursor(&lcd_handle, 1, 0);
zh_avr_160x_print_char(&lcd_handle, pgm_read_ptr(text_model)); strcpy_P(text_buffer, text_model);
zh_avr_160x_print_char(&lcd_handle, text_buffer);
zh_avr_160x_set_cursor(&lcd_handle, 2, 0); zh_avr_160x_set_cursor(&lcd_handle, 2, 0);
zh_avr_160x_print_char(&lcd_handle, pgm_read_ptr(text_firmware)); strcpy_P(text_buffer, text_firmware);
zh_avr_160x_print_char(&lcd_handle, text_buffer);
vTaskDelay(1000 / portTICK_PERIOD_MS);
zh_avr_160x_set_cursor(&lcd_handle, 3, 0); zh_avr_160x_set_cursor(&lcd_handle, 3, 0);
zh_avr_160x_print_char(&lcd_handle, pgm_read_ptr(text_loading)); strcpy_P(text_buffer, text_loading);
zh_avr_160x_print_char(&lcd_handle, text_buffer);
for (uint8_t i = 0; i <= 100; ++i) for (uint8_t i = 0; i <= 100; ++i)
{ {
zh_avr_160x_set_cursor(&lcd_handle, 3, 10); zh_avr_160x_set_cursor(&lcd_handle, 3, 9);
zh_avr_160x_print_int(&lcd_handle, i); zh_avr_160x_print_int(&lcd_handle, i);
zh_avr_160x_print_char(&lcd_handle, "%"); zh_avr_160x_print_char(&lcd_handle, "%");
vTaskDelay(50 / portTICK_PERIOD_MS); vTaskDelay(50 / portTICK_PERIOD_MS);
@@ -143,12 +154,14 @@ void system_setup_task(void *pvParameters)
//** LCD default text print. **/ //** LCD default text print. **/
zh_avr_160x_lcd_clear(&lcd_handle); zh_avr_160x_lcd_clear(&lcd_handle);
zh_avr_160x_set_cursor(&lcd_handle, 0, 0); zh_avr_160x_set_cursor(&lcd_handle, 0, 0);
zh_avr_160x_print_char(&lcd_handle, pgm_read_ptr(text_ac_power)); strcpy_P(text_buffer, text_ac_power);
zh_avr_160x_set_cursor(&lcd_handle, 0, 10); zh_avr_160x_print_char(&lcd_handle, text_buffer);
zh_avr_160x_set_cursor(&lcd_handle, 0, 11);
zh_avr_160x_print_int(&lcd_handle, 100); zh_avr_160x_print_int(&lcd_handle, 100);
zh_avr_160x_print_char(&lcd_handle, "%"); zh_avr_160x_print_char(&lcd_handle, "%");
zh_avr_160x_set_cursor(&lcd_handle, 1, 0); zh_avr_160x_set_cursor(&lcd_handle, 1, 0);
zh_avr_160x_print_char(&lcd_handle, pgm_read_ptr(text_component)); strcpy_P(text_buffer, text_component);
zh_avr_160x_print_char(&lcd_handle, text_buffer);
//**"ANY" component setup after loading **/ //**"ANY" component setup after loading **/
used_channels = component_channel[0]; used_channels = component_channel[0];
component_setup_function(0); component_setup_function(0);
@@ -165,6 +178,7 @@ void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
void zh_avr_pcf8574_event_handler(zh_avr_pcf8574_event_on_isr_t *event) void zh_avr_pcf8574_event_handler(zh_avr_pcf8574_event_on_isr_t *event)
{ {
// portENTER_CRITICAL();
switch (event->gpio_number) switch (event->gpio_number)
{ {
case DMM_BUTTON: case DMM_BUTTON:
@@ -174,14 +188,16 @@ void zh_avr_pcf8574_event_handler(zh_avr_pcf8574_event_on_isr_t *event)
{ {
zh_avr_pcf8574_write_gpio(&led1_handle, DMM_LED_RED, LED_ON); zh_avr_pcf8574_write_gpio(&led1_handle, DMM_LED_RED, LED_ON);
zh_avr_pcf8574_write_gpio(&led1_handle, DMM_LED_GREEN, LED_OFF); zh_avr_pcf8574_write_gpio(&led1_handle, DMM_LED_GREEN, LED_OFF);
zh_avr_pcf8574_write_gpio(&relay_handle, DMM_RELAY, RELAY_ON); // zh_avr_pcf8574_write_gpio(&relay_handle, DMM_RELAY, RELAY_ON);
// zh_avr_pcf8574_write(&relay_handle, 0xFF);
is_dmm = false; is_dmm = false;
} }
else else
{ {
zh_avr_pcf8574_write_gpio(&led1_handle, DMM_LED_RED, LED_OFF); zh_avr_pcf8574_write_gpio(&led1_handle, DMM_LED_RED, LED_OFF);
zh_avr_pcf8574_write_gpio(&led1_handle, DMM_LED_GREEN, LED_ON); zh_avr_pcf8574_write_gpio(&led1_handle, DMM_LED_GREEN, LED_ON);
zh_avr_pcf8574_write_gpio(&relay_handle, DMM_RELAY, RELAY_OFF); // zh_avr_pcf8574_write_gpio(&relay_handle, DMM_RELAY, RELAY_OFF);
// zh_avr_pcf8574_write(&relay_handle, 0x00);
is_dmm = true; is_dmm = true;
} }
} }
@@ -276,16 +292,18 @@ void zh_avr_pcf8574_event_handler(zh_avr_pcf8574_event_on_isr_t *event)
break; break;
} }
#ifdef DEBUG #ifdef DEBUG
printf("Interrupt happened on device address 0x%02X on GPIO number %d at level %d.\n", event->i2c_address, event->gpio_number, event->gpio_level); // printf("Interrupt happened on device address 0x%02X on GPIO number %d at level %d.\n", event->i2c_address, event->gpio_number, event->gpio_level);
#endif #endif
// portEXIT_CRITICAL();
} }
void zh_avr_encoder_event_handler(zh_avr_encoder_event_on_isr_t *event) void zh_avr_encoder_event_handler(zh_avr_encoder_event_on_isr_t *event)
{ {
// portENTER_CRITICAL();
switch (event->encoder_number) switch (event->encoder_number)
{ {
case POWER_ENCODER: case POWER_ENCODER:
zh_avr_160x_set_cursor(&lcd_handle, 0, 10); zh_avr_160x_set_cursor(&lcd_handle, 0, 11);
zh_avr_160x_print_int(&lcd_handle, (uint8_t)event->encoder_position); zh_avr_160x_print_int(&lcd_handle, (uint8_t)event->encoder_position);
zh_avr_160x_print_char(&lcd_handle, "% "); zh_avr_160x_print_char(&lcd_handle, "% ");
zh_avr_ac_dimmer_set(event->encoder_position); zh_avr_ac_dimmer_set(event->encoder_position);
@@ -297,8 +315,9 @@ void zh_avr_encoder_event_handler(zh_avr_encoder_event_on_isr_t *event)
break; break;
} }
#ifdef DEBUG #ifdef DEBUG
printf("Encoder number %d position %0.2f.\n", event->encoder_number, event->encoder_position); // printf("Encoder number %d position %d.\n", event->encoder_number, (uint8_t)event->encoder_position);
#endif #endif
// portEXIT_CRITICAL();
} }
ISR(PCINT1_vect) ISR(PCINT1_vect)
@@ -345,14 +364,18 @@ ISR(PCINT2_vect)
void component_setup_function(uint8_t component) void component_setup_function(uint8_t component)
{ {
char text_buffer[17];
zh_avr_160x_set_cursor(&lcd_handle, 1, 10); zh_avr_160x_set_cursor(&lcd_handle, 1, 10);
zh_avr_160x_print_char(&lcd_handle, pgm_read_ptr(component_cmm[component])); strcpy_P(text_buffer, component_cmm[component]);
zh_avr_160x_print_char(&lcd_handle, text_buffer);
zh_avr_160x_set_cursor(&lcd_handle, 2, 0); zh_avr_160x_set_cursor(&lcd_handle, 2, 0);
zh_avr_160x_print_char(&lcd_handle, pgm_read_ptr(component_line_1[component])); strcpy_P(text_buffer, component_line_1[component]);
zh_avr_160x_print_char(&lcd_handle, text_buffer);
zh_avr_160x_set_cursor(&lcd_handle, 3, 0); zh_avr_160x_set_cursor(&lcd_handle, 3, 0);
zh_avr_160x_print_char(&lcd_handle, pgm_read_ptr(component_line_2[component])); strcpy_P(text_buffer, component_line_2[component]);
zh_avr_pcf8574_write(&led1_handle, 0x0F); zh_avr_160x_print_char(&lcd_handle, text_buffer);
zh_avr_pcf8574_write(&led2_handle, 0x0F); // zh_avr_pcf8574_write(&led1_handle, 0xF0);
// zh_avr_pcf8574_write(&led2_handle, 0x0F);
if ((used_channels & (1 << CHANNEL1)) == CHANNEL1) if ((used_channels & (1 << CHANNEL1)) == CHANNEL1)
{ {
zh_avr_pcf8574_write_gpio(&led1_handle, NUM1_LED_BLUE, LED_ON); zh_avr_pcf8574_write_gpio(&led1_handle, NUM1_LED_BLUE, LED_ON);

View File

@@ -62,7 +62,7 @@ extern "C"
const char text_model[] PROGMEM = "ATE0003.000.000"; const char text_model[] PROGMEM = "ATE0003.000.000";
const char text_firmware[] PROGMEM = "Firmware v1.0.0"; const char text_firmware[] PROGMEM = "Firmware v1.0.0";
const char text_loading[] PROGMEM = "Loading:"; const char text_loading[] PROGMEM = "Loading:";
const char text_ac_power[] PROGMEM = "AC POWER: "; const char text_ac_power[] PROGMEM = "A/C POWER: ";
const char text_component[] PROGMEM = "COMPONENT: "; const char text_component[] PROGMEM = "COMPONENT: ";
const char component_1_cmm[] PROGMEM = " ANY "; const char component_1_cmm[] PROGMEM = " ANY ";
@@ -76,7 +76,7 @@ extern "C"
const char component_2_line_1[] PROGMEM = "1:N/A 2:LOGO "; const char component_2_line_1[] PROGMEM = "1:N/A 2:LOGO ";
const char component_2_line_2[] PROGMEM = "3:N/A 4:N/A "; const char component_2_line_2[] PROGMEM = "3:N/A 4:N/A ";
const char component_3_line_1[] PROGMEM = "1:N/A 2:N/A "; const char component_3_line_1[] PROGMEM = "1:N/A 2:N/A ";
const char component_3_line_2[] PROGMEM = "3:RUNW 4:N/A "; const char component_3_line_2[] PROGMEM = "3:RUNWA 4:N/A ";
const char component_4_line_1[] PROGMEM = "1:LOGO 2:N/A "; const char component_4_line_1[] PROGMEM = "1:LOGO 2:N/A ";
const char component_4_line_2[] PROGMEM = "3:N/A 4:N/A "; const char component_4_line_2[] PROGMEM = "3:N/A 4:N/A ";
const char component_5_line_1[] PROGMEM = "1:400W 2:600W "; const char component_5_line_1[] PROGMEM = "1:400W 2:600W ";