/*********************************************************************** * 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; }
static void _cmd_setTimeout() { RecvCmd<10, 0> timeoutReceiver; while (-1 == timeoutReceiver.addChar(softuart_getchar())); unsigned int seconds = strtol(timeoutReceiver.buffer(), 0, 0); timeoutTicks = seconds * 1000000 / timerTick_us; }
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; } }
int main() { resetPin.low(); resetPin.input(); ledPin.low(); ledPin.output(); // Ensure that whatever is in the EEPROM is null-terminated. If it // wasn't yet, we have just been flashed so set the stored // timestamp to a null string. if (0 != read1EEPROM(sizeof(lastTimestamp))) { _cmd_clearmem(); } softuart_init(); // Setup Timer1. TCCR1 = 15; // Prescaler period 2^14. FAST_SET(TCCR1, CTC1); // CTC mode with OCR1C register. // Set the counter interval to 244, which produces the period of // just under half a second (timerTick_us). OCR1C = 244; OCR1A = 244; // Timer1 interrupt will only be enabled once the timeout is known. sei(); RecvCmd<16, CMDNUM> cmdReceiver; for (byte i = 0; i < CMDNUM; ++i) { cmdReceiver.addCommand_P(commandStrings[i]); } softuart_turn_rx_on(); for (;;) { auto status = cmdReceiver.addChar(softuart_getchar()); if (status == -2) { softuart_puts_P("Invalid command!\n\r"); cmdReceiver.reset(); } else if (status >= 0) { commands[(byte)status](); cmdReceiver.reset(); } } return 0; }
static void _cmd_reset() { char c = 0; byte i = 0; while ('\r' != (c = softuart_getchar())) { if (c == '\n') continue; lastTimestamp[i] = c; i = (i + 1) % sizeof(lastTimestamp); } lastTimestamp[i] = 0; ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { ticks = 0; } // Reset also starts the watchdog. This way, it will also function // with no configuration. _cmd_start(); }
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; }
/** Reads a character from the serial port * * This function reads one character from the serial port * if available. * */ unsigned char clixxIOSerial::read(void) { // Return a character return softuart_getchar(); }
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 */ }