void toggle_options(uint8_t value, uint8_t num) { blink(num, BLINK_SPEED/4); // indicate which option number this is uint8_t temp = options; options = value; save_state(); blink(32, 500/32); // "buzz" for a while to indicate the active toggle window // if the user didn't click, reset the value and return options = temp; save_state(); _delay_s(); }
int main(void) { basic_clock clock = basic_clock(DCO_F_1MHz); tlc5925 tlc = tlc5925(p1_4); pinOutput(LED_PIN); pinOutput(LED_PIN2); pinOn(LED_PIN2); tlc.write((uint16_t)TLC5925_CH01); _delay_s(2); tlc.write((uint16_t)TLC5925_CH0_15); _delay_s(5); for (;;) //ever { tlc.shiftDown(16,TLC5925_CH00); pinToggle(LED_PIN); pinToggle(LED_PIN2); _delay_s(1); } }
int main(void) { basic_clock clock = basic_clock(); spi spi_test = spi(SPI_B0); pinOutput(LED_PIN); pinOutput(LED_PIN2); pinOn(LED_PIN2); for (;;) //ever { for (uint8_t i=0; i<255; i++) { spi_test.write(i); } pinToggle(LED_PIN); pinToggle(LED_PIN2); _delay_s(1); } }
int main(void) { DDRB |= (1 << PWM_PIN); // Set PWM pin to output, enable main channel TCCR0A = FAST; // Set timer to do PWM for correct output pin and set prescaler timing TCCR0B = 0x01; // Set timer to do PWM for correct output pin and set prescaler timing restore_state(); // Read config values and saved state count_modes(); // Enable the current mode group // check button press time, unless we're in group selection mode if (mode_idx != GROUP_SELECT_MODE) { if ( we_did_a_fast_press() ) { // sram hasn't decayed yet, must have been a short press increment_fast_presses(); next_mode(); // Will handle wrap arounds } else { // Long press, keep the same mode reset_fast_presses(); if( !memory_is_enabled() ) {mode_idx = 0;} // if memory is turned off, set mode_idx to 0 } } save_mode(); // Turn features on or off as needed #ifdef VOLTAGE_MON ADC_on(); #else ADC_off(); #endif uint8_t output; uint8_t i = 0; uint16_t ticks = 0; #ifdef TURBO_RAMP_DOWN uint8_t adj_output = 255; #endif #ifdef VOLTAGE_MON uint8_t lowbatt_cnt = 0; uint8_t voltage; ADCSRA |= (1 << ADSC); // Make sure voltage reading is running for later #endif if(mode_idx > num_modes) { output = mode_idx; } // special modes, override output else { output = modes[mode_idx]; } while(1) { if (fast_presses[0] >= 12) { // Config mode if 12 or more fast presses _delay_s(); // wait for user to stop fast-pressing button reset_fast_presses(); // exit this mode after one use toggle_mode(GROUP_SELECT_MODE, 1); // Enter the mode group selection mode? toggle_options((options ^ 0b00010000), 2); // memory toggle_options((options ^ 0b00100000), 3); // hidden blinkies toggle_options((options ^ 0b01000000), 4); // hidden battcheck toggle_options((options ^ 0b10000000), 5); // turbo timer toggle_options(DEFAULTS, 6); // reset to defaults } else if (output == STROBE) { // 10Hz tactical strobe for(i=0;i<8;i++) { set_level(RAMP_SIZE); _delay_ms(33); set_output(0); _delay_ms(67); } } else if (output == BEACON) { set_level(RAMP_SIZE); _delay_ms(10); set_output(0); _delay_s(); _delay_s(); } else if (output == SOS) { // dot = 1 unit, dash = 3 units, space betwen parts of a letter = 1 unit, space between letters = 3 units #define SOS_SPEED 200 // these speeds aren't perfect, but they'll work for the [never] times they'll actually get used blink(3, SOS_SPEED); // 200 on, 400 off, 200 on, 400 off, 200 on, 400 off _delay_ms(SOS_SPEED); // wait for 200 blink(3, SOS_SPEED*5/2); // 500 on, 1000 off, 500 on, 1000 off, 500 on, 1000 off (techically too long on that last off beat, but oh well) blink(3, SOS_SPEED); // 200 on, 400 off, 200 on, 400 off, 200 on, 400 off _delay_s(); _delay_s(); } else if (output == BATT_CHECK) { blink(battcheck(), BLINK_SPEED/4); _delay_s(); _delay_s(); } else if (output == GROUP_SELECT_MODE) { mode_idx = 0; // exit this mode after one use for(i=0; i<NUM_MODEGROUPS; i++) { toggle_options(((options & 0b11110000) | i), i+1); } _delay_s(); } else { if ((output == TURBO) && ( ttimer_is_enabled() ) && (ticks > (TURBO_MINUTES * TICKS_PER_MINUTE))) { #ifdef TURBO_RAMP_DOWN if (adj_output > TURBO_LOWER) { adj_output = adj_output - 2; } set_output(adj_output); #else set_output(TURBO_LOWER); #endif } else { ticks ++; // count ticks for turbo timer set_level(output); } _delay_ms(500); // Otherwise, just sleep. } reset_fast_presses(); #ifdef VOLTAGE_MON if (ADCSRA & (1 << ADIF)) { // if a voltage reading is ready voltage = ADCH; // get the waiting value if (voltage < ADC_LOW) { // See if voltage is lower than what we were looking for lowbatt_cnt ++; } else { lowbatt_cnt = 0; } if (lowbatt_cnt >= 8) { // See if it's been low for a while, and maybe step down //set_output(0); _delay_ms(100); // blink on step-down: if (output > RAMP_SIZE) { // blinky modes output = RAMP_SIZE / 2; // step down from blinky modes to medium } else if (output > 1) { // regular solid mode output = output - 1; // step down from solid modes somewhat gradually } else { // Already at the lowest mode set_output(0); // Turn off the light set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Power down as many components as possible sleep_mode(); } set_level(output); lowbatt_cnt = 0; _delay_s(); // Wait before lowering the level again } ADCSRA |= (1 << ADSC); // Make sure conversion is running for next time through } #endif // ifdef VOLTAGE_MON } }
void main(void) { set_up(); buttonsPressed = 0; while(1){ exec_counter=0; //to select menu //store only when any value has changed if(calibrated_adc==1||calibrated_F==1||calibrated_steady==1){ char storage[]={AdcThreshold, dcval, steady_crest, fourier_offset, FThreshold}; eeprom_write_bytes(storage,5);//check for FThreshold>255 } tempF=255; temp=0; //to refresh the thresholds only when switches are pressed calibrated_adc=0; calibrated_F=0; calibrated_steady=0; if (buttonsPressed & BUTTON_S2){ if(menu==0) { menu=option+1; option=0; } else exec_counter=1; disp_counter=1; buttonsPressed = 0; } //to scroll options if (buttonsPressed & BUTTON_S1){ if(option<(total_options[menu]-1)){ option++; } else option=0; disp_counter=1; buttonsPressed = 0; } if(disp_counter==1){ disp_counter=0;//to prevent writing if no change GrClearDisplay(&g_sContext); switch(menu){ case 0: print_string(0,0,"=====Menu===="); print_string(0,8,"1.Calibrate"); print_string(0,16,"2.View Thresholds"); print_string(0,24,"3.Enable Eye "); print_string(0,32,"4.View Signal"); switch(option){ case 1: highlight_and_print_string(0,16,"2.View Thresholds"); break; case 2: highlight_and_print_string(0,24,"3.Enable Eye "); break; case 3: highlight_and_print_string(0,32,"4.View Signal"); break; default: highlight_and_print_string(0,8,"1.Calibrate"); option=0; break; } break; case 1: print_string(0,0,"===Calibrate==="); print_string(0,8,"1.Steady Values"); print_string(0,16,"2.Peak"); print_string(0,24,"3.Fourier "); print_string(0,32,"4.Back"); tempF=255; temp=0; //to refresh the thresholds only when switches are pressed calibrated_adc=0; calibrated_F=0; calibrated_steady=0; switch(option){ case 1: highlight_and_print_string(0,16,"2.Peak"); if(exec_counter==1){ exec_counter=0; lcd_clear(); //to calibrate small blink adc Thresholds;cannot view adc values till out while(!(buttonsPressed & BUTTON_S2)){ if(calibrated_adc==0) { AdcThreshold=255; lcd_clear(); print_string(0,0,"Blink to calib"); print_int(50, 30, AdcThreshold); print_string(0,56,"*S2=ESC*"); _delay_ms(200); } temp = adc_read(); _delay_ms(20); crest=0; // to check crest while(temp<adc_read()){ temp = adc_read(); _delay_ms(20); crest=1; } if(crest && temp>steady_crest && temp<AdcThreshold){ AdcThreshold=temp; } calibrated_adc=1; lcd_clear(); // print_string(0,0,"Blink to calibrate"); //print_string(56,0,"*S2=ESC*"); print_string(0,0,"Blink to calib"); print_int(50, 30, AdcThreshold); print_string(0,56,"*S2=ESC*"); _delay_ms(200); } if(AdcThreshold==255) AdcThreshold=steady_crest;//if 50 hz noise lcd_clear(); buttonsPressed=0; disp_counter=1; } break; case 2: highlight_and_print_string(0,24,"3.Fourier"); if(exec_counter==1){ exec_counter=0; //calibrate FThreshold while(!(buttonsPressed & BUTTON_S2)){ if(calibrated_F==0){ //write anything that you want to execute only once in the loop lcd_clear(); } X=dft(); if(X>fourier_offset+10 && tempF>X){ //+10 is for lesser sensitivity. If offset is more use % of fourier_offset FThreshold=X; tempF=X; } calibrated_F=1; lcd_clear(); print_string(0,0,"Blink to calibrate"); print_string(0,56,"*S2=ESC*"); print_int(50,30,(int)X); _delay_ms(200); } lcd_clear(); buttonsPressed=0; disp_counter=1; } break; case 3: highlight_and_print_string(0,32,"4.Back"); if(exec_counter==1){ exec_counter=0; menu=0; option=0; disp_counter=1; } break; default: highlight_and_print_string(0,8,"1.Steady Values"); option=0; if(exec_counter==1){ exec_counter=0; //initialize before setting dcval=0; fourier_offset=0; steady_trough=255,steady_crest=0; lcd_clear(); print_string(0,0,"Focus on the dot"); print_string(32,16,"for 3s "); print_string(40,30,"(.)"); _delay_s(3); //compute fourier_offset for(i=0;i<10;i++){ //will check for 10*100 ms check_fourier_offset(); } //compute dc_val for(i=0;i<2000;i++){ //average 2000 samples collected over 2 sec input=adc_read(); dcval+=(float)input/2000; if(steady_crest<input) steady_crest=input; if(steady_trough>input) steady_trough=input; _delay_ms(1); } steady_crest=steady_crest+(steady_crest-dcval)*0; //0% more than the steady peak steady_trough=steady_trough-(dcval-steady_trough)*0; lcd_clear(); print_string(0,0,"Done Calibrating!"); print_string(0,16,"Fourier Off:"); print_int(80,16,fourier_offset); print_string(0,32,"DC:"); print_int(80,32,dcval); print_string(0,40,"Steady Crest:"); print_int(80,40,steady_crest); print_string(0,56,"*S2=ESC*"); _delay_s(3); while(!(buttonsPressed & BUTTON_S2)); lcd_clear(); calibrated_steady=1; //to display when it goes back buttonsPressed=0; disp_counter=1; } break; } break; case 2: print_string(0,0,"===Thresholds==="); print_string(0,8,"1.DC........."); print_int(80,8,dcval); print_string(0,16,"2.Steady Pk.."); print_int(80,16,steady_crest); print_string(0,24,"3.Peak......."); print_int(80,24,AdcThreshold); print_string(0,32,"4.Fourier...."); print_int(80,32,FThreshold); print_string(0,56,"*S2=ESC*"); while(!(buttonsPressed & BUTTON_S2)); //to check slope buttonsPressed=0; while(!(buttonsPressed & BUTTON_S2)){ X=dft(); print_int(48,24,max_input); print_int(48,32,max_slope); _delay_ms(200); lcd_clear() } buttonsPressed=0; menu=0; option=0; disp_counter=1; break; case 3: print_string(0,0,"===Eye Control==="); print_string(0,8,"1.Wait "); print_string(0,16,"2.Left "); print_string(0,24,"3.Right "); print_string(0,32,"4.Forward "); print_string(0,40,"5.Backward "); print_string(0,56,"*S2=ESC*"); eye_control(); menu=0; option=0; disp_counter=1; break; //debug signal on lcd case 4: print_string(0,0,"S1 pause/resume"); print_string(0,56,"S2 Back"); plot(); menu=0; option=0; disp_counter=1; break; default:break; } } }