/** process serial data received by uart */ void check_serial_input(uint8_t data) /* {{{ */ { static uint8_t expectdata = 0; static uint8_t command = 0; //letzter Befehl static uint8_t currentstate = 0; static uint8_t fade_speed = 0; //wenn größer 0 wird gefadet, anstatt die Farbe direkt zu setzten currentstate = checkcommand(data);//Befehlszuordnung if(!expectdata){ if (currentstate == 1) {//Sonderbefehle switch(data) { case 'i': jump_to_bootloader(); break; case 'p': jump_to_bootloader(); break; } } else if (currentstate > 1) {//0 wird ausgelassen /*für Befehle die noch Werte brauchen*/ command = data; UDR0 = command; expectdata = 1; } } else { if (checkcommand(command) == 2) {//Farbsetz-Befehle static uint8_t color = 0; switch(command) { case 'r': color = 0; break; case 'g': color = 1; break; case 'b': color = 2; break; } if(fade_speed){ /* wenn zu den übergebenen farbcode gefadet werden soll */ global_pwm.channels[color].speed_h = HIGH(fade_speed); global_pwm.channels[color].speed_l = LOW(fade_speed); } else { /*wenn die übergebene Farbe sofort gesetzt werden soll */ global_pwm.channels[color].brightness=data; } global_pwm.channels[color].target_brightness=data; } else if (checkcommand(command) == 3) {//weitere Farbsetz-Befehle switch(command) { case 'f': fade_speed = data; break; } } expectdata = 0; UDR0 = 's'; } } /* }}} */
/* * Main loop */ int main(void) { /* Initialize all peripherals */ open_board(); /* Configure stdout/stderr to be keyboard input to the host */ open_streams(); led_set(3, 1, 1); for (;;) { poll_matrix(); update_matrix(); // Handle USB HID reporting // Generic USB tasks take place in interrupt USB_USBTask(); USB_HIDTask(); if (GFLAGS & GF_BOOTLOADER) break; maybe_sleep(); } /* Disable stdout/stderr. We can't use it if usb is down */ close_streams(); /* Teardown everything we setup. Nothing can be active before the jump */ close_board(); jump_to_bootloader(); }
void __attribute__ ((section (".bootlup"))) __attribute__((noreturn)) bootlup() { const uint32_t addr_to = (uint32_t)BOOTLOADER_ADDRESS*2; /* byte address of bootloader */ const uint8_t nbpages=3072/SPM_PAGESIZE; /* maximum of 3k bootloader at the moment */ #if SPM_PAGESIZE > 255 uint16_t i; #else uint8_t i; #endif for(uint8_t page=0;page<nbpages;page++) { boot_page_erase(addr_to+page*SPM_PAGESIZE); boot_spm_busy_wait (); // Wait until the memory is erased for (i=0; i<SPM_PAGESIZE; i+=2) { uint16_t w = pgm_read_word(page*SPM_PAGESIZE+i); boot_page_fill (addr_to+page*SPM_PAGESIZE+i, w); } boot_page_write (addr_to+page*SPM_PAGESIZE); // Store buffer in flash page boot_spm_busy_wait ();// Wait until the memory is erased } boot_rww_enable (); jump_to_bootloader(); }
void do_ms1_load(gint port_fd, gint file_fd) { gboolean result = FALSE; EcuState ecu_state = NOT_LISTENING; ecu_state = detect_ecu(port_fd); switch (ecu_state) { case NOT_LISTENING: output("NO response to signature request\n",FALSE); break; case IN_BOOTLOADER: output("ECU is in bootloader mode, good!\n",FALSE); break; case LIVE_MODE: output("ECU detected in LIVE! mode, attempting to access bootloader\n",FALSE); result = jump_to_bootloader(port_fd); if (result) { ecu_state = detect_ecu(port_fd); if (ecu_state == IN_BOOTLOADER) { output("ECU is in bootloader mode, good!\n",FALSE); break; } else output("Could NOT attain bootloader mode\n",FALSE); } else output("Could NOT attain bootloader mode\n",FALSE); break; } if (ecu_state != IN_BOOTLOADER) { /*output("Please jump the boot jumper on the ECU and power cycle it\n\nPress any key to continue\n",FALSE);*/ boot_jumper_prompt(); ecu_state = detect_ecu(port_fd); if (ecu_state != IN_BOOTLOADER) { output("Unable to get to the bootloader, update FAILED!\n",FALSE); boot_jumper_prompt(); } else output("Got into the bootloader, good!\n",FALSE); } result = prepare_for_upload(port_fd); if (!result) { output("Failure getting ECU into a state to accept the new firmware\n",FALSE); exit (-1); } upload_firmware(port_fd,file_fd); output("Firmware upload completed...\n",FALSE); reboot_ecu(port_fd); output("ECU reboot complete\n",FALSE); return; }
static THD_FUNCTION(ButtonThread, arg) { (void)arg; while(TRUE) { if(palReadPad(BTN_GPIO, BTN_PIN) == PAL_LOW) { jump_to_bootloader(); } chThdSleepMilliseconds(50); } }
static THD_FUNCTION(BlinkThread, arg) { (void)arg; uint8_t i; // while(TRUE) { for(i=0; i<10; i++) { palTogglePad(GPIOB, GPIOB_LED); chThdSleepMilliseconds(700); } jump_to_bootloader(); }
/** process serial data received by uart */ void check_serial_input(uint8_t data) /* {{{ */ { switch (data) { #if 0 case '1': global_pwm.channels[0].target_brightness-=1; break; case '4': global_pwm.channels[0].target_brightness+=1; break; case '2': global_pwm.channels[1].target_brightness-=1; break; case '5': global_pwm.channels[1].target_brightness+=1; break; case '3': global_pwm.channels[2].target_brightness-=1; break; case '6': global_pwm.channels[2].target_brightness+=1; break; case '0': global_pwm.channels[0].target_brightness=0; global_pwm.channels[1].target_brightness=0; global_pwm.channels[2].target_brightness=0; break; case '=': global_pwm.channels[0].target_brightness=global_pwm.channels[0].brightness; global_pwm.channels[1].target_brightness=global_pwm.channels[1].brightness; global_pwm.channels[2].target_brightness=global_pwm.channels[2].brightness; break; #if SCRIPT_SPEED_CONTROL case '>': script_threads[0].speed_adjustment--; script_threads[1].speed_adjustment--; script_threads[2].speed_adjustment--; break; case '<': script_threads[0].speed_adjustment++; script_threads[1].speed_adjustment++; script_threads[2].speed_adjustment++; break; #endif case 'i': TWCR |= _BV(TWSTA) | _BV(TWINT); break; #endif case 'p': jump_to_bootloader(); break; } } /* }}} */
void start_bootloader(void) { cli(); /* move interrupt vectors to bootloader section and jump to bootloader */ MCUCR = _BV(IVCE); MCUCR = _BV(IVSEL); #define jump_to_bootloader ((void(*)(void))0x1FC00) jump_to_bootloader(); }
static THD_FUNCTION(BlinkThread, arg) { (void)arg; uint8_t i; // while(TRUE) { for(i=0; i<5; i++) { palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); chThdSleepMilliseconds(700); palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); chThdSleepMilliseconds(700); } jump_to_bootloader(); }
static void start_bootloader(void) { cli(); /* move interrupt vectors to bootloader section and jump to bootloader */ MCUCR = _BV(IVCE); MCUCR = _BV(IVSEL); #if defined(CUL_ARDUINO) # define jump_to_bootloader ((void(*)(void))0x3800) #endif jump_to_bootloader(); }
void startBootloader(void) { TIMSK0 &= ~(1<<TOIE0); // disable timer overflow cli(); // turn off interrupts wdt_disable(); // disable watchdog timer usbDeviceDisconnect(); // disconnect from USB bus cbi(ADCSRA, ADIE); // disable ADC interrupts cbi(ADCSRA, ADEN); // disable ADC (turn off ADC power) PORTA = 0; // pull all pins low PORTB = 0; PORTC = 0; jump_to_bootloader(); }
static THD_FUNCTION(Thread2, arg) { (void)arg; chRegSetThreadName("checkbutton1"); uint8_t newstate, state = PAL_LOW; while(true) { if(palReadPad(GPIOB, GPIOB_BUTTON) != state) { chThdSleepMilliseconds(20); /* debounce */ newstate = palReadPad(GPIOB, GPIOB_BUTTON); if(newstate != state) { chThdSleepMilliseconds(2000); jump_to_bootloader(); } } chThdSleepMilliseconds(20); } }
void start_bootloader(void) { cli(); /* move interrupt vectors to bootloader section and jump to bootloader */ MCUCR = _BV(IVCE); MCUCR = _BV(IVSEL); #if defined(__AVR_AT90USB1286__) #define jump_to_bootloader ((void(*)(void))0xf000) #elif defined(__AVR_AT90USB646__) #define jump_to_bootloader ((void(*)(void))0x7800) #else #define jump_to_bootloader ((void(*)(void))0x1800) #endif jump_to_bootloader(); }
/** * Stop the system and jump to the bootloader. */ void flash_helper_jump_to_bootloader(void) { typedef void (*pFunction)(void); mc_interface_unlock(); mc_interface_release_motor(); usbDisconnectBus(&USBD1); usbStop(&USBD1); sdStop(&HW_UART_DEV); palSetPadMode(HW_UART_TX_PORT, HW_UART_TX_PIN, PAL_MODE_INPUT); palSetPadMode(HW_UART_RX_PORT, HW_UART_RX_PIN, PAL_MODE_INPUT); // Disable watchdog timeout_configure_IWDT_slowest(); chSysDisable(); pFunction jump_to_bootloader; // Variable that will be loaded with the start address of the application volatile uint32_t* jump_address; const volatile uint32_t* bootloader_address = (volatile uint32_t*)0x080E0000; // Get jump address from application vector table jump_address = (volatile uint32_t*) bootloader_address[1]; // Load this address into function pointer jump_to_bootloader = (pFunction) jump_address; // Clear pending interrupts SCB->ICSR = SCB_ICSR_PENDSVCLR_Msk; // Disable all interrupts for(int i = 0;i < 8;i++) { NVIC->ICER[i] = NVIC->IABR[i]; } // Set stack pointer __set_MSP((uint32_t) (bootloader_address[0])); // Jump to the bootloader jump_to_bootloader(); }
/** * Stop the system and jump to the bootloader. */ void flash_helper_jump_to_bootloader(void) { typedef void (*pFunction)(void); mcpwm_unlock(); mcpwm_release_motor(); usbDisconnectBus(&USBD1); usbStop(&USBD1); uartStop(&HW_UART_DEV); palSetPadMode(HW_UART_TX_PORT, HW_UART_TX_PIN, PAL_MODE_INPUT); palSetPadMode(HW_UART_RX_PORT, HW_UART_RX_PIN, PAL_MODE_INPUT); // Disable watchdog RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, DISABLE); chSysDisable(); pFunction jump_to_bootloader; // Variable that will be loaded with the start address of the application vu32* jump_address; const vu32* bootloader_address = (vu32*)0x080E0000; // Get jump address from application vector table jump_address = (vu32*) bootloader_address[1]; // Load this address into function pointer jump_to_bootloader = (pFunction) jump_address; // Clear pending interrupts SCB_ICSR = ICSR_PENDSVCLR; // Disable all interrupts for(int i = 0;i < 8;i++) { NVIC->ICER[i] = NVIC->IABR[i]; } // Set stack pointer __set_MSP((u32) (bootloader_address[0])); // Jump to the bootloader jump_to_bootloader(); }
void setup(){ // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); // 0 bits for preemption, 4 bits for subpriority /* Set up interrupt controller: 2 bits for priority (0-3), * 2 bits for sub-priority (0-3). Priorities control which * interrupts are allowed to preempt one another. */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Increase SysTick priority to be higher than USB interrupt * priority. USB code stalls inside interrupt and we can't let * this throw off the SysTick timer. */ NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup_2, SYSTICK_PRIORITY, SYSTICK_SUBPRIORITY)); NVIC_SetPriority(DMA1_Stream3_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup_2, 0, 0)); NVIC_SetPriority(DMA1_Stream4_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup_2, 0, 0)); NVIC_SetPriority(SPI2_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup_2, 1, 0)); NVIC_SetPriority(ADC_IRQn, NVIC_EncodePriority(NVIC_PriorityGroup_2, 2, 0)); ledSetup(); setLed(RED); /* check if we need to DFU boot */ configureDigitalInput(SWITCH_B_PORT, SWITCH_B_PIN, GPIO_PuPd_UP); if(isPushButtonPressed()) jump_to_bootloader(); adcSetup(); clockSetup(); setupSwitchA(footSwitchCallback); setupSwitchB(pushButtonCallback); settings.init(); midi.init(MIDI_CHANNEL); patches.init(); #ifdef EXPRESSION_PEDAL #ifndef OWLMODULAR setupExpressionPedal(); #endif #endif RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // DEBUG configureDigitalOutput(GPIOB, GPIO_Pin_1); // PB1, DEBUG LED debugClear(); #ifdef DEBUG_AUDIO RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // DEBUG configureDigitalOutput(GPIOA, GPIO_Pin_7); // PA7 DEBUG configureDigitalOutput(GPIOC, GPIO_Pin_5); // PC5 DEBUG clearPin(GPIOC, GPIO_Pin_5); // DEBUG clearPin(GPIOA, GPIO_Pin_7); // DEBUG #endif /* DEBUG_AUDIO */ usb_init(); #if SERIAL_PORT == 1 setupSerialPort1(115200); #elif SERIAL_PORT == 2 setupSerialPort2(115200); // expression pedal #warning expression pedal jack configured as serial port #ifdef EXPRESSION_PEDAL #error invalid configuration #endif #endif #ifdef OWLMODULAR configureDigitalInput(GPIOB, GPIO_Pin_6, GPIO_PuPd_NOPULL); // PB6 OWL Modular digital input configureDigitalOutput(GPIOB, GPIO_Pin_7); // PB7 OWL Modular digital output setPin(GPIOB, GPIO_Pin_7); // PB7 OWL Modular digital output #endif codec.setup(); codec.init(settings); printString("startup\n"); updateBypassMode(); codec.start(); }
/** 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 } }
int main() { int key_to_send; int n_pulses = 0; uchar i, button_release_counter = 0, state = STATE_WAIT; DDRC = 0x38; // LEDs as output PORTC |= LED_BLUE | LED_RED | LED_GREEN; DDRD &= ~0xF3; // connector ports as input DDRB &= ~0x3C; PORTD &= ~0xF3; // disable pullups for unused ports PORTB &= ~0x0C; PORTB |= 0x30; // enable pullups for PB4 and 5 cli(); for (i = 0; i < sizeof(keyboard_report); i++) // clear report initially ((uchar *) & keyboard_report)[i] = 0; wdt_enable(WDTO_1S); // enable 1s watchdog timer usbInit(); usbDeviceDisconnect(); // enforce re-enumeration for (i = 0; i < 250; i++) { // wait 500 ms wdt_reset(); // keep the watchdog happy _delay_ms(10); } usbDeviceConnect(); sei(); // Enable interrupts after re-enumeration uint32_t j; while (1) { wdt_reset(); // keep the watchdog happy usbPoll(); // characters are sent when messageState == STATE_SEND and after receiving // the initial LED state from PC (good way to wait until device is recognized) int idle = (PINB & 0x10); int pulse = (PINB & 0x20); if (idle != idle_stable) { idlectr++; if (idlectr > DEBOUNCE_MAX) idle_stable = idle; } else idlectr=0; if (pulse != pulse_stable) { pulsectr++; if (pulsectr > DEBOUNCE_MAX) pulse_stable = pulse; } else pulsectr=0; if (pulse_stable) PORTC &= ~LED_RED; else PORTC |= LED_RED; if (idle_stable) PORTC |= LED_GREEN; else PORTC &= ~LED_GREEN; if (oldidle == 42) // init { oldidle = idle_stable; oldpulse = pulse_stable; } if (oldidle && !idle_stable) n_pulses = 0; if (!oldidle == 0 && idle_stable) { if (n_pulses > 17) jump_to_bootloader(); // also check if some time has elapsed since last button press if (state == STATE_WAIT && button_release_counter == 255) { if (n_pulses >= 1 && n_pulses <=10) { state = STATE_SEND_KEY; key_to_send = n_pulses; } } button_release_counter = 0; // now button needs to be released a while until retrigger } if (!oldpulse && pulse_stable) n_pulses++; oldidle = idle_stable; oldpulse = pulse_stable; if (button_release_counter < 255) button_release_counter++; // increase release counter if (usbInterruptIsReady() && state != STATE_WAIT && LED_state != 0xff) { switch (state) { case STATE_SEND_KEY: buildReport(key_to_send); state = STATE_RELEASE_KEY; // release next PORTC &= ~LED_BLUE; break; case STATE_RELEASE_KEY: buildReport(-1); state = STATE_WAIT; // go back to waiting PORTC |= LED_BLUE; break; default: state = STATE_WAIT; // should not happen } // start sending usbSetInterrupt((void *) &keyboard_report, sizeof(keyboard_report)); } } jump_to_bootloader(); return 0; }
/** 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 }
static void do_control_request(int direction) { switch (USB_ControlRequest.bRequest) { case REQUEST_REGISTER: do_register(direction, USB_ControlRequest.wValue); break; case REQUEST_FREQUENCY: do_frequency(direction, USB_ControlRequest.wValue); break; case REQUEST_RXTX_MODE: do_rxtx_mode(direction, USB_ControlRequest.wValue); break; case REQUEST_MODINDEX: do_modindex(direction, USB_ControlRequest.wValue); break; case REQUEST_CSMA_RSSI: do_csma_rssi(direction, USB_ControlRequest.wValue); break; case REQUEST_POWER: do_power(direction, USB_ControlRequest.wValue); break; case REQUEST_AFC: do_acf(direction, USB_ControlRequest.wValue); break; case REQUEST_IFBW: do_ifbw(direction, USB_ControlRequest.wValue); break; case REQUEST_TRAINING: do_training(direction, USB_ControlRequest.wValue); break; case REQUEST_SYNCWORD: do_syncword(direction, USB_ControlRequest.wValue); break; case REQUEST_BITRATE: do_bitrate(direction, USB_ControlRequest.wValue); break; case REQUEST_TX: do_tx(direction, USB_ControlRequest.wValue); break; case REQUEST_RX: do_rx(direction, USB_ControlRequest.wValue); break; case REQUEST_TX_FREQUENCY: do_tx_frequency(direction, USB_ControlRequest.wValue); break; case REQUEST_RX_FREQUENCY: do_rx_frequency(direction, USB_ControlRequest.wValue); break; case REQUEST_SERIALNUMBER: do_serialnumber(direction, USB_ControlRequest.wValue); break; case REQUEST_FWREVISION: do_fw_revision(direction, USB_ControlRequest.wValue); break; case REQUEST_RESET: reboot(); break; case REQUEST_DFU: jump_to_bootloader(); break; } }
/** * \brief The main application. */ int main(void) { uint8_t i; uint8_t temperature[BUFFER_SIZE]; uint8_t light[BUFFER_SIZE]; char value_disp[5]; uint32_t adc_value; uint32_t light_value; double temp; /* Initialize clocks. */ sysclk_init(); /* Initialize GPIO states. */ board_init(); /* Configure ADC for light sensor. */ configure_adc(); /* Initialize at30tse. */ at30tse_init(); /* Configure IO1 buttons. */ configure_buttons(); /* Initialize SPI and SSD1306 controller. */ ssd1306_init(); ssd1306_clear(); /* Clear internal buffers. */ for (i = 0; i < BUFFER_SIZE; ++i) { temperature[i] = 0; light[i] = 0; } /* Show the start info. */ multi_language_show_start_info(); /* Wait 3 seconds to show the above message. */ delay_s(3); /* Check for valid firmware in SD card. */ check_valid_firmware(); while (true) { /* Set the trigger and jump to bootloader */ if (reset_flag) { jump_to_bootloader(); } /* Refresh page title only if necessary. */ if (app_mode_switch > 0) { app_mode = (app_mode + 1) % 3; /* Clear screen. */ ssd1306_clear(); ssd1306_set_page_address(0); ssd1306_set_column_address(0); if (app_mode == 0) { /* Temperature mode. */ ioport_set_pin_level(OLED1_LED1_PIN, OLED1_LED1_ACTIVE); ioport_set_pin_level(OLED1_LED2_PIN, !OLED1_LED2_ACTIVE); ioport_set_pin_level(OLED1_LED3_PIN, !OLED1_LED3_ACTIVE); multi_language_show_temperature_info(); } else if (app_mode == 1) { /* Light mode. */ ioport_set_pin_level(OLED1_LED2_PIN, OLED1_LED2_ACTIVE); ioport_set_pin_level(OLED1_LED1_PIN, !OLED1_LED1_ACTIVE); ioport_set_pin_level(OLED1_LED3_PIN, !OLED1_LED3_ACTIVE); multi_language_show_light_info(); } else { /* SD mode. */ ioport_set_pin_level(OLED1_LED3_PIN, OLED1_LED3_ACTIVE); ioport_set_pin_level(OLED1_LED1_PIN, !OLED1_LED1_ACTIVE); ioport_set_pin_level(OLED1_LED2_PIN, !OLED1_LED2_ACTIVE); sd_listing_pos = 0; /* Show SD card info. */ display_sd_info(); } app_mode_switch = 0; } /* Shift graph buffers. */ for (i = 0; i < (BUFFER_SIZE - 1); ++i) { temperature[i] = temperature[i + 1]; light[i] = light[i + 1]; } /* Get temperature. */ if (at30tse_read_temperature(&temp) == TWI_SUCCESS) { /* Don't care about negative temperature. */ if (temp < 0) { temp = 0; } /* Update temperature for display. */ /* Note: rescale to 0~24 for better rendering. */ if (temp > 40) { temperature[BUFFER_SIZE - 1] = 24; } else { temperature[BUFFER_SIZE - 1] = (uint8_t)temp * 24 / 40; } } else { /* Error print zero values. */ temperature[BUFFER_SIZE - 1] = 0; } /* Get light sensor information. */ /* Rescale to 0~24 for better rendering. */ adc_start_software_conversion(ADC); adc_value = adc_channel_get_value(ADC, ADC_CHANNEL_0); light[BUFFER_SIZE - 1] = 24 - adc_value * 24 / 1024; if (app_mode == 0) { /* Display temperature in text format. */ sprintf(value_disp, "%d", (uint8_t)temp); ssd1306_set_column_address(98); ssd1306_write_command(SSD1306_CMD_SET_PAGE_START_ADDRESS(0)); ssd1306_write_text(" "); /* Avoid character overlapping. */ if (temp < 10) { ssd1306_clear_char(); } ssd1306_write_text(value_disp); /* Display degree symbol. */ ssd1306_write_data(0x06); ssd1306_write_data(0x06); ssd1306_write_text("c"); /* Refresh graph. */ ssd1306_draw_graph(0, 2, BUFFER_SIZE, 2, temperature); } else if (app_mode == 1) { light_value = 100 - (adc_value * 100 / 1024); sprintf(value_disp, "%lu", light_value); ssd1306_set_column_address(98); ssd1306_write_command(SSD1306_CMD_SET_PAGE_START_ADDRESS(0)); ssd1306_write_text(" "); /* Avoid character overlapping. */ if (light_value < 10) { ssd1306_clear_char(); } ssd1306_write_text(value_disp); ssd1306_write_text("%"); /* Avoid character overlapping. */ if (light_value < 100) { ssd1306_clear_char(); } /* Refresh graph. */ ssd1306_draw_graph(0, 2, BUFFER_SIZE, 2, light); } else { /** * Refresh screen if card was inserted/removed or * browsing content. */ if (sd_update == 1) { /* Clear screen. */ ssd1306_clear(); ssd1306_set_page_address(0); ssd1306_set_column_address(0); if (sd_listing_pos == 0) { /* Show SD card info. */ display_sd_info(); } else { /* List SD card files. */ display_sd_files_unicode(); } sd_update = 0; } } /* Wait and stop screen flickers. */ delay_ms(150); if (app_mode_switch == 0) { pio_enable_interrupt(OLED1_PIN_PUSHBUTTON_1_PIO, OLED1_PIN_PUSHBUTTON_1_MASK); } if (sd_update == 0) { pio_enable_interrupt(OLED1_PIN_PUSHBUTTON_2_PIO, OLED1_PIN_PUSHBUTTON_2_MASK); pio_enable_interrupt(OLED1_PIN_PUSHBUTTON_3_PIO, OLED1_PIN_PUSHBUTTON_3_MASK); } } }
int main(void) { #ifdef BOOTLOADER_SUPPORT _IVREG = _BV(IVCE); /* prepare ivec change */ _IVREG = _BV(IVSEL); /* change ivec to bootloader */ #endif /* Clear the MCUSR Register to avoid endless wdreset loops */ unsigned char reset_reason = 0; #ifdef MCUCSR reset_reason = MCUCSR; MCUCSR = 0; #else #ifdef MCUSR reset_reason = MCUSR; MCUSR = 0; #endif #endif /* Default DDR Config */ #if IO_HARD_PORTS == 4 && DDR_MASK_A != 0 DDRA = DDR_MASK_A; #endif #if DDR_MASK_B != 0 DDRB = DDR_MASK_B; #endif #if DDR_MASK_C != 0 DDRC = DDR_MASK_C; #endif #if DDR_MASK_D != 0 DDRD = DDR_MASK_D; #endif #if IO_HARD_PORTS == 6 #if DDR_MASK_E != 0 DDRE = DDR_MASK_E; #endif #if DDR_MASK_F != 0 DDRF = DDR_MASK_F; #endif #endif #ifdef STATUSLED_POWER_SUPPORT PIN_SET(STATUSLED_POWER); #endif //FIXME: zum ethersex meta system hinzufügen, aber vor allem anderem initalisieren debug_init(); debug_printf("Ethersex " VERSION_STRING " (Debug mode)\n"); #ifdef DEBUG_RESET_REASON if (bit_is_set(reset_reason, BORF)) debug_printf("reset: Brown-out\n"); else if (bit_is_set(reset_reason, PORF)) debug_printf("reset: Power on\n"); else if (bit_is_set(reset_reason, WDRF)) debug_printf("reset: Watchdog\n"); else if (bit_is_set(reset_reason, EXTRF)) debug_printf("reset: Extern\n"); else debug_printf("reset: Unknown\n"); #endif #ifdef BOOTLOADER_SUPPORT /* disable interrupts */ cli(); #else /* enable interrupts */ sei(); #endif //BOOTLOADER_SUPPORT #ifdef USE_WATCHDOG debug_printf("enabling watchdog\n"); #ifdef DEBUG /* for debugging, test reset cause and jump to bootloader */ if (MCUSR & _BV(WDRF)) { debug_printf("bootloader...\n"); jump_to_bootloader(); } #endif /* set watchdog to 2 seconds */ wdt_enable(WDTO_2S); wdt_kick(); #else //USE_WATCHDOG debug_printf("disabling watchdog\n"); wdt_disable(); #endif //USE_WATCHDOG #ifdef ADC_SUPPORT /* ADC Prescaler to 64 */ ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1); /* ADC set Voltage Reference to extern*/ /* FIXME: move config to the right place */ ADMUX = ADC_REF; //_BV(REFS0) | _BV(REFS1); #endif #if defined(RFM12_SUPPORT) || defined(ENC28J60_SUPPORT) \ || defined(DATAFLASH_SUPPORT) spi_init(); #endif ethersex_meta_init(); #ifdef RFM12_SUPPORT rfm12_init(); #ifdef TEENSY_SUPPORT cli (); rfm12_trans (0xa620); /* rfm12_setfreq(RFM12FREQ(433.92)); */ rfm12_trans (0x94ac); /* rfm12_setbandwidth(5, 1, 4); */ #ifdef RFM12_IP_SUPPORT rfm12_trans (0xc610); /* rfm12_setbaud(192); */ rfm12_trans (0x9820); /* rfm12_setpower(0, 2); */ rfm12_rxstart(); #endif /* RFM12_IP_SUPPORT */ sei (); #else /* TEENSY_SUPPORT */ rfm12_setfreq(RFM12FREQ(433.92)); rfm12_setbandwidth(5, 1, 4); #ifdef RFM12_IP_SUPPORT rfm12_setbaud(CONF_RFM12_BAUD / 100); rfm12_setpower(0, 2); rfm12_rxstart(); #endif /* RFM12_IP_SUPPORT */ #endif /* not TEENSY_SUPPORT */ #endif /* RFM12_SUPPORT */ /* must be called AFTER all other initialization */ #ifdef PORTIO_SUPPORT portio_init(); #elif defined(NAMED_PIN_SUPPORT) np_simple_init(); #endif #ifdef ENC28J60_SUPPORT debug_printf("enc28j60 revision 0x%x\n", read_control_register(REG_EREVID)); debug_printf("mac: %02x:%02x:%02x:%02x:%02x:%02x\n", uip_ethaddr.addr[0], uip_ethaddr.addr[1], uip_ethaddr.addr[2], uip_ethaddr.addr[3], uip_ethaddr.addr[4], uip_ethaddr.addr[5] ); #endif #ifdef STATUSLED_BOOTED_SUPPORT PIN_SET(STATUSLED_BOOTED); #endif ethersex_meta_startup(); /* main loop */ while(1) { wdt_kick(); ethersex_meta_mainloop(); #ifdef SD_READER_SUPPORT if (sd_active_partition == NULL) { if (! sd_try_init ()) vfs_sd_try_open_rootnode (); wdt_kick(); } #endif #ifndef BOOTLOAD_SUPPORT if(status.request_bootloader) { #ifdef CLOCK_CRYSTAL_SUPPORT _TIMSK_TIMER2 &= ~_BV(TOIE2); #endif #ifdef DCF77_SUPPORT ACSR &= ~_BV(ACIE); #endif cli(); jump_to_bootloader(); } #ifndef TEENSY_SUPPORT if(status.request_wdreset) { cli(); wdt_enable(WDTO_15MS); for(;;); } #endif if(status.request_reset) { cli(); void (* reset)(void) = NULL; reset(); } #endif } }
int main (void) { #ifdef BOOTLOADER_SUPPORT _IVREG = _BV (IVCE); /* prepare ivec change */ _IVREG = _BV (IVSEL); /* change ivec to bootloader */ #endif /* Default DDR Config */ #if IO_HARD_PORTS >= 4 && DDR_MASK_A != 0 DDRA = DDR_MASK_A; #endif #if DDR_MASK_B != 0 DDRB = DDR_MASK_B; #endif #if DDR_MASK_C != 0 DDRC = DDR_MASK_C; #endif #if DDR_MASK_D != 0 DDRD = DDR_MASK_D; #endif #if IO_HARD_PORTS >= 6 #if DDR_MASK_E != 0 DDRE = DDR_MASK_E; #endif #if DDR_MASK_F != 0 DDRF = DDR_MASK_F; #endif #endif #if IO_HARD_PORTS >= 7 #if DDR_MASK_G != 0 DDRG = DDR_MASK_G; #endif #endif #ifdef STATUSLED_POWER_SUPPORT PIN_SET(STATUSLED_POWER); #endif //FIXME: zum ethersex meta system hinzufügen, aber vor allem anderem initalisieren debug_init(); debug_printf("%S (Debug mode)\n", pstr_E6_VERSION_STRING_LONG); #ifdef DEBUG_RESET_REASON if (bit_is_set (mcusr_mirror, BORF)) debug_printf("reset: Brown-out\n"); else if (bit_is_set (mcusr_mirror, PORF)) debug_printf("reset: Power on\n"); else if (bit_is_set (mcusr_mirror, WDRF)) debug_printf("reset: Watchdog\n"); else if (bit_is_set (mcusr_mirror, EXTRF)) debug_printf("reset: Extern\n"); else debug_printf("reset: Unknown\n"); #endif #ifdef BOOTLOADER_SUPPORT /* disable interrupts */ cli (); wdt_disable(); #endif //BOOTLOADER_SUPPORT /* enable interrupts */ sei (); #ifdef USE_WATCHDOG debug_printf("enabling watchdog\n"); #ifdef DEBUG /* for debugging, test reset cause and jump to bootloader */ if (MCU_STATUS_REGISTER & _BV (WDRF)) { debug_printf("bootloader...\n"); jump_to_bootloader(); } #endif /* set watchdog to 2 seconds */ wdt_enable(WDTO_2S); wdt_kick(); #else //USE_WATCHDOG debug_printf("disabling watchdog\n"); wdt_disable(); #endif //USE_WATCHDOG #ifdef SPI_SUPPORT spi_init(); #endif ethersex_meta_init(); /* must be called AFTER all other initialization */ #ifdef PORTIO_SUPPORT portio_init(); #elif defined(NAMED_PIN_SUPPORT) np_simple_init(); #endif #ifdef ENC28J60_SUPPORT debug_printf ("enc28j60 revision 0x%x\n", read_control_register (REG_EREVID)); debug_printf ("mac: %02x:%02x:%02x:%02x:%02x:%02x\n", uip_ethaddr.addr[0], uip_ethaddr.addr[1], uip_ethaddr.addr[2], uip_ethaddr.addr[3], uip_ethaddr.addr[4], uip_ethaddr.addr[5]); #endif #ifdef STATUSLED_BOOTED_SUPPORT PIN_SET(STATUSLED_BOOTED); #endif ethersex_meta_startup(); /* main loop */ while (1) { wdt_kick(); ethersex_meta_mainloop(); #ifdef SD_READER_SUPPORT if (sd_active_partition == NULL) { if (!sd_try_init()) { #ifdef VFS_SD_SUPPORT vfs_sd_try_open_rootnode(); #endif } wdt_kick(); } #endif #ifdef BOOTLOADER_JUMP if (status.request_bootloader) { #ifdef MBR_SUPPORT mbr_config.bootloader = 1; write_mbr(); #endif #ifdef CLOCK_CRYSTAL_SUPPORT TIMER_8_AS_1_INT_OVERFLOW_OFF; #endif #ifdef DCF77_SUPPORT ACSR &= ~_BV (ACIE); #endif cli(); jump_to_bootloader(); } #endif #ifndef TEENSY_SUPPORT if (status.request_wdreset) { cli(); wdt_enable(WDTO_15MS); for (;;); } #endif if (status.request_reset) { cli(); void (*reset) (void) = NULL; reset(); } } }
uint8_t cmd_handler(uint8_t cmd, uint8_t * param, uint8_t * result) { if(cmd == CMD_BRIGHTNESS_DOWN){ if(global_pwm.dim > 0){ global_pwm.dim--; } }else if(cmd == CMD_BRIGHTNESS_UP){ if(global_pwm.dim < 255){ global_pwm.dim++; } }else if(cmd == CMD_FULL_BRIGHTNESS){ global_pwm.dim=255; }else if(cmd == CMD_ZERO_BRIGHTNESS){ global_pwm.dim=0; }else if(cmd == CMD_POWER){ if(global.state != STATE_STANDBY){ global.nextstate = global.state; if(global.nextstate == STATE_SLEEP) global.nextstate = STATE_RUNNING; //global.state = STATE_ENTERSTANDBY; control_standby(0); }else{ global.state = STATE_LEAVESTANDBY; } }else if(cmd == CMD_SET_SCRIPT){ if(sizeof(global_playlist)/(sizeof(struct playlist_t)) > param[0]) cmd_setscript( global_playlist[param[0]].execute, global_playlist[param[0]].position); }else if(cmd == CMD_FASTER){ if(script_threads[0].speed_adjustment < 8){ script_threads[0].speed_adjustment++; respeed(&script_threads[0]); //update_brightness(); //execute_script_threads(); } }else if(cmd == CMD_SLOWER){ if(script_threads[0].speed_adjustment > -8){ script_threads[0].speed_adjustment--; respeed(&script_threads[0]); //update_brightness(); //execute_script_threads(); } }else if(cmd == CMD_PAUSE){ if(global.state != STATE_PAUSE) global.state = STATE_PAUSE; else global.state = STATE_RUNNING; }else if(cmd == CMD_SAVE){ settings_save(); }else if(cmd == CMD_SLEEP){ global.state = STATE_ENTERSLEEP; }else if(cmd == CMD_RED){ global_pwm.channel_modifier = 0; }else if(cmd == CMD_GREEN){ global_pwm.channel_modifier = 1; }else if(cmd == CMD_BLUE){ global_pwm.channel_modifier = 2; }else if(cmd == CMD_COLOR_UP){ global_pwm.channels[global_pwm.channel_modifier].brightness++; }else if(cmd == CMD_COLOR_DOWN){ global_pwm.channels[global_pwm.channel_modifier].brightness--; }else if(cmd == CMD_COLOR_FULL){ global_pwm.channels[global_pwm.channel_modifier].brightness=255; }else if(cmd == CMD_COLOR_ZERO){ global_pwm.channels[global_pwm.channel_modifier].brightness=0; }else if(cmd == CMD_GET_VERSION){ //strcpy((char *) result,"D="); //strcat((char *) result,__DATE__); //strcat((char *) result,"H="); //strcpy((char *) result, "D="__DATE__"H="); //strcat((char *) result,__DATE__); if( result == NULL ) return 0; sprintf((char *)result,"D="__DATE__" H=%d",global.config); return strlen((char *)result); }else if(cmd == CMD_GET_STATE){ if( result == NULL ) return 0; result[0] = global.state; result[1] = script_threads[0].speed_adjustment; result[2] = global_pwm.dim; return 3; }else if(cmd == CMD_SET_SPEED){ script_threads[0].speed_adjustment = param[0]; }else if(cmd == CMD_SET_STATE){ global.state = param[0]; }else if(cmd == CMD_SET_BRIGHTNESS){ global_pwm.dim = param[0]; }else if(cmd == CMD_SET_COLOR){ control_setColor(param[0],param[1],param[2]); }else if(cmd == CMD_RESET){ jump_to_bootloader(); }else if(cmd == CMD_FADE){ uint16_t speed = (param[3]<<8)+param[4]; control_fade(param[0],param[1],param[2],speed); }else if(cmd == CMD_FADEMS){ uint16_t time = (param[3]<<8)+param[4]; control_fadems(param[0],param[1],param[2],time); }else if(cmd == CMD_FADEMSALT){ uint16_t time = (param[3]<<8)+param[4]; control_fademsalt(param[0],param[1],param[2],time); }else if(cmd == CMD_GET_VOLTAGE){ if( result == NULL ) return 0; uint16_t adc = adc_getChannel(6); sprintf((char *)result,"V=%u",adc); return strlen((char *)result); }else if(cmd == CMD_GET_COLOR){ if( result == NULL ) return 0; result[0] = global_pwm.channels[0].brightness; result[1] = global_pwm.channels[1].brightness; result[2] = global_pwm.channels[2].brightness; return 3; }else if(cmd == CMD_FLASH){ uint16_t time = (param[3]<<8)+param[4]; control_flash(param[0],param[1],param[2],time); } return 0; }