/*********************************************************************** * Name: get_ant_msg * * Description: wait and recieve a message from the nRF24AP2 * * Author(s): Charles Wolf * * @param: * int max_wait -> timeout in 1mSec increments * UCHAR *MSG -> pointer to the message array * @return: * int -> 0 = message timeout * 1 = message recieved * Comments: * ANT Message Structure * SYNC (0xA4) 1 Byte * length 1 Byte * message ID 1 Byte * Data length * CheckSum 1 Byte * * ********************************************************************/ int get_ant_msg(int max_wait, UCHAR *MSG) { int count = 0; int i = 0; int outcome = 0; int length = 0; int checksum = 0; softuart_turn_rx_on(); softuart_flush_input_buffer(); while((count < max_wait) && (outcome != 1)) { //wait for data while((softuart_kbhit() < 1) && (count < max_wait)) { _delay_us(100); // 1bit = 208.333uSec count++; } if ( softuart_kbhit() > 0 ) { outcome = 2; //no sync //check for sync MSG[0] = softuart_getchar(); //softuart_getchar() is a possible place to get stuck waiting if(MSG[0] == MESG_TX_SYNC) { MSG[1] = softuart_getchar(); //length length = MSG[1]+2; if(length > 17) length = 17; //protect length for (i = 0; i < length; i++) { MSG[i+2] = softuart_getchar(); } checksum = checkSum(&MSG[0], length+3); if (checksum != MSG[length+4]) { outcome = 3; //bad checksum } else { outcome = 1; //message recieved } } } } softuart_turn_rx_off(); return outcome; }
int main(void) { static char cmd[10]; char c, *p = cmd; setup(); while(1) { if(softuart_kbhit()) { c = softuart_getchar(); switch(c) { case '\n': case '\r': { softuart_putchar(c); *p = 0; p = cmd; if((cmd[0] == '\0') || (execute_cmd(cmd) == CMD_OK)) softuart_puts_p(PSTR("ok\r\n")); else softuart_puts_p(PSTR("error\r\n")); break; } default: { if(p < cmd+(sizeof(cmd))) { *p++ = c; softuart_putchar(c); } else { softuart_putchar(7); // bell } } } } } return 0; }
int32_t motor_getPos(uint8_t motor, uint8_t * valid_val) { if(softuart_kbhit(motor)){ char c; int32_t r=0; uint8_t is_negative= 0; while( (c=softuart_getchar(motor))!='\r' ) { // MUST IMPLEMENT WATCHDOG TIMER HERE if(c=='-') is_negative=1; // Assuming "-" is the first character. else if(c>='0'&&c<='9') { r*=10; r+=c-'0'; } } if(is_negative)r*=-1; softuart_getchar(motor); // for '\n', MUST IMPLEMENT WATCHDOG TIMER HERE *valid_val = 1; return r; } else{ *valid_val = 0; return 0; } }
/** Determines if characters are available for reading * * This function checks internal buffers and informs the * caller if there are characters that are available for * reading from the port. * */ int clixxIOSerial::available(void) { return softuart_kbhit(); }
int main(void) { char c; unsigned short cnt = 0; #if (F_CPU > 4000000UL) #define CNTHALLO (unsigned int)(0xFFFF) #else #define CNTHALLO (unsigned int)(0xFFFF/3) #endif unsigned short mycnt=0; CLKPR=_BV(CLKPCE); CLKPR=0; pir=0; DDRA |= 0xe2; PORTA |= 0x81; adc_init(); softuart_init(); TCCR1A=_BV(COM0B1)|_BV(WGM00); TCCR1B=_BV(CS00); softuart_turn_rx_on(); /* redundant - on by default */ sei(); #if WITH_STDIO_DEMO stdio_demo_func(); #endif OCR1A=0x10; OCR1B=0xf0; for (;;) { if((PINB&4)==4 ) pir=1; if ( softuart_kbhit() ) { c = softuart_getchar(); //softuart_putchar( '[' ); //softuart_putchar( c ); //softuart_putchar( ']' ); if(c=='g') { c=softuart_getchar(); if(c==C_ID(ID)) { softuart_putchar(3); softuart_putchar(2); send(); pir=0; } } else if( c=='s') { unsigned int pwm; c=softuart_getchar(); if(c==C_ID(ID)) { sprintf(line,"made it"); // char line[32]; for(cnt=0;cnt<=31;cnt++) { c=softuart_getchar(); if(c=='\n') {line[cnt]=0; break;} else line[cnt]=c; } int ret=sscanf_P(&line[0],PSTR("inTopic/pwm/"STD_ID(ID)" %d"),&pwm); sprintf_P(dbg,PSTR("%dpwm:%d"),ret,pwm); OCR1B=pwm; } } } } return 0; /* never reached */ }