void scene_arrive(){ printf("\n\tRegister Arrived Bus\n"); printf("\n\t1. Number : %s\n", temp->num); printf("\t2. Type : %s\n", temp->type); printf("\t3. Capacity: %u\n", temp->capacity); printf("\n\t4. Confirm\n"); printf("\t5. Cancel\n"); printf("\n\tChoice: "); if (!getint(&number)){ msg("Invalid Action!"); return; } switch (number){ case 1: printf("\n\tNumber: "); buffer = getpstr(); for (number = 0; number < strlen(buffer); number++) buffer[number] = toupper(buffer[number]); if (!bus_validate(buffer) || search_bus(buffer)){ msg(bus_validate(buffer) ? "Bus already in queue!":"Invalid Number!"); free(buffer); return; } temp->num = buffer; return; case 2: printf("\n\t1. %s\n", bustype[0]); printf("\t2. %s\n", bustype[1]); printf("\n\tWhich Type? "); fflush(stdout); if (!getint(&number)) msg("Invalid Type!") else if (number == 1 || number == 2) temp->type = bustype[number - 1]; else msg("Invalid Type!") return; case 3: printf("\n\tCapacity: "); if (!getint(&number) && number < 0){ msg("Invalid Input!"); return; } temp->capacity = number; return; case 4: if (busses.last)busses.last->next = temp; if (!busses.first) busses.first = temp; busses.last = temp; scene = &scene_menu; return; case 5: free(temp); scene = &scene_menu; return; default: msg("Invalid Action!"); return; } }
/* Generate random car plate, lazy to think already */ char * random_plate(){ srand(time(NULL)); char * plate = malloc(8); memset(plate, 0, 8); do { int r = rand() % 3, i; for (i = 0; i <= r; i++) plate[strlen(plate)] = 'A' + (rand() % 26); sprintf(plate, "%s%d", plate, rand() % 10000); } while (search_bus(plate)); return plate; }
void scene_search(){ printf("\n\tOkay, Google!\n"); printf("\tSearch: "); buffer = getpstr(); for (number = 0; number < strlen(buffer); number++) buffer[number] = toupper(buffer[number]); if (bus_validate(buffer) && (number = search_bus(buffer))){ printf("\n\tThe bus %s is the %d%s bus in queue\n\t", buffer, number, getoi(number)); pause(); } else if (!number){ msg("Error 404: Bus Not Found!"); } else msg("Invalid Keyword!"); free(buffer); }
int main( void ) { uint8_t i=0; int16_t decicelsius; uint8_t error; uint8_t delayCounter=0; hw_init(); uart_init((UART_BAUD_SELECT((BAUD),F_CPU))); memset(&sensor_fw, 0, sizeof(_sensor_data)); /* init 485 write */ DDRB |= 0b0000001; //1 = output, 0 = input //PORTB |= 0b00000001; //Enable pin 5 internal pullup PORTB &= 0b11111110; //Enable pin 5 internal pullup read //PORTB |= 0b00000001; //Enable pin 5 internal pullup 485 write led_g_on(); _delay_ms(1000); led_y_on(); _delay_ms(1000); led_r_on(); _delay_ms(1000); led_r_off(); _delay_ms(1000); led_y_off(); _delay_ms(1000); led_g_off(); #ifndef OW_ONE_BUS ow_set_bus(&PIND,&PORTD,&DDRD,PD6); #endif led_g_on(); search_bus(); led_g_off(); sei(); sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; for(;;) { // main loop switch (sensor_fw.fw_state) { case FW_STATE_SENSOR_SEARCH: led_g_on(); search_bus(); led_g_off(); uart_puts_P("FW_STATE_SENSOR_SEARCH? =0\n"); sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; break; case FW_STATE_SENSOR_START_MEAS: if ( sensor_fw.sensor_num == 0 ) { sensor_fw.fw_state = FW_STATE_SENSOR_SEARCH; uart_puts_P("error sensor num =0\n"); break; } if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { sensor_fw.fw_state = FW_STATE_SENSOR_DELAY_750ms; //reset_timeout(); } else { sensor_fw.fw_state = FW_STATE_SENSOR_SEARCH; uart_puts_P("error start mes faul =0\n"); } break; case FW_STATE_SENSOR_DELAY_750ms: //if (TCNT1 > 5400) //750 ms _delay_ms( DS18B20_TCONV_12BIT ); { sensor_fw.fw_state = FW_STATE_SENSOR_READ_I; i = 0; } break; case FW_STATE_SENSOR_READ_I: if ( DS18X20_read_decicelsius(&sensor_fw.sensors[i].id[0], &decicelsius ) == DS18X20_OK ) { sensor_fw.sensors[i].temp = decicelsius; uart_puts_P( "Sensor# " ); uart_put_int( (int)i + 1 ); uart_puts_P(" = "); uart_put_temp( decicelsius ); uart_puts_P( NEWLINESTR ); } else { //uart_puts_P( "CRC Error (lost connection?)" ); sensor_fw.fw_state = FW_STATE_SENSOR_SEARCH; uart_puts_P("error lost connection? =0\n"); break; } i++; if (i >= sensor_fw.sensor_num) { sensor_fw.fw_state = FW_STATE_SENSOR_DELAY_5s; delayCounter = 0; //reset_timeout(); } //uart_puts_P( NEWLINESTR ); break; case FW_STATE_SENSOR_DELAY_5s: //if (TCNT1 > (5 * TICKS_PER_SEC)) /*5s*/ _delay_ms( 1000 ); { if(delayCounter > 5) sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; delayCounter++; } break; case FW_STATE_READ_COMM: { //uart_puts_P( "Communication cmd rx\n" ); switch(sensor_fw.comm.rxbuff[2]) { case 0x01: sensor_fw.comm.txbuff[0] = 0x7E; sensor_fw.comm.txbuff[1] = 0x01; // dev_id sensor_fw.comm.txbuff[2] = 0x02; // len sensor_fw.comm.txbuff[3] = 0x00; // len sensor_fw.comm.txbuff[4] = 0xBB; // data sensor_fw.comm.txbuff[5] = 0xBB; // data sensor_fw.comm.txbuff[6] = 0x00; // crc sensor_fw.comm.txbuff[7] = 0x00; // crc uart_putData(sensor_fw.comm.txbuff,8); break; } sensor_fw.comm.rxlen = 0; sensor_fw.comm.valid_cmd = 0; sensor_fw.fw_state = FW_STATE_SENSOR_START_MEAS; } break; } if(sensor_fw.comm.valid_cmd) { sensor_fw.fw_state = FW_STATE_READ_COMM; } //_delay_ms(3000); } }