![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
I am no longer actively working on the ppCANOpen library, however, I want to publish this project so that anyone who wants to pick up any of the pieces can have a good example. This is a a project I was working on using the ppCANOpen library. It has a pretty in deep use of the object dictionary structure. And a number of functions to control high voltage pinball drivers, if you're into that sort of thing.
Dependencies: CANnucleo mbed ppCANOpen
Application/source/SerialBuffered.cpp@10:ec59d628ebdc, 2016-03-19 (annotated)
- Committer:
- ptpaterson
- Date:
- Sat Mar 19 01:44:35 2016 +0000
- Revision:
- 10:ec59d628ebdc
Final Submission (probs)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptpaterson | 10:ec59d628ebdc | 1 | #include "mbed.h" |
ptpaterson | 10:ec59d628ebdc | 2 | #include "SerialBuffered.h" |
ptpaterson | 10:ec59d628ebdc | 3 | |
ptpaterson | 10:ec59d628ebdc | 4 | /** |
ptpaterson | 10:ec59d628ebdc | 5 | * Create a buffered serial class. |
ptpaterson | 10:ec59d628ebdc | 6 | * |
ptpaterson | 10:ec59d628ebdc | 7 | * @param tx A pin for transmit. |
ptpaterson | 10:ec59d628ebdc | 8 | * @param rx A pin for receive. |
ptpaterson | 10:ec59d628ebdc | 9 | */ |
ptpaterson | 10:ec59d628ebdc | 10 | SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) { |
ptpaterson | 10:ec59d628ebdc | 11 | indexContentStart = 0; |
ptpaterson | 10:ec59d628ebdc | 12 | indexContentEnd = 0; |
ptpaterson | 10:ec59d628ebdc | 13 | timeout = 1; |
ptpaterson | 10:ec59d628ebdc | 14 | attach(this, &SerialBuffered::handleInterrupt); |
ptpaterson | 10:ec59d628ebdc | 15 | } |
ptpaterson | 10:ec59d628ebdc | 16 | |
ptpaterson | 10:ec59d628ebdc | 17 | /** |
ptpaterson | 10:ec59d628ebdc | 18 | * Destroy. |
ptpaterson | 10:ec59d628ebdc | 19 | */ |
ptpaterson | 10:ec59d628ebdc | 20 | SerialBuffered::~SerialBuffered() { |
ptpaterson | 10:ec59d628ebdc | 21 | } |
ptpaterson | 10:ec59d628ebdc | 22 | |
ptpaterson | 10:ec59d628ebdc | 23 | /** |
ptpaterson | 10:ec59d628ebdc | 24 | * Set timeout for getc(). |
ptpaterson | 10:ec59d628ebdc | 25 | * |
ptpaterson | 10:ec59d628ebdc | 26 | * @param ms milliseconds. (-1:Disable timeout) |
ptpaterson | 10:ec59d628ebdc | 27 | */ |
ptpaterson | 10:ec59d628ebdc | 28 | void SerialBuffered::setTimeout(int ms) { |
ptpaterson | 10:ec59d628ebdc | 29 | timeout = ms; |
ptpaterson | 10:ec59d628ebdc | 30 | } |
ptpaterson | 10:ec59d628ebdc | 31 | |
ptpaterson | 10:ec59d628ebdc | 32 | /** |
ptpaterson | 10:ec59d628ebdc | 33 | * Read requested bytes. |
ptpaterson | 10:ec59d628ebdc | 34 | * |
ptpaterson | 10:ec59d628ebdc | 35 | * @param bytes A pointer to a buffer. |
ptpaterson | 10:ec59d628ebdc | 36 | * @param requested Length. |
ptpaterson | 10:ec59d628ebdc | 37 | * |
ptpaterson | 10:ec59d628ebdc | 38 | * @return Readed byte length. |
ptpaterson | 10:ec59d628ebdc | 39 | */ |
ptpaterson | 10:ec59d628ebdc | 40 | size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) { |
ptpaterson | 10:ec59d628ebdc | 41 | int i = 0; |
ptpaterson | 10:ec59d628ebdc | 42 | while (i < requested) { |
ptpaterson | 10:ec59d628ebdc | 43 | int c = getc(); |
ptpaterson | 10:ec59d628ebdc | 44 | if (c < 0) { |
ptpaterson | 10:ec59d628ebdc | 45 | break; |
ptpaterson | 10:ec59d628ebdc | 46 | } |
ptpaterson | 10:ec59d628ebdc | 47 | bytes[i] = c; |
ptpaterson | 10:ec59d628ebdc | 48 | i++; |
ptpaterson | 10:ec59d628ebdc | 49 | } |
ptpaterson | 10:ec59d628ebdc | 50 | return i; |
ptpaterson | 10:ec59d628ebdc | 51 | } |
ptpaterson | 10:ec59d628ebdc | 52 | |
ptpaterson | 10:ec59d628ebdc | 53 | /** |
ptpaterson | 10:ec59d628ebdc | 54 | * Get a character. |
ptpaterson | 10:ec59d628ebdc | 55 | * |
ptpaterson | 10:ec59d628ebdc | 56 | * @return A character. (-1:timeout) |
ptpaterson | 10:ec59d628ebdc | 57 | */ |
ptpaterson | 10:ec59d628ebdc | 58 | int SerialBuffered::getc() { |
ptpaterson | 10:ec59d628ebdc | 59 | timer.reset(); |
ptpaterson | 10:ec59d628ebdc | 60 | timer.start(); |
ptpaterson | 10:ec59d628ebdc | 61 | while (indexContentStart == indexContentEnd) { |
ptpaterson | 10:ec59d628ebdc | 62 | wait_ms(1); |
ptpaterson | 10:ec59d628ebdc | 63 | if ((timeout > 0) && (timer.read_ms() > timeout)) { |
ptpaterson | 10:ec59d628ebdc | 64 | /* |
ptpaterson | 10:ec59d628ebdc | 65 | * Timeout occured. |
ptpaterson | 10:ec59d628ebdc | 66 | */ |
ptpaterson | 10:ec59d628ebdc | 67 | // printf("Timeout occured.\n"); |
ptpaterson | 10:ec59d628ebdc | 68 | return EOF; |
ptpaterson | 10:ec59d628ebdc | 69 | } |
ptpaterson | 10:ec59d628ebdc | 70 | } |
ptpaterson | 10:ec59d628ebdc | 71 | timer.stop(); |
ptpaterson | 10:ec59d628ebdc | 72 | |
ptpaterson | 10:ec59d628ebdc | 73 | uint8_t result = buffer[indexContentStart++]; |
ptpaterson | 10:ec59d628ebdc | 74 | indexContentStart = indexContentStart % BUFFERSIZE; |
ptpaterson | 10:ec59d628ebdc | 75 | |
ptpaterson | 10:ec59d628ebdc | 76 | return result; |
ptpaterson | 10:ec59d628ebdc | 77 | } |
ptpaterson | 10:ec59d628ebdc | 78 | |
ptpaterson | 10:ec59d628ebdc | 79 | /** |
ptpaterson | 10:ec59d628ebdc | 80 | * Returns 1 if there is a character available to read, 0 otherwise. |
ptpaterson | 10:ec59d628ebdc | 81 | */ |
ptpaterson | 10:ec59d628ebdc | 82 | int SerialBuffered::readable() { |
ptpaterson | 10:ec59d628ebdc | 83 | return indexContentStart != indexContentEnd; |
ptpaterson | 10:ec59d628ebdc | 84 | } |
ptpaterson | 10:ec59d628ebdc | 85 | |
ptpaterson | 10:ec59d628ebdc | 86 | void SerialBuffered::handleInterrupt() { |
ptpaterson | 10:ec59d628ebdc | 87 | while (Serial::readable()) { |
ptpaterson | 10:ec59d628ebdc | 88 | if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) { |
ptpaterson | 10:ec59d628ebdc | 89 | /* |
ptpaterson | 10:ec59d628ebdc | 90 | * Buffer overrun occured. |
ptpaterson | 10:ec59d628ebdc | 91 | */ |
ptpaterson | 10:ec59d628ebdc | 92 | // printf("Buffer overrun occured.\n"); |
ptpaterson | 10:ec59d628ebdc | 93 | Serial::getc(); |
ptpaterson | 10:ec59d628ebdc | 94 | } else { |
ptpaterson | 10:ec59d628ebdc | 95 | buffer[indexContentEnd++] = Serial::getc(); |
ptpaterson | 10:ec59d628ebdc | 96 | indexContentEnd = indexContentEnd % BUFFERSIZE; |
ptpaterson | 10:ec59d628ebdc | 97 | } |
ptpaterson | 10:ec59d628ebdc | 98 | } |
ptpaterson | 10:ec59d628ebdc | 99 | } |