// gets a single byte from the uart receive buffer (getchar-style) int uartGetByte(void) { u08 c; //implement blocking read while(!uartReceiveByte(&c)); return c; }
void CommandLine(void) { u08 c; /* Variable to Run or stop program */ Run = TRUE; /* Clears the screen and sets the cursor under the Title */ vt100ClearScreen(); vt100SetCursorPos(1,0); rprintfProgStrM("\r\n\t\t\tStepper Motor Driver - Serial Console\r\n"); cmdlineInit(); cmdlineSetOutputFunc(uartSendByte); cmdlineAddCommand("quit", quitCommandLine); cmdlineAddCommand("help", helpDisplay); cmdlineAddCommand("step", runMotor); cmdlineInputFunc('\r'); while(Run) { while( uartReceiveByte(&c) ) cmdlineInputFunc(c); cmdlineMainLoop(); } rprintfCRLF(); rprintf("Program halted."); }
// gets a single byte from the uart receive buffer (getchar-style) int uartGetByte(void) { u08 c; if(uartReceiveByte(&c)) return c; else return -1; }
void midiPoll(void) { unsigned char byte, tmp; while (uartReceiveByte(&byte)) { // state machine for parsing the byte switch(midiState) { // we are currently stateless, waiting to start reading an event we care about. case MIDI_WAIT: if (byte == 0xF0) { // start of sysex // call sysex handler, which will return the state we should be in. midiState = handleSysex(); break; } // store length of midi command tmp = commandLen(byte); // is the message one byte long? if (tmp == 1) { // send event out! /* if (byte == MIDI_CLOCK && midiClockFunc) { // it's a clock event and we have a registered clock handler midiClockFunc(); } else {*/ processMidiEvent(byte, 0,0); // } } else if (tmp == 0) { // PORTC = byte; } else { // save first byte of event, position pointer.. midiEvent[0] = byte; midiReadIndex = 1; // go to state READING to read rest of event midiState = MIDI_READING; } break; case MIDI_READING: midiEvent[midiReadIndex++] = byte; if (midiReadIndex == commandLen(midiEvent[0]&0xF0)) { processMidiEvent(midiEvent[0], midiEvent[1], midiEvent[2]); midiState = MIDI_WAIT; } break; case MIDI_IGNORING: if (byte == 0xF7) { midiState = MIDI_WAIT; } break; } } }
int uart1GetByte(void) { // get single byte from receive buffer (if available) u08 c; if(uartReceiveByte(1,&c)) return c; else return -1; }
int uartGetByte(u08 nUart) { assert(nUart < 4); u08 c; // get single byte from receive buffer (if available) if(uartReceiveByte(nUart, &c)) return c; else return -1; }
// functions u08 inputString(u08 termChar, u08 termLen, u08* data) { u08 s=0; u08 length=0; while(length < termLen) { // get input #ifdef __AVR_ATmega128__ while(!uartReceiveByte(INPUT_UART,&s)); #else while(!uartReceiveByte(&s)); #endif // handle special characters if(s == 0x08) // backspace { if(length > 0) { // echo backspace-space-backspace rprintfChar(0x08); rprintfChar(' '); rprintfChar(0x08); length--; } } else if(s == termChar) // termination character { // save null-termination data[length] = 0; break; } else { // echo character rprintfChar(s); // save character data[length++] = s; } } return length; }
void sendIO(u08 uartReceiveId, u08 uartSendId) { u08 data; bool dataToSend = false; while (uartReceiveByte(uartReceiveId, &data)) { dataToSend = true; uartAddToTxBuffer(uartSendId, data); } if (dataToSend) uartSendTxBuffer(uartSendId); }
void goCmdline(void) { u08 c; // print welcome message vt100ClearScreen(); vt100SetCursorPos(1,0); rprintfProgStrM("\r\nWelcome to the Command Line Test Suite!\r\n"); // initialize cmdline system cmdlineInit(); // direct cmdline output to uart (serial port) cmdlineSetOutputFunc(uartSendByte); // add commands to the command database cmdlineAddCommand("exit", exitFunction); cmdlineAddCommand("help", helpFunction); cmdlineAddCommand("dumpargs1", dumpArgsStr); cmdlineAddCommand("dumpargs2", dumpArgsInt); cmdlineAddCommand("dumpargs3", dumpArgsHex); // send a CR to cmdline input to stimulate a prompt cmdlineInputFunc('\r'); // set state to run Run = TRUE; // main loop while(Run) { // pass characters received on the uart (serial port) // into the cmdline processor while(uartReceiveByte(&c)) cmdlineInputFunc(c); // run the cmdline execution functions cmdlineMainLoop(); } rprintfCRLF(); rprintf("Exited program!\r\n"); }
void open_log_ls(char *buffer, size_t size) { open_log_command_mode(); _delay_ms(10); uartSendString_P(1,"ls"); uartSendByte(1,0x0D); _delay_ms(10); u08 index = 0; while( 1 ) { u08 data = 0; if( uartReceiveByte(1,&data) ) { buffer[index++] = data; } if( index >= size ) { buffer[index] = '\0'; break; } _delay_ms(10); } }
void i2cTest(void) { u08 c=0; showByte(0x55); // initialize i2c function library i2cInit(); // set local device address and allow response to general call i2cSetLocalDeviceAddr(LOCAL_ADDR, TRUE); // set the Slave Receive Handler function // (this function will run whenever a master somewhere else on the bus // writes data to us as a slave) i2cSetSlaveReceiveHandler( i2cSlaveReceiveService ); // set the Slave Transmit Handler function // (this function will run whenever a master somewhere else on the bus // attempts to read data from us as a slave) i2cSetSlaveTransmitHandler( i2cSlaveTransmitService ); timerPause(500); showByte(0xAA); while(1) { rprintf("Command>"); while(!c) uartReceiveByte(&c); switch(c) { case 's': rprintf("Send: "); // get string from terminal localBufferLength = 0; c = 0; while((c != 0x0D) && (localBufferLength < 0x20)) { while(!uartReceiveByte(&c)); localBuffer[localBufferLength++] = c; uartSendByte(c); } // switch CR to NULL to terminate string localBuffer[localBufferLength-1] = 0; // send string over I2C i2cMasterSend(TARGET_ADDR, localBufferLength, localBuffer); //i2cMasterSendNI(TARGET_ADDR, localBufferLength, localBuffer); rprintfCRLF(); break; case 'r': rprintf("Receive: "); // receive string over I2C i2cMasterReceive(TARGET_ADDR, 0x10, localBuffer); //i2cMasterReceiveNI(TARGET_ADDR, 0x10, localBuffer); // format buffer localBuffer[0x10] = 0; rprintfStr(localBuffer); rprintfCRLF(); break; case 'm': testI2cMemory(); break; default: rprintf("Unknown Command!"); break; } c = 0; rprintfCRLF(); } }
int main(void) { unsigned char tempSec; get_mcusr(); // Disable the watchdog timer uartInit(); // Init both UARTs uartSetBaudRate(0,BAUD); uartSetBaudRate(1,BAUD); stdout = stdin = &uart1_str; // Init the stdout and stdin (printf, scanf...) functions stderr = &uart0_str; // Init the stderror functions i2cInit(); i2cSetBitrate(100); ds1307_enable(DS1307_I2C_ADDR); // Enable the DS1307 RTC // Make a flash on LEDs connected to PORTB when starting / restarting DDRB=0xff; // PORTB output PORTB=0xff; // All bits on port B = 1 _delay_ms(1000); // Wait until XBee modem has started PORTB=0x00; // All bits on port B = 0 // Empty the receive buffer of both UARTs uartFlushReceiveBuffer(EthUART); uartFlushReceiveBuffer(XBeeUART); wdt_enable(WDTO_2S); // Enable watchdog, reset after 2 seconds wdt_reset(); while(1) { wdt_reset(); // Reset the watchdog timer tempSec=time.sec; if(ds1307_gettime(DS1307_I2C_ADDR, &time)) { //if(tempSec!=time.sec) if((time.sec%2==0)&&(tempSec!=time.sec)) // True every 2nd second { while(!uartReceiveBufferIsEmpty(XBeeUART)) { int i; i=0; signed char XBeePacket[12]; // Read data from slave module while (i<13) { while (uartReceiveBufferIsEmpty(XBeeUART)) ;; // Syncronization uartReceiveByte(XBeeUART, &XBeePacket[i]); if (XBeePacket[i]=='~') // Counter is set to 0 if the current character is '~' i=0; if (XBeePacket[0]=='~') //Increment if the first character in received data is '~' i++; } sm[XBeePacket[7]-'0'].temp=XBeePacket[12]-7; // Compensate for heat from components in slave module // Check status of slave module and make status ready to send to webserver if (XBeePacket[10]=='f') sm[XBeePacket[7]-'0'].status=0; else if (XBeePacket[10]=='n') sm[XBeePacket[7]-'0'].status=1; XBeePacketCounter[XBeePacket[7]-'0']=1; // Set packet counter for this slave module to 1 } // Check packet counter of all modules, if false: status=2 is sent to the webserver // Webserver will then now which modules that are unconnected for (int i=0; i<noOfModules; i++) { if (!XBeePacketCounter[i]) sm[i].status=2; XBeePacketCounter[i]=0; } // Receive data from webserver if (checkForEthPacket(ethPacket)) { if (ethPacket[pHour]<24)// edit time { time.yr=ethPacket[pYear]; time.mon=ethPacket[pMonth]; time.dat=ethPacket[pDate]; time.hr=ethPacket[pHour]; time.min=ethPacket[pMin]; ds1307_settime(DS1307_I2C_ADDR,time); } int number=0; while (number<noOfModules) { sm_web[number].type=ethPacket[pType+number*pFieldsModules]; sm_web[number].status=ethPacket[pStatus+number*pFieldsModules]; sm_web[number].temp=ethPacket[pTemp+number*pFieldsModules]; switch (sm_web[number].type) { case 0: if (sm_web[number].status==0) // Check if the current slave module is disabled on the website sendSlaveModule(number,0); // Send data to slave module: turn off relay else { if ((sm[number].temp)<(sm_web[number].temp-1)) // Measured temperature < wanted temperature - 1 ? sendSlaveModule(number,1); // Send data to slave module: turn on relay else if ((sm[number].temp)>(sm_web[number].temp+1)) // Measured temperature > wanted temperature + 1 ? sendSlaveModule(number,0); // Send data to slave module: turn off relay else sendSlaveModule(number,2); // Send data to slave module } break; } number++; // next slave module } } sendEthPacket(time, sm); // send packet with data to webserver } } } return 0; }