diff --git a/main.c b/main.c index 337cfdb..ff05592 100644 --- a/main.c +++ b/main.c @@ -27,16 +27,64 @@ ________________________________________________________________________________ #define CH_DOWN 7 // IR command code for channel down #define POWER 69 // IR command code for power -#define BRIGHTNESS_STEP (uint8_t) 15 // Step size for adjusting brightness +#define BRIGHTNESS_STEP (uint8_t) 50 // Step size for adjusting brightness + +#define true 1 +#define false 0 + +static uint8_t brightness = 20; // Initial brightness level +static uint8_t level = 0; // Initial LED level +static uint8_t level_is_increased; // flag for finding the trend + +void LED_brightness(); +void LED_turn_on(uint8_t led); +void LED_turn_off(uint8_t led); +void RGB_toggle(uint8_t led); +void vol_up(); +void vol_down(); +void ch_up(); +void ch_down(); +void power(); +void LED_control(uint16_t command); -uint8_t brightness = 20; // Initial brightness level -uint8_t level = 0; // Initial LED level +int main() +{ + uint16_t address = 0; // Variable to store IR address + uint16_t command = 0; // Variable to store IR command + + IR_init(); // Initialize IR remote control + // uart_init(); // Initialize UART communication + softwarePWM_Init(); // Initialize software PWM + + // uart_puts("Well I'm working here!\r\n"); // Send message indicating successful initialization + while (1) { + if (IR_codeAvailable()) { // Check if IR code is available + if (!IR_isRepeatCode()) { // Check if IR code is not a repeat + IR_getCode(&address, &command); // Get IR address and command + LED_control(command); // Control LEDs based on received command + // uart_putU16(address); // Send IR address over UART + // uart_puts(", "); + // uart_putU16(command); // Send IR command over UART + // uart_puts(", "); + // uart_putU8(level); // Send current LED level over UART + // uart_puts("\r\n"); + _delay_us(10); + } + } + } +} // Function to set LED brightness according to level void LED_brightness() { - for (uint8_t i = 0; i < level; i++) { - softwarePWM_Set(i, brightness); + if (level > 7) { + RGB_toggle(level_is_increased ? level - 1 : level); + for (uint8_t i = 0;i < 4;i++) + LED_turn_on(i); + return; } + + for (uint8_t i = 0;i < level;i++) + LED_turn_on(i); } // Function to turn on a specific LED @@ -50,8 +98,8 @@ void LED_turn_off(uint8_t led) { } // Function to toggle RGB LEDs based on level -void RGB_toggle() { - uint8_t m = (level - 1) % 3; +void RGB_toggle(uint8_t led) { + uint8_t m = (led - 2) % 3; softwarePWM_Set(4, brightness * (m == 0)); softwarePWM_Set(5, brightness * (m == 1)); softwarePWM_Set(6, brightness * (m == 2)); @@ -59,31 +107,30 @@ void RGB_toggle() { // Function to increase volume level void vol_up() { - if (level > 6) { - RGB_toggle(); // Toggle RGB LEDs - level = (level <= 9) ? level + 1 : 7; // Increment level, limit at 7 - return; + if (level > 7) { + RGB_toggle(level++); + goto EXIT; } LED_turn_on(level++); // Turn on LED at current level + EXIT: + level_is_increased = true; } // Function to decrease volume level void vol_down() { - if (level > 7) { - --level; - RGB_toggle(); // Toggle RGB LEDs - return; + if (level > 8) { + RGB_toggle(--level); + goto EXIT; } - if (level == 7) { - // Turn on RGB LEDs at level 7 + if (level == 8) { LED_turn_on(4); LED_turn_on(5); LED_turn_on(6); - --level; - return; } - LED_turn_off(level--); // Turn off LED at current level + if (level != 0) LED_turn_off(--level); // Turn off LED at current level if (level == UINT8_MAX) level = 0; // Wrap level around if overflow + EXIT: + level_is_increased = false; } // Function to increase brightness @@ -102,7 +149,7 @@ void ch_down() { // Function to turn off all LEDs void power() { - for (int8_t i = 6; i >= 0; i--) LED_turn_off(i); // Turn off all LEDs + for (uint8_t i = 0; i < 7; i++) LED_turn_off(i); // Turn off all LEDs level = 0; // Reset LED levels } @@ -117,29 +164,3 @@ void LED_control(uint16_t command) { default: break; } } - -int main() -{ - uint16_t address = 0; // Variable to store IR address - uint16_t command = 0; // Variable to store IR command - - IR_init(); // Initialize IR remote control - // uart_init(); // Initialize UART communication - softwarePWM_Init(); // Initialize software PWM - - // uart_puts("Well I'm working here!\r\n"); // Send message indicating successful initialization - while (1) { - if (IR_codeAvailable()) { // Check if IR code is available - if (!IR_isRepeatCode()) { // Check if IR code is not a repeat - IR_getCode(&address, &command); // Get IR address and command - LED_control(command); // Control LEDs based on received command - // uart_putU16(address); // Send IR address over UART - // uart_puts(", "); - // uart_putU16(command); // Send IR command over UART - // uart_puts(", "); - // uart_putU8(level); // Send current LED level over UART - // uart_puts("\r\n"); - } - } - } -} diff --git a/src/libs/softwarePWM.h b/src/libs/softwarePWM.h index bd74749..907efc6 100644 --- a/src/libs/softwarePWM.h +++ b/src/libs/softwarePWM.h @@ -83,39 +83,39 @@ ________________________________________________________________________________ #define USE_LOGARITHMIC_ARRAY FALSE // Mapping channels to pin and ports -#define SOFTPWM_CHANNEL0_DDR DDRD -#define SOFTPWM_CHANNEL0_PORT PORTD -#define SOFTPWM_CHANNEL0_PIN 4 -#define SOFTPWM_CHANNEL0_PIN_STATES pinlevelD - -#define SOFTPWM_CHANNEL1_DDR DDRD -#define SOFTPWM_CHANNEL1_PORT PORTD -#define SOFTPWM_CHANNEL1_PIN 5 -#define SOFTPWM_CHANNEL1_PIN_STATES pinlevelD - -#define SOFTPWM_CHANNEL2_DDR DDRD -#define SOFTPWM_CHANNEL2_PORT PORTD -#define SOFTPWM_CHANNEL2_PIN 6 -#define SOFTPWM_CHANNEL2_PIN_STATES pinlevelD - -#define SOFTPWM_CHANNEL3_DDR DDRD -#define SOFTPWM_CHANNEL3_PORT PORTD -#define SOFTPWM_CHANNEL3_PIN 7 -#define SOFTPWM_CHANNEL3_PIN_STATES pinlevelD +#define SOFTPWM_CHANNEL0_DDR DDRC +#define SOFTPWM_CHANNEL0_PORT PORTC +#define SOFTPWM_CHANNEL0_PIN 1 +#define SOFTPWM_CHANNEL0_PIN_STATES pinlevelC + +#define SOFTPWM_CHANNEL1_DDR DDRC +#define SOFTPWM_CHANNEL1_PORT PORTC +#define SOFTPWM_CHANNEL1_PIN 0 +#define SOFTPWM_CHANNEL1_PIN_STATES pinlevelC + +#define SOFTPWM_CHANNEL2_DDR DDRB +#define SOFTPWM_CHANNEL2_PORT PORTB +#define SOFTPWM_CHANNEL2_PIN 5 +#define SOFTPWM_CHANNEL2_PIN_STATES pinlevelB + +#define SOFTPWM_CHANNEL3_DDR DDRB +#define SOFTPWM_CHANNEL3_PORT PORTB +#define SOFTPWM_CHANNEL3_PIN 4 +#define SOFTPWM_CHANNEL3_PIN_STATES pinlevelB #define SOFTPWM_CHANNEL4_DDR DDRB #define SOFTPWM_CHANNEL4_PORT PORTB -#define SOFTPWM_CHANNEL4_PIN 2 +#define SOFTPWM_CHANNEL4_PIN 3 #define SOFTPWM_CHANNEL4_PIN_STATES pinlevelB #define SOFTPWM_CHANNEL5_DDR DDRB #define SOFTPWM_CHANNEL5_PORT PORTB -#define SOFTPWM_CHANNEL5_PIN 3 +#define SOFTPWM_CHANNEL5_PIN 2 #define SOFTPWM_CHANNEL5_PIN_STATES pinlevelB #define SOFTPWM_CHANNEL6_DDR DDRB #define SOFTPWM_CHANNEL6_PORT PORTB -#define SOFTPWM_CHANNEL6_PIN 4 +#define SOFTPWM_CHANNEL6_PIN 1 #define SOFTPWM_CHANNEL6_PIN_STATES pinlevelB #define SOFTPWM_CHANNEL7_DDR 0 @@ -135,14 +135,14 @@ ________________________________________________________________________________ // Which ports are used #define SOFTPWM_ON_PORT_B TRUE -#define SOFTPWM_ON_PORT_C FALSE -#define SOFTPWM_ON_PORT_D TRUE +#define SOFTPWM_ON_PORT_C TRUE +#define SOFTPWM_ON_PORT_D FALSE #define SOFTPWM_ON_PORT_E FALSE // What pins are on this ports -#define SOFTPWM_PINS_PORTB _BV(2) | _BV(3) | _BV(4) -#define SOFTPWM_PINS_PORTC FALSE -#define SOFTPWM_PINS_PORTD _BV(4) | _BV(5) | _BV(6) | _BV(7) +#define SOFTPWM_PINS_PORTB _BV(1) | _BV(2) | _BV(3) | _BV(4) | _BV(5) +#define SOFTPWM_PINS_PORTC _BV(0) | _BV(1) +#define SOFTPWM_PINS_PORTD FALSE #define SOFTPWM_PINS_PORTE FALSE