/** main function */ int main(void) { init_output(); init_pwm(); #if SERIAL_UART init_uart(); #endif #if RC5_DECODER init_rc5(); #endif #if I2C init_i2c(); #endif global_pwm.channels[0].brightness = 50; global_pwm.channels[0].target_brightness = 50; #if STATIC_SCRIPTS init_script_threads(); #if RS485_CTRL == 0 /* start the example scripts */ script_threads[0].handler.execute = &memory_handler_flash; script_threads[0].handler.position = (uint16_t) &colorchange_red; script_threads[0].flags.disabled = 0; //script_threads[1].handler.execute = &memory_handler_flash; //script_threads[1].handler.position = (uint16_t) &testscript_flash2; //script_threads[1].flags.disabled = 0; // //script_threads[2].handler.execute = &memory_handler_eeprom; //script_threads[2].handler.position = (uint16_t) &testscript_eeprom; //script_threads[2].flags.disabled = 0; //script_threads[0].handler.execute = &memory_handler_flash; //script_threads[0].handler.position = (uint16_t) &blinken; //script_threads[0].flags.disabled = 0; #endif #endif #if I2C_MASTER i2c_global.send_messages[0].command.size = 4; i2c_global.send_messages[0].command.code = COMMAND_SET_COLOR; i2c_global.send_messages[0].command.set_color_parameters.colors[0] = 0x10; i2c_global.send_messages[0].command.set_color_parameters.colors[1] = 0x10; i2c_global.send_messages[0].command.set_color_parameters.colors[2] = 0x10; i2c_global.send_messages_count = 1; #endif #if RS485_CTRL /* init command bus */ UCSR0A = _BV(MPCM0); /* enable multi-processor communication mode */ UCSR0C = _BV(UCSZ00) | _BV(UCSZ01); /* 9 bit frame size */ #define UART_UBRR 8 /* 115200 baud at 16mhz */ UBRR0H = HIGH(UART_UBRR); UBRR0L = LOW(UART_UBRR); UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(UCSZ02); /* enable receiver and transmitter */ #endif /* enable interrupts globally */ sei(); while (1) { /* after the last pwm timeslot, rebuild the timeslot table */ if (global.flags.last_pulse) { global.flags.last_pulse = 0; update_pwm_timeslots(); } /* at the beginning of each pwm cycle, call the fading engine and * execute all script threads */ if (global.flags.new_cycle) { global.flags.new_cycle = 0; update_brightness(); #if STATIC_SCRIPTS execute_script_threads(); #endif continue; } #if SERIAL_UART /* check if we received something via uart */ if (fifo_fill(&global_uart.rx_fifo) > 0) { check_serial_input(fifo_load(&global_uart.rx_fifo)); continue; } #endif #if RC5_DECODER /* check if we received something via ir */ if (global_rc5.new_data) { static uint8_t toggle_bit = 2; /* if key has been pressed again */ if (global_rc5.received_command.toggle_bit != toggle_bit) { /* if code is 0x01 (key '1' on a default remote) */ if (global_rc5.received_command.code == 0x01) { /* install script into thread 1 */ script_threads[1].handler.execute = &memory_handler_flash; script_threads[1].handler.position = (uint16_t) &green_flash; script_threads[1].flags.disabled = 0; script_threads[1].handler_stack_offset = 0; } /* store new toggle bit state */ toggle_bit = global_rc5.received_command.toggle_bit; } /* reset the new_data flag, so that new commands can be received */ global_rc5.new_data = 0; continue; } #endif #if RS485_CTRL if (UCSR0A & _BV(RXC0)) { uint8_t address = UCSR0B & _BV(RXB80); /* read nineth bit, zero if data, one if address */ uint8_t data = UDR0; static uint8_t buffer[8]; static uint8_t fill = 0; if (UCSR0A & _BV(MPCM0) || address) { /* if MPCM mode is still active, or ninth bit set, this is an address packet */ /* check if we are ment */ if (data == 0 || data == RS485_ADDRESS) { /* remove MPCM flag and reset buffer fill counter */ UCSR0A &= ~_BV(MPCM0); fill = 0; continue; } else {/* turn on MPCM */ UCSR0A |= _BV(MPCM0); continue; } } /* else this is a data packet, put data into buffer */ buffer[fill++] = data; if (buffer[0] == 0x01) { /* soft reset */ jump_to_bootloader(); } else if (buffer[0] == 0x02 && fill == 4) { /* set color */ for (uint8_t pos = 0; pos < 3; pos++) { global_pwm.channels[pos].target_brightness = buffer[pos + 1]; global_pwm.channels[pos].brightness = buffer[pos + 1]; } UCSR0A |= _BV(MPCM0); /* return to MPCM mode */ } else if (buffer[0] == 0x03 && fill == 6) { /* fade to color */ for (uint8_t pos = 0; pos < 3; pos++) { global_pwm.channels[pos].speed_h = buffer[1]; global_pwm.channels[pos].speed_l = buffer[2]; global_pwm.channels[pos].target_brightness = buffer[pos + 3]; } UCSR0A |= _BV(MPCM0); /* return to MPCM mode */ } } #endif #if I2C_MASTER i2c_master_check_queue(); #endif } }
/** main function */ int main(void) { /* {{{ */ reboot = 0; wdt_enable(WDTO_1S); #if RANDOM unsigned long int cnt = 1000; unsigned char white, red, green, blue; unsigned char save = 0; srand(eeprom_read_word(&seed)); #endif init_output(); #if COLORFUL_INIT uint16_t j = 0; LED01_PORT |= _BV(LED_CHANNEL0); //Switch On for (j = 0; j <= 10; j ++) { wdt_reset(); _delay_ms(10); } LED01_PORT &= ~_BV(LED_CHANNEL0); //Switch Off for (j = 0; j <= 17; j ++) { wdt_reset(); _delay_ms(10); } LED01_PORT |= _BV(LED_CHANNEL1); //Switch On for (j = 0; j <= 10; j ++) { wdt_reset(); _delay_ms(10); } LED01_PORT &= ~_BV(LED_CHANNEL1); //Switch Off for (j = 0; j <= 17; j ++) { wdt_reset(); _delay_ms(10); } LED23_PORT |= _BV(LED_CHANNEL2); //Switch On for (j = 0; j <= 10; j ++) { wdt_reset(); _delay_ms(10); } LED23_PORT &= ~_BV(LED_CHANNEL2); //Switch Off for (j = 0; j <= 17; j ++) { wdt_reset(); _delay_ms(10); } LED23_PORT |= _BV(LED_CHANNEL3); //Switch On for (j = 0; j <= 10; j ++) { wdt_reset(); _delay_ms(10); } LED23_PORT &= ~_BV(LED_CHANNEL3); //Switch Off #endif init_pwm(); #if RC5_DECODER init_rc5(); #endif #if STATIC_SCRIPTS init_script_threads(); #if RS485_CTRL == 0 /* start the example scripts */ //script_threads[0].handler.execute = &memory_handler_flash; //script_threads[0].handler.position = (uint16_t) &blinken; //script_threads[0].flags.disabled = 0; script_threads[0].handler.execute = &memory_handler_flash; script_threads[0].handler.position = (uint16_t) &strobo_rgb; script_threads[0].flags.disabled = 0; #endif #endif #if RS485_CTRL /* init command bus */ UCSR0A = _BV(MPCM0); /* enable multi-processor communication mode */ UCSR0C = _BV(UCSZ00) | _BV(UCSZ01); /* 9 bit frame size */ #define UART_UBRR 8 /* 115200 baud at 16mhz */ UBRR0H = HIGH(UART_UBRR); UBRR0L = LOW(UART_UBRR); UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(UCSZ02); /* enable receiver and transmitter */ #endif #if USB usbInit(); usbDeviceDisconnect(); /* enforce re-enumeration, do this while interrupts are disabled! */ uchar i = 0; while(--i){ /* fake USB disconnect for > 250 ms */ _delay_ms(1); } usbDeviceConnect(); //TCCR0 = 5; /* set prescaler to 1/1024 */ #endif /* enable interrupts globally */ sei(); while (1) { wdt_reset(); #if USB usbPoll(); #endif // if (TIFR & (1 << TOV0)) { // TIFR |= 1 << TOV0; /* clear pending flag */ // } if (reboot) { soft_reset(); } if (global.flags.new_cycle) { global.flags.new_cycle = 0; update_brightness(); #if RANDOM if (++cnt == 1200) { red = pwmtable[rand() % 16]; green = pwmtable[rand() % 16]; blue = pwmtable[rand() % 16]; white = whitetable[rand() % 8]; set_fade(1, blue, 800); set_fade(2, green, 800); set_fade(3, red, 800); if (blue + green + red <= 32) set_fade(0, white, 800); else set_fade(0, 0, 800); cnt = 0; if (++save == 100) eeprom_write_word(&seed, rand()); } #endif #if STATIC_SCRIPTS execute_script_threads(); #endif continue; } } #if RC5_DECODER /* check if we received something via ir */ if (global_rc5.new_data) { static uint8_t toggle_bit = 2; /* if key has been pressed again */ if (global_rc5.received_command.toggle_bit != toggle_bit) { /* if code is 0x01 (key '1' on a default remote) */ if (global_rc5.received_command.code == 0x01) { /* install script into thread 1 */ script_threads[1].handler.execute = &memory_handler_flash; script_threads[1].handler.position = (uint16_t) &green_flash; script_threads[1].flags.disabled = 0; script_threads[1].handler_stack_offset = 0; } /* store new toggle bit state */ toggle_bit = global_rc5.received_command.toggle_bit; } /* reset the new_data flag, so that new commands can be received */ global_rc5.new_data = 0; continue; } #endif #if RS485_CTRL if (UCSR0A & _BV(RXC0)) { uint8_t address = UCSR0B & _BV(RXB80); /* read nineth bit, zero if data, one if address */ uint8_t data = UDR0; static uint8_t buffer[8]; static uint8_t fill = 0; if (UCSR0A & _BV(MPCM0) || address) { /* if MPCM mode is still active, or ninth bit set, this is an address packet */ /* check if we are ment */ if (data == 0 || data == RS485_ADDRESS) { /* remove MPCM flag and reset buffer fill counter */ UCSR0A &= ~_BV(MPCM0); fill = 0; continue; } else {/* turn on MPCM */ UCSR0A |= _BV(MPCM0); continue; } } /* else this is a data packet, put data into buffer */ buffer[fill++] = data; if (buffer[0] == 0x01) { /* soft reset */ jump_to_bootloader(); } else if (buffer[0] == 0x02 && fill == 4) { /* set color */ CHANNEL0_PWM = buffer[1]; CHANNEL1_PWM = buffer[2]; CHANNEL2_PWM = buffer[3]; CHANNEL3_PWM = buffer[4]; for (uint8_t pos = 0; pos < PWM_CHANNELS; pos++) { global_pwm.channels[pos].target_brightness = buffer[pos + 1]; global_pwm.channels[pos].brightness = buffer[pos + 1]; } UCSR0A |= _BV(MPCM0); /* return to MPCM mode */ } else if (buffer[0] == 0x03 && fill == 6) { /* fade to color */ for (uint8_t pos = 0; pos < PWM_CHANNELS; pos++) { global_pwm.channels[pos].speed_h = buffer[1]; global_pwm.channels[pos].speed_l = buffer[2]; global_pwm.channels[pos].target_brightness = buffer[pos + 3]; } UCSR0A |= _BV(MPCM0); /* return to MPCM mode */ } } #endif }