void morse_glyph(char c) { switch(c) { case '.': switch_on(dot_pause); break; case '-': switch_on(dash_pause); break; default: raspi_timer_wait(word_pause); break; } switch_off(gap_pause); }
int switch_action(struct config *c) { char *statew; char state; if (0 < login(c)) { return 1; } switch (c->device.actor_command) { case CMD_ON: state = switch_on(c->avm.hostname, session_id, c->device.ain); break; case CMD_OFF: state = switch_off(c->avm.hostname, session_id, c->device.ain); break; case CMD_TOGGLE: state = switch_toggle(c->avm.hostname, session_id, c->device.ain); break; default: break; } if (SWITCH_STATE_ON == state) { statew = "on"; } else { statew = "off"; } utlog(LOG_NOTICE, " %s was turned %s\n", c->device.ain, statew); session_end(c->avm.hostname, session_id); session_id = NULL; return 0; }
int main(void) { switch_on(); DDRB = MASK_MERGE(~0 * DD_INPUT, 1 << OC0B_BIT | 1 << 2, ~0 * DD_OUTPUT); /* Unused pins pulled up to avoid floating inputs using excess power. Input pins floating. */ REG_MOD(PORTB, ~(1 << OC0B_BIT | 1 << 2), MASK_MERGE(~0 * PORT_PULLUP, 1 << AIN0_BIT | 1 << BAT_BIT, ~0 * PORT_FLOAT)); DIDR0 = ~0; BIT_MOD(ADCSRA, ADEN, 0); REG_MOD(ADMUX, MUX_MASK << MUX0, BAT_ADC << MUX0); BIT_MOD(ADCSRB, ACME, 1); ACSR = 0 << ACD | 0 << ACBG | ACIS_TOGGLE << ACIS0; BIT_MOD(ACSR, ACI, 1); WDTCR = 1 << WDTIE | WDP_16MS; BIT_MOD(ACSR, ACIE, 1); sei(); while(1) { sleep_mode(); } }
void Engine_State::change(double RPM) { const double rpmOff = 50; // 50 const double rpmIdle = 2000; // 150 if ( RPM < rpmOff ) { switch_off(); set_to_idle(); } else if ( RPM < rpmIdle ) { switch_on(); set_to_idle(); } else { switch_on(); unset_idle(); } }
/* Spoof Limit Checking: There is a known issue with the Vsense values, but we should at least get the framework set up for limit checking on arbitrary values. The power board should have upper and lower limits for vsense and csense data, and turn off components if their voltage/current is too high. ONLY CHECKS BATTERY 1 VOLTAGE LINE RIGHT NOW. */ void limit_check( void ) { unsigned char sw; SVIT_t *component; // turn off all switches and send ack_command w/ value of SAFE_MODE if (percent < SAFE_MODE) { safe_mode = 1; for (sw = 0; sw < sizeof(components); sw++) { component = &svit[components[sw]]; switch_off( component->switch_num ); component->switch_state = SW_OFF; } component = &svit[TORQUER_1]; component->switch_state = SW_OFF; component = &svit[TORQUER_2]; component->switch_state = SW_OFF; component = &svit[TORQUER_3]; component->switch_state = SW_OFF; torquer_off(TORQUER_1); torquer_off(TORQUER_2); torquer_off(TORQUER_3); // Only transmit once if (!transmit_safe && !been_to_safe) { transmit_packet( 0, VCP_ACK, SAFE_MODE); transmit_safe = 1; } else {} been_to_safe = 1; transmit_safe = 1; } else if (percent > SHUNT_MODE) { safe_mode = 0; // turn on the maestro and send ack_command w/ value of SHUNT_MODE component = &svit[MAESTRO]; switch_on( component->switch_num ); component->switch_state = SW_ON; // Only transmit once if (!transmit_shunt && !been_to_shunt) { transmit_packet( 0, VCP_ACK, SHUNT_MODE); } else {} been_to_shunt = 1; transmit_shunt = 1; } else {} // To avoid annoying compile warning }
@far @interrupt void I2CInterruptHandle (void) { #else void I2CInterruptHandle (void) interrupt 19 { #endif u8 sr1,sr2,cr2; switch_on(LED1); //test purpose /* Get Value of Status registers and Control register 2 */ sr1 = I2C->SR1; sr2 = I2C->SR2; cr2 = I2C->CR2; /* Check for error in communication */ if (sr2 != 0) { ErrProc(); } /* Start bit detected */ if ((sr1 & I2C_SR1_SB) == 1) { switch(STATE) { case SB_01: if (u8_AddType_cpy == TEN_BIT_ADDRESS) { I2C->DR = (u8)(((u16_SlaveAdd_cpy >> 7) & 6) | 0xF0); // send header of 10-bit device address (R/W = 0) STATE = ADD10_02; break; } else { I2C->DR = (u8)(u16_SlaveAdd_cpy << 1); // send 7-bit device address & Write (R/W = 0) STATE = ADDR_03; break; } case SB_11: if (u8_AddType_cpy == TEN_BIT_ADDRESS) { I2C->DR = (u8)(((u16_SlaveAdd_cpy >> 7) & 6) | 0xF1);// send header of 10-bit device address (R/W = 1) } else {
/****************************************************************************** * Function name : main * Description : Main testing loop * Input param : None * Return : None * See also : None *******************************************************************************/ void main (void) { /* peripheral initialization */ #ifdef FAST_I2C_MODE CLK->CKDIVR = 0x00; // sys clock / 1 #else CLK->CKDIVR = 0x01; // sys clock / 2 #endif // Set GPIO for LED uses GPIOH->DDR |= 0x0F; GPIOH->CR1 |= 0x0F; // initialize timer 4 mandatory for timout and tick measurement TIM4_Init(); // Initialize I2C for communication I2C_Init(); // initialization of dummy field for test purpose memcpy(Dummy, DUMMY_INIT, MAX_DUMMY); #ifndef _COSMIC_ err_save= 0; TIM4_tout= loop_count= 0; #endif // Enable all interrupts enableInterrupts(); /* main test loop */ while(1) { // switch on LED1 at the beginning of test switch_on(LED1); // write 1 data bytes with offset 8 from Dummy filed to slave memory set_tout_ms(10); I2C_WriteRegister(8, 1, &Dummy[8]); // read 1 byte with offset 8 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(8, 1, &Dummy[8]); } // write 6 bytes with offset 2 from Dummy filed to slave memory if(tout()) { set_tout_ms(10); I2C_WriteRegister(2, 6, &Dummy[2]); } // read 6 bytes with offset 2 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(2, 6, &Dummy[2]); } // write 1 byte with offset 9 from Dummy filed to slave memory if(tout()) { set_tout_ms(10); I2C_WriteRegister(9, 1, &Dummy[9]); } // read 1 byte with offset 9 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(9, 1, &Dummy[9]); } // write 2 bytes with offset 0 from Dummy filed to slave memory if(tout()) { set_tout_ms(10); I2C_WriteRegister(0, 2, &Dummy[0]); } // read 2 bytes with offset 0 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(0, 2, &Dummy[0]); } // if a timout error occures switch on LED2 if(!tout()) switch_on(LED2); // switch off LED1 at the end of test switch_off(LED1); // check if dummy field is not corrupted => switch on LED 4 if test not successful if(memcmp(Dummy, DUMMY_INIT, MAX_DUMMY) != 0) switch_on(LED4); delay(1); } }
inline void receive_message( uint8_t uart, uint8_t* message, uint8_t message_size )//was inline { uint8_t command = message[VCP_COMMAND_FIELD]; uint8_t payload = message[VCP_PAYLOAD_FIELD]; switch(command) { case VCP_COMPONENT_ON: // Reset CDH IB Heartbeat timer if ( svit[payload].switch_num != SW_NULL ) { switch_on( svit[payload].switch_num ); svit[payload].switch_state = 1; } transmit_packet( uart, VCP_ACK, command ); break; case VCP_COMPONENT_OFF: if ( svit[payload].switch_num != SW_NULL ) { switch_off( svit[payload].switch_num ); svit[payload].switch_state = 0; } transmit_packet( uart, VCP_ACK, command ); break; case VCP_POWER_CYCLE: if ( svit[payload].switch_num != SW_NULL ) { switch_off( svit[payload].switch_num ); svit[payload].switch_state = 0; } _delay_us(1); if ( svit[payload].switch_num != SW_NULL ) { switch_on( svit[payload].switch_num ); svit[payload].switch_state = 1; } transmit_packet( uart, VCP_ACK, command ); break; case VCP_TORQ_CTRL: transmit_packet( uart, VCP_ACK, command ); break; case VCP_GET_TELEMETRY: transmit_packet( uart, VCP_POWER_TELEMETRY, 0); break; case VCP_FORCE_ON: if ( svit[payload].switch_num != SW_NULL ) { switch_on( svit[payload].switch_num ); svit[payload].switch_state = 1; svit[payload].force_on = 1; } transmit_packet( uart, VCP_ACK, command ); break; case VCP_CRIT_V_CHANGE: if ( svit[payload].switch_num != SW_NULL ) { svit[payload].V_upper_limit = message[VCP_PAYLOAD_FIELD + 1]; V_upper_val_change= message[VCP_PAYLOAD_FIELD + 1]; } //transmit_packet( uart, VCP_ACK, 0); break; case VCP_CRIT_I_CHANGE: if ( svit[payload].switch_num != SW_NULL ) { svit[payload].I_upper_limit = message[VCP_PAYLOAD_FIELD + 1]; I_upper_val_change= message[VCP_PAYLOAD_FIELD + 1]; } //transmit_packet( uart, VCP_ACK, 0); break; default: transmit_packet( uart, VCP_INVALID_COMMAND, 0 ); break; } }
/** * Start the X server by fork-exec, turn card on and load driver if needed. * If after this method finishes X is running, it was successfull. * If it somehow fails, X should not be running after this method finishes. */ void start_secondary(void) { char driver[BUFFER_SIZE] = {0}; /* enable card if the switcher is available */ if (switcher) { if (switch_on() != SWITCH_ON) { set_bb_error("Could not enable discrete graphics card"); return; } if (pci_config_restore(pci_bus_id_discrete, &pci_config_state_discrete)) { bb_log(LOG_WARNING, "Could not restore PCI configuration space: %s\n", strerror(errno)); } } //if runmode is BB_RUN_EXIT, do not start X, we are shutting down. if (bb_status.runmode == BB_RUN_EXIT) { return; } if (pci_get_driver(driver, pci_bus_id_discrete, sizeof driver)) { /* if the loaded driver does not equal the driver from config, unload it */ if (strcasecmp(bb_config.driver, driver)) { if (!module_unload(driver)) { /* driver failed to unload, aborting */ return; } } } /* load the driver if none was loaded or if the loaded driver did not match * the configured one */ if (strcasecmp(bb_config.driver, driver)) { char *module_name = bb_config.module_name; char *driver_name = bb_config.driver; if (!module_load(module_name, driver_name)) { set_bb_error("Could not load GPU driver"); return; } } //no problems, start X if not started yet if (!bb_is_running(bb_status.x_pid)) { char pci_id[12]; static char *x_conf_file; snprintf(pci_id, 12, "PCI:%02x:%02x:%o", pci_bus_id_discrete->bus, pci_bus_id_discrete->slot, pci_bus_id_discrete->func); if (!x_conf_file) { x_conf_file = xorg_path_w_driver(bb_config.x_conf_file, bb_config.driver); } bb_log(LOG_INFO, "Starting X server on display %s.\n", bb_config.x_display); char *x_argv[] = { XORG_BINARY, bb_config.x_display, "-config", x_conf_file, "-sharevts", "-nolisten", "tcp", "-noreset", "-verbose", "3", "-isolateDevice", pci_id, "-modulepath", bb_config.mod_path, NULL }; if (!*bb_config.mod_path) { x_argv[12] = 0; //remove -modulepath if not set } //close any previous pipe, if it (still) exists if (bb_status.x_pipe[0] != -1){close(bb_status.x_pipe[0]); bb_status.x_pipe[0] = -1;} if (bb_status.x_pipe[1] != -1){close(bb_status.x_pipe[1]); bb_status.x_pipe[1] = -1;} //create a new pipe if (pipe2(bb_status.x_pipe, O_NONBLOCK)){ set_bb_error("Could not create output pipe for X"); return; } bb_status.x_pid = bb_run_fork_ld_redirect(x_argv, bb_config.ld_path, bb_status.x_pipe[1]); //close the end of the pipe that is not ours if (bb_status.x_pipe[1] != -1){close(bb_status.x_pipe[1]); bb_status.x_pipe[1] = -1;} } //check if X is available, for maximum 10 seconds. time_t xtimer = time(0); Display * xdisp = 0; while ((time(0) - xtimer <= 10) && bb_is_running(bb_status.x_pid)) { xdisp = XOpenDisplay(bb_config.x_display); if (xdisp != 0) { break; } check_xorg_pipe();//make sure Xorg errors come in smoothly usleep(100000); //don't retry too fast } check_xorg_pipe();//make sure Xorg errors come in smoothly //check if X is available if (xdisp == 0) { //X not available /// \todo Maybe check X exit status and/or messages? if (bb_is_running(bb_status.x_pid)) { //X active, but not accepting connections set_bb_error("X unresponsive after 10 seconds - aborting"); bb_stop(bb_status.x_pid); } else { //X terminated itself set_bb_error("X did not start properly"); } } else { //X accepted the connetion - we assume it works XCloseDisplay(xdisp); //close connection to X again bb_log(LOG_INFO, "X successfully started in %i seconds\n", time(0) - xtimer); //reset errors, if any set_bb_error(0); } }//start_secondary