ben winstone
/
ucam
As of Monday morning, so this is the code we showed at Uncraftivism.
Embed:
(wiki syntax)
Show/hide line numbers
SerialBuffered.h
00001 #pragma once 00002 00003 // This is a buffered serial reading class, using the serial interrupt introduced in mbed library version 18 on 17/11/09 00004 00005 // In the simplest case, construct it with a buffer size at least equal to the largest message you 00006 // expect your program to receive in one go. 00007 00008 class SerialBuffered : public Serial 00009 { 00010 public: 00011 SerialBuffered( size_t bufferSize, PinName tx, PinName rx ); 00012 virtual ~SerialBuffered(); 00013 00014 int getc(); // will block till the next character turns up, or return -1 if there is a timeout 00015 00016 int readable(); // returns 1 if there is a character available to read, 0 otherwise 00017 00018 void setTimeout( float seconds ); // maximum time in seconds that getc() should block 00019 // while waiting for a character 00020 // Pass -1 to disable the timeout. 00021 00022 size_t readBytes( uint8_t *bytes, size_t requested ); // read requested bytes into a buffer, 00023 // return number actually read, 00024 // which may be less than requested if there has been a timeout 00025 00026 00027 private: 00028 00029 void handleInterrupt(); 00030 00031 00032 uint8_t *m_buff; // points at a circular buffer, containing data from m_contentStart, for m_contentSize bytes, wrapping when you get to the end 00033 uint16_t m_contentStart; // index of first bytes of content 00034 uint16_t m_contentEnd; // index of bytes after last byte of content 00035 uint16_t m_buffSize; 00036 float m_timeout; 00037 Timer m_timer; 00038 00039 };
Generated on Wed Jul 27 2022 03:18:56 by 1.7.2