/* Read up from standard input up to the first newline (\n) character, and at most size-1 characters, into the buffer s, with echoing and support for backspace. Returns the number of characters read. You can check whether a full line was read by seeing whether a newline precedes the terminating \0. */ ssize_t readline(char *s, size_t size) { size_t count = 0; while (1) { int c = getc_raw(); switch (c) { case '\r': /* Treat as newline */ case '\n': putc1('\n'); goto stop; break; case 127: if (count > 0) { putc1('\010'); putc1(' '); putc1('\010'); count--; } break; default: if (count<size-1) { putc1(s[count++]=c); } } } stop: s[count] = '\0'; return count; }
void iso_pdu1_rx(iso_m *m_receive) { iso_m m; byte i; byte da; pgn request_pgn; if (m_receive->bits.pf1 == PF1(ISO_REQUEST_PGN) && m_receive->bits.pf2 == PF2(ISO_REQUEST_PGN)) { da = m_receive->bits.ps1<<7 | m_receive->bits.ps2; if (da == 255 || da == source_address){ // something has been requested of us //m_receive->bits.data = &m_receive+4; request_pgn.byte_pgn[0] = 0; request_pgn.byte_pgn[1] = *((byte *) m_receive+6); request_pgn.byte_pgn[2] = *((byte *) m_receive+5); request_pgn.byte_pgn[3] = *((byte *) m_receive+4); //request_pgn = (byte *) m_receive+1; putc1(0xab); putc1(0xab); //puts1(m_receive, 7,0); //puts1((byte *) m_receive+1,6,0); puts1(request_pgn.byte_pgn, 4,0); switch (request_pgn.dw_pgn) { case ((dword) ADDRESS_CLAIM_PGN): toggle_led(LED2); address_claim_message(); break; case ((dword) PRODUCT_INFORMATION_PGN): toggle_led(LED1); break; } } } // if address claim and this source address, then send out a new address claim if (m_receive->bits.sa == source_address) { // quick first check m.bits.priority = 6; ISO_M(ADDRESS_CLAIM_PGN); if (m_receive->dw_id == m.dw_id) { // if my name < received name I get to keep my source address for(i=0;i<ADDRESS_CLAIM_PGN_LENGTH;i++) { if (name[i] < m_receive->bits.data[i]) break; if (name[i] > m_receive->bits.data[i]) { generate_new_source_address(); break; } if (i == ADDRESS_CLAIM_PGN_LENGTH-1) generate_new_source_address(); } address_claim_message(); } } }
/* Output the given char either to the string or to the TTY. */ static void printc(char *buf, char c, int flags) { if (flags & FLAG_TTY) { /* do not output (terminating) zeros to TTY */ if (c != '\0') putc1(c); } else *buf = c; }
void Task1() { nrk_time_t t; uint16_t cnt; cnt=0; nrk_kprintf( PSTR("Nano-RK Version ") ); printf( "%d\r\n",NRK_VERSION ); setup_uart1(UART_BAUDRATE_19K2); DDRE=0x2; printf( "My node's address is %u\r\n",NODE_ADDR ); printf( "Task1 PID=%u\r\n",nrk_get_pid()); t.secs=5; t.nano_secs=0; // setup a software watch dog timer nrk_sw_wdt_init(0, &t, NULL); nrk_sw_wdt_start(0); putc1('~'); putc1('A'); putc1(' '); putc1('8'); nrk_wait_until_next_period(); putc1('~'); putc1('A'); putc1(' '); putc1('8'); while(1) { // Update watchdog timer nrk_sw_wdt_update(0); nrk_led_toggle(ORANGE_LED); nrk_gpio_toggle(NRK_DEBUG_0); printf( "Task1 cnt=%u\r\n",cnt ); nrk_wait_until_next_period(); // Uncomment this line to cause a stack overflow // if(cnt>20) kill_stack(10); // At time 50, the OS will halt and print statistics // This requires the NRK_STATS_TRACKER #define in nrk_cfg.h // if(cnt==50) { // nrk_stats_display_all(); // nrk_halt(); // } cnt++; } }
void print_to_serial(byte *can_buf) { byte data_length; byte i; data_length = (can_buf[12]&0xF) + 4; // 4 bytes for id field /* if (ascii_out) { rprintf("%02X", data_length); for(i=0;i<data_length;i++) rprintf("%02X", can_buf[i]); rprintf("\n"); } else {*/ // putc1(0xAB); // putc1(0xCD); putc1(data_length); puts1(can_buf, (int) data_length,0); // from the id field forward through the bytes putcontrol1(NEWLINE_AND_LINEFEED); // } }