void controlpanel_encoder() { int ticks = 0; while (true) { char ch = controlpanel_promptChar("Encoders"); switch (ch) { case 'p': ticks = encoder_get(LEFT_ENCODER); printf_P(PSTR("L Ticks: %d "), ticks); ticks = encoder_get(RIGHT_ENCODER); printf_P(PSTR("R Ticks: %d\n"), ticks); break; case 'r': encoder_reset(LEFT_ENCODER); encoder_reset(RIGHT_ENCODER); break; case 's': motorcontrol_setEnabled(true); printf_P(PSTR("L RPS: %f, R RPS: %f\n"), motorcontrol_getRPS(MOTOR_LEFT), motorcontrol_getRPS(MOTOR_RIGHT)); break; case 'q': return; case '?': static const char msg[] PROGMEM = "Encoder menu:\n" " p - position (ticks)\n" " r - Reset counter\n" " s - RPS of wheels\n" " q - Back\n"; puts_P(msg); break; default: puts_P(unknown_str); break; } } }
int main(void) { setup_hw(); static uint8_t button_press_just_happened = 0; static uint8_t not_the_first_time_pressed = 0; #ifdef USE_PWM uint8_t lamp_state = 0; uint8_t OCR1B_tmp = 0; #endif while(1) { int8_t counts = encoder_get(ENC_COUNTS); int16_t velocity = encoder_get(ENC_VELOCITY); if( counts > 0 ) { #ifdef AUTO_COARSE_FINE if( velocity > COARSE_FINE_THRESHOLD ) { soft_uart_send(PSTR("+"),4); } else { soft_uart_send(PSTR("+"),1); } #else soft_uart_send(PSTR("+"),1); #endif #ifdef USE_PWM if(OCR1B < 255) { OCR1B++; } else { #ifdef SHOW_PWM_MAX LED_on; // signal that the maximum has been reached delay(200); LED_off; LED_idle; #endif } lamp_state = 1; #endif } if( counts < 0 ) { #ifdef AUTO_COARSE_FINE if( velocity < -COARSE_FINE_THRESHOLD ) { soft_uart_send(PSTR("-"),4); } else { soft_uart_send(PSTR("-"),1); } #else soft_uart_send(PSTR("-"),1); #endif #ifdef USE_PWM if(OCR1B > 0) { OCR1B--; } else { lamp_state = 0; } #endif } if( encoder_get(BUTTON_WAS_PRESSED) ) { soft_uart_send(PSTR("/"),1); button_press_just_happened = 1; #ifdef USE_PWM if( lamp_state == 1 ) { OCR1B_tmp = OCR1B; // save PWM value lamp_state = 0; // lamp is off OCR1B = 0; // turn PWM off } else { OCR1B = OCR1B_tmp; // restore PWM value lamp_state = 1; // lamp is on } #endif } if( encoder_get(BUTTON_WAS_RELEASED) ) { soft_uart_send(PSTR("\\"),1); not_the_first_time_pressed = 0; } if( encoder_get(BUTTON_STATE) && ( (button_press_just_happened == 1) || (not_the_first_time_pressed == 1) ) ) { // "¯" is an unicode character! It will be sent as 2 bytes ( 0xC2 0xAF ) soft_uart_send(PSTR("¯"),1); button_press_just_happened = 0; not_the_first_time_pressed = 1; delay(50); } else { //soft_uart_send(PSTR("_"),1); } } return 0; }