void processTWI( void ) { uint8_t b,p1,p2; b = usiTwiReceiveByte(); switch (b) { case 0x81: // set slave address p1 = usiTwiReceiveByte(); if(p1 < 128) // Address is 7 bit { eeprom_update_byte(&b_slave_address, p1); usiTwiSlaveInit(eeprom_read_byte(&b_slave_address)); } break; case 0x82: // clear led led_clear(); break; case 0x83: // set led brightness, p1=led, p2=brightness p1 = usiTwiReceiveByte(); p2 = usiTwiReceiveByte(); set_led_pulse(p1,0); // Turn off pulsing set_brightness(p1,p2); break; case 0x84: // Set to pulse led, p1=led, p2 = (0 = OFF, 1 = ON) set_led_pulse(usiTwiReceiveByte(),usiTwiReceiveByte()); break; case 0x85: // Dim up/down led to specific value, p1=led, p2=value to dim to dim_led(usiTwiReceiveByte(),usiTwiReceiveByte()); break; case 0x86: // get firmware revision usiTwiTransmitByte(FIRMWARE_REVISION); break; case 0x90: // get keyUp Event usiTwiTransmitByte(getKeyUp()); break; case 0x91: // get KeyDown Event usiTwiTransmitByte(getKeyDown()); break; case 0x92: // set keyrepeat set_keyrepeat(usiTwiReceiveByte(),usiTwiReceiveByte()); break; case 0xFE: // reset to known state led_clear(); button_init(); flushTwiBuffers(); break; case 0xFF: // flush the bus break; default: break; } }
void update_motif(void) { motif_t * m = db->motifs; for (uint32_t i = 1; i < current_motif_index; i++) m = m->next; sprintf(current_motif_title, "%i/%i -- %s", (int)current_motif_index, (int)db->nb_motifs, m->name); current_motif_desc = m->desc; MULTIEDIT_SetText(motif_desc_widget, current_motif_desc); TEXT_SetText(motif_name_widget, current_motif_title); led_clear(); point_t * p = m->points; while (p != NULL) { led_set(p->x, p->y, p->z); p = p->next; } clear_points2blink(); option_t * o = m->options; while (o != NULL) { handle_option(o); o = o->next; } }
void led_init() { PEADDR = 0x02; //set alternative function (port E) PECTL = 0x00; PEADDR = 0x01; //set datadirection to output (port E) PECTL = 0x00; PGADDR = 0x02; //set alternative function (port G) PGCTL = 0x00; PGADDR = 0x01; //set datadirection to output (port G) PGCTL = 0x00; T0CTL = 0x01; //Disable Timer0 and set it to continous mode T0CTL |= 0x38; //Set the prescale value T0H = 0x00; T0L = 0x01; //reset timer T0RH = 0x00; // T0RH = 0x02; T0RL = 0xD0; //set reload value (0.5 ms) IRQ0ENH &= 0xDF; IRQ0ENL |= 0x20; //enable Timer0 interrupt, and set low priority T0CTL |= 0x80; //enable Timer0 EI(); //enable interrupts SET_VECTOR(TIMER0, drawtimer); //set timer interrupt function led_clear(); //clear the LEDs led_onall(); }
/** * Gyroscope presission automatic bias null calibration */ void adis_recalibrate_gyros( void ) { unsigned char tmp[2]; // Temporary variable unsigned char addr[2]; // Address to TEMP_OUT addr[0] = 0xbe; // This is the address for restoring factory calibration addr[1] = 0x10; // This is the data portion, wich the IMU ignores when readiing memcpy(tmp,addr,2); // Copy address to temporay variable spi0_send(tmp,1,ADIS_CS); // Send data printf("The gyros are being recalibrated.\n\rDON'T TOUCH THE IMU! FFS\n\r"); // Print the recieved data led_flash(0); led_set(0); for(int i = 30; i>0 ; i--){ printf("%i second(s) left\n\r",i); vTaskDelay(1000/portTICK_RATE_MS); } led_clear(0); led_set(1); vTaskDelay(500/portTICK_RATE_MS); led_clear(1); printf("The gyros have been recalibrated.\n\r"); // Print the recieved data }
static void led_brightness(struct led_classdev *led_cdev, enum led_brightness value) { struct cpld_led_dev *led_dev = container_of(led_cdev, struct cpld_led_dev, cdev); if (value) led_set(led_dev->mask, led_dev->base); else led_clear(led_dev->mask, led_dev->base); }
bool bus_power(bool enable) { if (enable) { if (bus_voltage_get() > 0.5 && palReadPad(GPIOB, GPIOB_V_BUS_ENABLE) == 0) { return false; } palSetPad(GPIOB, GPIOB_V_BUS_ENABLE); } else { led_clear(CAN1_PWR_LED); palClearPad(GPIOB, GPIOB_V_BUS_ENABLE); } return true; }
void bus_voltage_adc_conversion(void) { // read bus voltage adcStartConversion(&ADCD1, &adcgrpcfg, adc_samples, ADC_BUF_DEPTH); chBSemWait(&adc_wait); // set LED float voltage = bus_voltage_get(); if (voltage > 4.5f && voltage < 5.5f) { led_set(CAN1_PWR_LED); } else if (voltage > 0.5f || voltage > 5.5f) { // voltage warning led_toggle(CAN1_PWR_LED); } else { led_clear(CAN1_PWR_LED); } }
void main(void) { IRCF2=1; IRCF1=1; IRCF0=1; //8MHz internal RC oscillator ADCON0 = 0b000001; //Channel 0 //A/D converter module is enabled ADCON1 = 0b001110; //Voltage Reference //A/D Port Configuration Control bits - pin AN0 ADCON2 = 0b10000101; //A/D Result Format Select bit //000=0TAD //110=FOSC/64, 010=FOSC/32, 101=FOSC/16 T0CON=0b10010011; //prescaler 011 - 1:16 Fosc/4=2000, 2000/16=125kHz TMR0=TMR0_LOAD; //preload TMR0IE=1; //timer overflow intterrupt enable PEIE=1; //peripheral interrupt enable GIE=1; //global interrupts enable led_init(); sbuff[0]=0xff; //light all segments sbuff[1]=0xff; sbuff[2]=0xff; delay_ms(1400); led_clear(); for(;;) { GODONE=1; while(GODONE); volt=ADRES*10/VOLT_CONT; if(volt>999) volt=999; sprintf(strBuff,"%2u%1u",volt/10,volt%10); led_print(0,strBuff); led_dot(1,1); delay_ms(200); } }
/** * Initializes the DBGU and ISO7816 driver, and starts some tests. * \return Unused (ANSI-C compatibility) */ extern int main( void ) { uint8_t Atr[MAX_ATR_SIZE] ; uint8_t size ; /* Disable watchdog */ wdt_disable(); /* Disable all PIO interrupts */ pio_reset_all_it(); /* Configure console */ board_cfg_console(); console_clear_screen(); console_reset_cursor(); /* Configure PIT. Must be always ON, used for delay */ printf("Configure PIT \n\r"); timer_configure(BLINK_PERIOD); printf( "-- USART ISO7816 Example %s --\n\r", SOFTPACK_VERSION ) ; printf( "-- %s\n\r", BOARD_NAME ) ; printf( "-- Compiled: %s %s --\n\r", __DATE__, __TIME__ ) ; #ifdef CONFIG_HAVE_PMIC_ACT8945A pio_configure(act8945a_pins, ARRAY_SIZE(act8945a_pins)); if (act8945a_configure(&act8945a, &act8945a_twid)) { act8945a_set_regulator_voltage(&act8945a, 6, 2500); act8945a_enable_regulator(&act8945a, 6, true); } else { printf("--E-- Error initializing ACT8945A PMIC\n\r"); } #endif /* PIO configuration for LEDs */ printf("Configure LED PIOs.\n\r"); _configure_leds(); led_set(LED_BLUE); timer_wait(500); led_clear(LED_BLUE); led_status[LED_BLUE] = 1; /* PIO configuration for Button, use to simulate card detection. */ printf("Configure buttons with debouncing.\n\r"); _configure_buttons(); /* Configure Pios usart*/ pio_configure(&pins_com2[0], ARRAY_SIZE(pins_com2)); /* Init ISO7816 interface */ iso7816_init(&iso7816_desc, &iso7816_opt); /* Warm reset */ iso7816_warm_reset(&iso7816_desc); /* Read ATR */ memset( Atr, 0, sizeof(Atr) ) ; iso7816_get_data_block_ATR(&iso7816_desc, Atr, &size ); /* Decode ATR */ iso7816_decode_ATR(Atr); /* Allow user to send some commands */ _send_receive_commands(&iso7816_desc); printf("\n\r Exit App \n\r"); while (1) ; }
void ui_state_game(void) { int i, x, y, go_x, go_y, result; int aimline_x1 = SCREEN_WIDTH - UI_TILE_WIDTH / 2, aimline_y1 = UI_Y_OFFSET + UI_TILE_HEIGHT / 2, aimline_x2, aimline_y2, strengthbar_width; int play_explosion = 0; int tank_direction, tank_strength, meter_direction; double radians; game_position_t result_pos; image_t *tile; led_clear(); for (i = 0; i < game_soldier_score; i++) led_set(7 - i, 1); for (i = 0; i < game_tank_score; i++) led_set(i, 1); if (game_soldier_score >= UI_MAX_POINTS || game_tank_score >= UI_MAX_POINTS) { ui_state = UI_SCOREBOARD; return; } game_reset(); while (1) { screen_clear(color_black); for (x = 0; x < GAME_WIDTH; x++) for (y = 0; y < GAME_HEIGHT; y++) { tile = game_is_scorched[y][x] ? img_dirt : img_grass; image_draw(x * UI_TILE_WIDTH, y * UI_TILE_HEIGHT + UI_Y_OFFSET, tile); } go_x = game_player.x; go_y = game_player.y; switch (ui_player_direction) { case GAME_NORTH: go_y--; break; case GAME_SOUTH: go_y++; break; case GAME_WEST: go_x--; break; case GAME_EAST: go_x++; break; } switch (ui_state) { case UI_GAME_SOLDIER: if (go_x >= 0 && go_x < GAME_WIDTH && go_y >= 0 && go_y < GAME_HEIGHT) { screen_rectangle( go_x * UI_TILE_WIDTH, go_y * UI_TILE_HEIGHT + UI_Y_OFFSET, UI_TILE_WIDTH, UI_TILE_HEIGHT, color_blue); } if (ui_play_point_sound) snd_play("sounds/point.raw"); else if (play_explosion) snd_play("sounds/eksplosjon.raw"); ui_play_point_sound = 0; play_explosion = 0; if (btn_is_pushed(7)) { result = game_move_player(ui_player_direction); if (result == GAME_MOVE_TANK) { ui_play_point_sound = 1; return; } if (result == GAME_MOVE_OK) { tank_direction = GAME_MIN_DIRECTION; meter_direction = UI_DIRECTION_SPEED; ui_state = UI_GAME_DIRECTION; } } if (btn_is_pushed(6)) { ui_player_direction = ((int)ui_player_direction + 1) % 4; } break; case UI_GAME_DIRECTION: if (btn_is_pushed(0)) { tank_strength = GAME_MIN_STRENGTH; meter_direction = UI_STRENGTH_SPEED; ui_state = UI_GAME_STRENGTH; break; } radians = tank_direction * M_PI / 180; aimline_x2 = aimline_x1 - (int)(cos(radians) * UI_TILE_WIDTH * 3); aimline_y2 = aimline_y1 + (int)(sin(radians) * UI_TILE_HEIGHT * 3); screen_line(aimline_x1, aimline_y1, aimline_x2, aimline_y2, color_red); if (tank_direction >= GAME_MAX_DIRECTION) meter_direction = -UI_DIRECTION_SPEED; if (tank_direction <= GAME_MIN_DIRECTION) meter_direction = UI_DIRECTION_SPEED; tank_direction += meter_direction; tank_direction = MAX(GAME_MIN_DIRECTION, tank_direction); tank_direction = MIN(GAME_MAX_DIRECTION, tank_direction); break; case UI_GAME_STRENGTH: if (btn_is_pushed(0)) { snd_play_wait("sounds/skyte.raw"); result_pos = game_shoot_bullet(tank_direction, tank_strength); ui_state = UI_GAME_SOLDIER; play_explosion = !game_position_equals(result_pos, GAME_SHOT_OOB); if (result_pos.x < 0 && result_pos.y < 0) { ui_play_point_sound = 1; return; } break; } screen_line(aimline_x1, aimline_y1, aimline_x2, aimline_y2, color_red); strengthbar_width = tank_strength * (SCREEN_WIDTH - UI_TILE_WIDTH) / GAME_MAX_STRENGTH; screen_fill_rectangle( SCREEN_WIDTH - UI_TILE_WIDTH / 2 - strengthbar_width, UI_TILE_HEIGHT / 2, strengthbar_width, UI_TILE_HEIGHT, color_red); if (tank_strength >= GAME_MAX_STRENGTH) meter_direction = -UI_STRENGTH_SPEED; if (tank_strength <= GAME_MIN_STRENGTH) meter_direction = UI_STRENGTH_SPEED; tank_strength += meter_direction; tank_strength = MAX(GAME_MIN_STRENGTH, tank_strength); tank_strength = MIN(GAME_MAX_STRENGTH, tank_strength); break; } image_draw( (GAME_WIDTH - 2) * UI_TILE_WIDTH, UI_Y_OFFSET, img_tank); image_draw( game_player.x * UI_TILE_WIDTH, game_player.y * UI_TILE_HEIGHT + UI_Y_OFFSET, img_soldier); screen_show_buffer(); } }
/** * This reads all sensor data from the ADIS sesnor via burst read mode */ void adis_burst_read(void * pvParameters) { unsigned char addr[2]; // Temporary variable portTickType xLastWakeTime; // Variable for the vTaskDelayUntil() xLastWakeTime = xTaskGetTickCount(); // Start the counting for the ticktimer while(1){ if (sample_output_type > ADIS_STOP) { // Do not sample if (sample_output_type >= ADIS_SILENT) { // Do the sampling led_set(2); addr[0] = 0x3e; addr[1] = 0x00; spi0_send(addr,1,ADIS_CS); //Initiate burst read mode vTaskDelay(.01/portTICK_RATE_MS); spi0_send(SUPPLY_OUT,1,ADIS_CS); //Power supply measurement vTaskDelay(.01/portTICK_RATE_MS); spi0_send(XGYRO_OUT,1,ADIS_CS); //X-axis gyroscope output vTaskDelay(.01/portTICK_RATE_MS); spi0_send(YGYRO_OUT,1,ADIS_CS); //Y-axis gyroscope output vTaskDelay(.01/portTICK_RATE_MS); spi0_send(ZGYRO_OUT,1,ADIS_CS); //Z-axis gyroscope output vTaskDelay(.01/portTICK_RATE_MS); spi0_send(XACCL_OUT,1,ADIS_CS); //X-axis accelerometer output vTaskDelay(.01/portTICK_RATE_MS); spi0_send(YACCL_OUT,1,ADIS_CS); //Y-axis accelerometer output vTaskDelay(.01/portTICK_RATE_MS); spi0_send(ZACCL_OUT,1,ADIS_CS); //Z-axis accelerometer output vTaskDelay(.01/portTICK_RATE_MS); spi0_send(XMAGN_OUT,1,ADIS_CS); //X-axis magnetometer measurement vTaskDelay(.01/portTICK_RATE_MS); spi0_send(YMAGN_OUT,1,ADIS_CS); //Y-axis magnetometer measurement vTaskDelay(.01/portTICK_RATE_MS); spi0_send(ZMAGN_OUT,1,ADIS_CS); //Z-axis magnetometer measurement vTaskDelay(.01/portTICK_RATE_MS); spi0_send(TEMP_OUT,1,ADIS_CS); //Temperature output vTaskDelay(.01/portTICK_RATE_MS); spi0_send(AUX_ADC,1,ADIS_CS); //Auxiliary ADC measurement } if (sample_output_type == ADIS_CSV) { // Output CSV format // Supply voltage decoding unsigned int voltage; voltage = (unsigned int)((SUPPLY_OUT[0] << 8) | SUPPLY_OUT[1]) & 0x3fff; voltage = voltage * 242; printf("%d,",voltage); //======================GYROS======================= //xgyro printf("%d,",adis_decode_14bit_raw(XGYRO_OUT,50)); // Print the recieved data //ygyro printf("%d,",adis_decode_14bit_raw(YGYRO_OUT,50)); // Print the recieved data //zgyro printf("%d,",adis_decode_14bit_raw(ZGYRO_OUT,50)); // Print the recieved data //======================ACCLEROMETERS======================= //xaccl printf("%d,",adis_decode_14bit_raw(XACCL_OUT,3330)); // Print the recieved data //yaccl printf("%d,",adis_decode_14bit_raw(YACCL_OUT,3330)); // Print the recieved data //zaccl printf("%d,",adis_decode_14bit_raw(ZACCL_OUT,3330)); // Print the recieved data //======================MAGNETOMETERS======================= //xmag printf("%d,",adis_decode_14bit_raw(XMAGN_OUT,500)); // Print the recieved data //ymag printf("%d,",adis_decode_14bit_raw(YMAGN_OUT,500)); // Print the recieved data //zmag printf("%d,",adis_decode_14bit_raw(ZMAGN_OUT,500)); // Print the recieved data // Temperature @TODO This is not tested yet printf("%d,",(adis_decode_12bit_raw(TEMP_OUT,140)+25000)); // Print the recieved data // ADC @TODO This is not tested or verified yet printf("%d\n\r",adis_decode_12bit_raw(AUX_ADC,810)); // Print the recieved data } if (sample_output_type == ADIS_BIN) { // Output binary format printf("%c%c", SUPPLY_OUT[0], SUPPLY_OUT[1]); //Power supply measurement printf("%c%c", XGYRO_OUT[0], XGYRO_OUT[1]); //X-axis gyroscope output printf("%c%c", YGYRO_OUT[0], YGYRO_OUT[1]); //Y-axis gyroscope output printf("%c%c", ZGYRO_OUT[0], ZGYRO_OUT[1]); //Z-axis gyroscope output printf("%c%c", XACCL_OUT[0], XACCL_OUT[1]); //X-axis accelerometer output printf("%c%c", YACCL_OUT[0], YACCL_OUT[1]); //Y-axis accelerometer output printf("%c%c", ZACCL_OUT[0], ZACCL_OUT[1]); //Z-axis accelerometer output printf("%c%c", XMAGN_OUT[0], XMAGN_OUT[1]); //X-axis magnetometer measurement printf("%c%c", YMAGN_OUT[0], YMAGN_OUT[1]); //Y-axis magnetometer measurement printf("%c%c", ZMAGN_OUT[0], ZMAGN_OUT[1]); //Z-axis magnetometer measurement printf("%c%c", TEMP_OUT[0], TEMP_OUT[1]); //Temperature output printf("%c%c", AUX_ADC[0], AUX_ADC[1]); //Auxiliary ADC measurement printf("\n\r"); // Newline and return } led_clear(2); } //------Here a datacollecter is implemented--- dc_collector[dc_counter] = 9.82*((float)(adis_decode_14bit_raw(XACCL_OUT,3330))/1000000); // Collect 10 values // printf("accx=\t%d\n\r",(int)(dc_collector[dc_counter]*1000)); // Prints in mili g dc_counter++; if (dc_counter == 10){ dc_collector_avg = 0; for( sum_counter = 0 ; sum_counter < 10 ; sum_counter++){ dc_collector_avg = dc_collector_avg + dc_collector[sum_counter]; // Sum up the last 10 values } dc_collector_avg = dc_collector_avg/10; // Calculate average of 10 collected values printf("avg\t%d mm/s^2\t",(int)(dc_collector_avg*1000)); // Integrate twice dc_collector_speed = dc_collector_speed + (dc_collector_avg*0.5); printf("Speed=\t%d mm/s\t",(int)(dc_collector_speed*1000)); dc_collector_distance = dc_collector_distance + (dc_collector_speed*0.5); printf("Distance:\t%d mm\n\r",(int)(dc_collector_distance*1000)); dc_counter = 0; } //------Here a datacollecter is implemented (end)--- vTaskDelayUntil( &xLastWakeTime, 50/portTICK_RATE_MS); } }
int main(void) { // uint8_t rgb[3]; led_init(); button_init(); commstack_init(COMM_DEV, COMM_BAUDRATE); // uartbridge_send1("Hello UART 1\n", 13); // uartbridge_send2("Hello UART 2\n", 13); pwm_init(); // SysTick_Config(48000 * 40); // set systick interrupt to be triggered every 40ms // SystemCoreClockUpdate(); while (1) { // while (USART_GetFlagStatus(UART_2_DEV, USART_FLAG_TXE) == RESET); // USART_SendData(UART_2_DEV, 'X'); if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) { led_set(); ++pushed; } else { pushed = 0; led_clear(); } if (pushed >= PUSH_LIMIT) { pushed = 0; state = ((state + 1) == STATE_END) ? 0 : state + 1; led_blink(); } // switch (state) { // case STATE_R: // rgb[0] = 150; // rgb[1] = 0; // rgb[2] = 0; // pwm_set(rgb); // break; // case STATE_G: // rgb[0] = 0; // rgb[1] = 155; // rgb[2] = 0; // pwm_set(rgb); // break; // case STATE_B: // rgb[0] = 0; // rgb[1] = 0; // rgb[2] = 155; // pwm_set(rgb); // break; // case STATE_RG: // rgb[0] = 155; // rgb[1] = 155; // rgb[2] = 0; // pwm_set(rgb); // break; // case STATE_GB: // rgb[0] = 0; // rgb[1] = 155; // rgb[2] = 155; // pwm_set(rgb); // break; // case STATE_BR: // rgb[0] = 155; // rgb[1] = 0; // rgb[2] = 155; // pwm_set(rgb); // break; // case STATE_SLIDE: // break; // default: // rgb[0] = 0; // rgb[1] = 0; // rgb[2] = 0; // pwm_set(rgb); // break; // } } return 0; }
void led_blink_clear(u8 mask, u16 *base) { led_clear(mask, base); }
void flash(int led) { led_set(led); delay(DELAY); led_clear(led); delay(DELAY); }
void process_bytes(){ uint16_t nb_received = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); uint8_t c; for( uint16_t i = 0; i < nb_received ; i++ ){ c = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); switch (c){ case 'l': CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); for (uint8_t x = 0; x < 255; x++){ led_set(255,20,x); Delay_MS(10); } led_clear(); println("LED test finished"); break; #ifdef ONBOARD_STUFF case 'm':{ switch (motor_get()){ case 0: // TODO use ranges instead of exact values println("Motor ON (PWM @255)"); motor_set(255); break; case 255: println("Motor ON (PWM @100)"); motor_set(100); break; case 100: println("Motor OFF"); motor_set(0); break; } }break; case 'd':{ fprintf(&USBSerialStream, "GP2Y PM: %.1f ug/m3\r\n", gp2y_get_pm() ); }break; case 'k': fprintf(&USBSerialStream, "humidity: %.0f %%\r\n", cc2dxx_get_humidity()); Delay_MS(100); fprintf(&USBSerialStream, "temp: %.2f deg C\r\n", cc2dxx_get_temperature()); break; #endif #ifdef SENSORBOARD case 'i': fprintf(&USBSerialStream, "TCOV: %d\r\n", iaqengine_get_ppm()); break; #endif #ifdef WIFI case 'w': wifi_print_MAC(); wifi_print_ip(); break; case 'y': wifi_config_set_dev(); wifi_connect_to_ap(wifi_ap_ssid, wifi_ap_password, wifi_ap_encrypt_mode); wifi_print_ip(); wifi_ping(192,168,1,1); break; case 's': wifi_resolve_backend_ip(); break; /*case 'e': fprintf(&USBSerialStream, "is set? %u\r\n", wifi_config_is_set()); fprintf(&USBSerialStream, "set dev %u\r\n", wifi_config_set_dev()); fprintf(&USBSerialStream, "is set? %u\r\n", wifi_config_is_set()); fprintf(&USBSerialStream, "save %u\r\n", wifi_config_save()); fprintf(&USBSerialStream, "%s %s %u\r\n", wifi_ap_ssid, wifi_ap_password, wifi_ap_encrypt_mode); fprintf(&USBSerialStream, "set lol %u\r\n", wifi_config_set("lol", "lolilol", 2)); fprintf(&USBSerialStream, "%s %s %u\r\n", wifi_ap_ssid, wifi_ap_password, wifi_ap_encrypt_mode); fprintf(&USBSerialStream, "load %u\r\n", wifi_config_load()); fprintf(&USBSerialStream, "%s %s %u\r\n", wifi_ap_ssid, wifi_ap_password, wifi_ap_encrypt_mode); wifi_config_mem_clear(); fprintf(&USBSerialStream, "clear then load %u\r\n", wifi_config_load()); break;*/ case 'p':{ println("Trying to POST"); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); wifi_config_set_dev(); /* // There is a problem with that, it hangs sometimes >< if (!wifi_config_is_set()){ if (!wifi_config_load()){ #ifdef DEV printf("setting up DEV config"); wifi_config_set_dev(); #else printf("No config anywhere!"); return; // TODO what do we do if it's not configured? #endif } }*/ fprintf(&USBSerialStream, "%s %s %u\r\n", wifi_ap_ssid, wifi_ap_password, wifi_ap_encrypt_mode); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); // TODO check if the SSID is available before trying to connect or it might hang... :/ if( !wifi_connect_to_ap(wifi_ap_ssid, wifi_ap_password, wifi_ap_encrypt_mode) == WERR_OK){ println("AP NOK"); } else { println("AP OK"); wifi_print_ip(); uint32_t backend_ip; uint16_t backend_port = 80; //backend_ip = cc3000_IP2U32(54,221,218,154); //backend_ip = cc3000_IP2U32(67,215,65,132); //backend_ip = cc3000_IP2U32(192,168,1,11); //backend_ip = cc3000_IP2U32(192,168,42,102); backend_ip = wifi_resolve_backend_ip(); print_ip_helper("backend", backend_ip); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); if (cc3000_ping(backend_ip, 3, 500, 32)){ fprintf(&USBSerialStream, "ping OK\r\n"); } else { fprintf(&USBSerialStream, "ping timeout\r\n"); } CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); void* sock = cc3000_connectTCP(backend_ip, backend_port); if (cc3000_cli_connected(sock)){ println("cli connected"); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); uint16_t res = 0; res += cc3000_cli_fastrprint(sock, "POST /v1/airboxlab/ HTTP/1.1\n"); res += cc3000_cli_fastrprint(sock, "User-Agent: abl1.0\n"); res += cc3000_cli_fastrprint(sock, "Host: api.airboxlab.com\n"); res += cc3000_cli_fastrprint(sock, "Accept: */*\n"); //res += cc3000_cli_fastrprint(sock, "Content-Type: application/json\n"); res += cc3000_cli_fastrprint(sock, "Content-Length: 35\n\n"); res += cc3000_cli_fastrprint(sock, "5040f3bd20c8,20.7,53.9,1255,11.1,78\n"); fprintf(&USBSerialStream, "written: %u / %u\r\n", res, 171); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); Delay_MS(1000); uint8_t nb_available = cc3000_cli_available(sock); fprintf(&USBSerialStream, "Returned %u\r\n", nb_available); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); for (int i = 0; i<cc3000_cli_available(sock); i++){ fprintf(&USBSerialStream, "%c" ,cc3000_cli_read(sock)); } println(""); cc3000_cli_close(sock); println("closed"); } else { println("cli not connected"); } } }break;/* case 'W':{ // setup wifi int nb_returned; println("SSID?"); println("password?"); println("encryption mode number?[0:none, 1:WEP, 2:WPA, 3:WPA2]"); uint16_t _mode; CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); while(1){ // TODO timeout? opt-out? _mode = fgetc(&USBSerialStream); printf("read: %i", _mode); if(_mode <= 3){ wifi_ap_encrypt_mode = _mode; fprintf(&USBSerialStream, "Mode = %u\r\n", _mode); break; } else { fprintf(&USBSerialStream, "Not a valid mode. Please choose number between [0:none, 1:WEP, 2:WPA, 3:WPA2]\r\n"); CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } } }break;*/ #endif default: println("Airboxlab serial mode: press 'l' key to test LEDs"); break; } } }
int main ( void ) { char* line; time_t* led_flash_timer; time_t* reboot_timer; socket_t* socket = socket0_g; int reboot_stage = 0; /* Turn off all LEDs as a starting point */ led_clear(); /* this must be first, because all the other init functions call malloc */ init_malloc(); /* Initialize current time and some timers */ init_current_time(); led_flash_timer = init_timer(); set_timer(led_flash_timer, 500); reboot_timer = init_timer(); /* receive packet buffer */ rx_packet_g = malloc(sizeof(packet_t)); /* socket init */ socket0_g = init_socket(0); socket1_g = init_socket(1); /* open ethernet port and wait for connection requests keep trying forever */ while (!open_link()); /* infinite loop. Everything is timer, interrupt and queue driven from here on down */ while (1) { /* Flash a heartbeat LED */ if (timer_expired(led_flash_timer)) { led_flip(0); set_timer(led_flash_timer, 500); } /* Check for newly downloaded tftp file. Add to all tx buffers */ /* Has a file been uploaded via tftp ? */ if (udp_file_g != NULL) { /* Notify telnet clients that file has been received */ if (udp_file_g->ready) { udp_file_g->ready = 0; telnet_broadcast("Received file %s, %d bytes, linux %d\r\n", udp_file_g->filename, udp_file_g->total_bytes, udp_file_g->linux_boot); if (process_file(socket0_g) == 0) /* Disconnect in 1 second */ set_timer(reboot_timer, 1000); else telnet_broadcast("Not an elf file\r\n"); } } /* reboot timer expired */ if (timer_expired(reboot_timer)) { /* First stage of reboot sequence is to nicely disconnect */ if (reboot_stage == 0) { set_timer(reboot_timer, 1000); reboot_stage = 1; socket0_g->tcp_disconnect = 1; socket1_g->tcp_disconnect = 1; } else { /* Second stage of reboot sequence is to turn off ethmac and then jump to restart vector */ close_link(); reboot(); } } /* Poll both sockets in turn for activity */ if (socket == socket0_g) socket = socket1_g; else socket = socket0_g; /* Check if any tcp packets need to be re-transmitted */ tcp_retransmit(socket); /* Handle exit command */ if (socket->tcp_disconnect && socket->tcp_connection_state == TCP_OPEN) { tcp_disconnect(socket); } /* Reset connection */ if (socket->tcp_reset) { socket->tcp_connection_state = TCP_CLOSED; socket->telnet_connection_state = TELNET_CLOSED; socket->telnet_options_sent = 0; tcp_reply(socket, NULL, 0); socket->tcp_reset = 0; } /* Send telnet options */ if (socket->tcp_connection_state == TCP_OPEN && !socket->telnet_options_sent){ telnet_options(socket); socket->telnet_options_sent = 1; } /* telnet connection open Communicate with client */ else if (socket->telnet_connection_state == TELNET_OPEN) { /* Send telnet greeting */ if (!socket->telnet_sent_opening_message){ put_line (socket->telnet_txbuf, "Amber Processor Boot Loader\r\n> "); socket->telnet_sent_opening_message = 1; } /* Parse telnet rx buffer */ if (get_line(socket->telnet_rxbuf, &line)) parse_command (socket, line); /* Transmit text from telnet tx buffer */ telnet_tx(socket, socket->telnet_txbuf); } } }