inoue hirohumi
/
line_counter_test
クロスラインを通過したらカウントする
SerialHalfDuplex/SerialHalfDuplex.h@3:aade65d7ae44, 2014-11-29 (annotated)
- Committer:
- iou16
- Date:
- Sat Nov 29 18:17:26 2014 +0000
- Revision:
- 3:aade65d7ae44
- Parent:
- 0:af0b7ccbbad8
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iou16 | 0:af0b7ccbbad8 | 1 | /* mbed Microcontroller Library |
iou16 | 0:af0b7ccbbad8 | 2 | * Copyright (c) 2006-2012 ARM Limited |
iou16 | 0:af0b7ccbbad8 | 3 | * |
iou16 | 0:af0b7ccbbad8 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
iou16 | 0:af0b7ccbbad8 | 5 | * of this software and associated documentation files (the "Software"), to deal |
iou16 | 0:af0b7ccbbad8 | 6 | * in the Software without restriction, including without limitation the rights |
iou16 | 0:af0b7ccbbad8 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
iou16 | 0:af0b7ccbbad8 | 8 | * copies of the Software, and to permit persons to whom the Software is |
iou16 | 0:af0b7ccbbad8 | 9 | * furnished to do so, subject to the following conditions: |
iou16 | 0:af0b7ccbbad8 | 10 | * |
iou16 | 0:af0b7ccbbad8 | 11 | * The above copyright notice and this permission notice shall be included in |
iou16 | 0:af0b7ccbbad8 | 12 | * all copies or substantial portions of the Software. |
iou16 | 0:af0b7ccbbad8 | 13 | * |
iou16 | 0:af0b7ccbbad8 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
iou16 | 0:af0b7ccbbad8 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
iou16 | 0:af0b7ccbbad8 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
iou16 | 0:af0b7ccbbad8 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
iou16 | 0:af0b7ccbbad8 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
iou16 | 0:af0b7ccbbad8 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
iou16 | 0:af0b7ccbbad8 | 20 | * SOFTWARE. |
iou16 | 0:af0b7ccbbad8 | 21 | * |
iou16 | 0:af0b7ccbbad8 | 22 | * NOTE: This is an unsupported legacy untested library. |
iou16 | 0:af0b7ccbbad8 | 23 | */ |
iou16 | 0:af0b7ccbbad8 | 24 | #ifndef MBED_SERIALHALFDUPLEX_H |
iou16 | 0:af0b7ccbbad8 | 25 | #define MBED_SERIALHALFDUPLEX_H |
iou16 | 0:af0b7ccbbad8 | 26 | |
iou16 | 0:af0b7ccbbad8 | 27 | #include "device.h" |
iou16 | 0:af0b7ccbbad8 | 28 | |
iou16 | 0:af0b7ccbbad8 | 29 | #if DEVICE_SERIAL |
iou16 | 0:af0b7ccbbad8 | 30 | |
iou16 | 0:af0b7ccbbad8 | 31 | #include "Serial.h" |
iou16 | 0:af0b7ccbbad8 | 32 | #include "PinNames.h" |
iou16 | 0:af0b7ccbbad8 | 33 | #include "PeripheralNames.h" |
iou16 | 0:af0b7ccbbad8 | 34 | |
iou16 | 0:af0b7ccbbad8 | 35 | namespace mbed { |
iou16 | 0:af0b7ccbbad8 | 36 | |
iou16 | 0:af0b7ccbbad8 | 37 | /* Class: SerialHalfDuplex |
iou16 | 0:af0b7ccbbad8 | 38 | * A serial port (UART) for communication with other devices using |
iou16 | 0:af0b7ccbbad8 | 39 | * Half-Duplex, allowing transmit and receive on a single |
iou16 | 0:af0b7ccbbad8 | 40 | * shared transmit and receive line. Only one end should be transmitting |
iou16 | 0:af0b7ccbbad8 | 41 | * at a time. |
iou16 | 0:af0b7ccbbad8 | 42 | * |
iou16 | 0:af0b7ccbbad8 | 43 | * Both the tx and rx pin should be defined, and wired together. |
iou16 | 0:af0b7ccbbad8 | 44 | * This is in addition to them being wired to the other serial |
iou16 | 0:af0b7ccbbad8 | 45 | * device to allow both read and write functions to operate. |
iou16 | 0:af0b7ccbbad8 | 46 | * |
iou16 | 0:af0b7ccbbad8 | 47 | * Example: |
iou16 | 0:af0b7ccbbad8 | 48 | * > // Send a byte to a second HalfDuplex device, and read the response |
iou16 | 0:af0b7ccbbad8 | 49 | * > |
iou16 | 0:af0b7ccbbad8 | 50 | * > #include "mbed.h" |
iou16 | 0:af0b7ccbbad8 | 51 | * > |
iou16 | 0:af0b7ccbbad8 | 52 | * > // p9 and p10 should be wired together to form "a" |
iou16 | 0:af0b7ccbbad8 | 53 | * > // p28 and p27 should be wired together to form "b" |
iou16 | 0:af0b7ccbbad8 | 54 | * > // p9/p10 should be wired to p28/p27 as the Half Duplex connection |
iou16 | 0:af0b7ccbbad8 | 55 | * > |
iou16 | 0:af0b7ccbbad8 | 56 | * > SerialHalfDuplex a(p9, p10); |
iou16 | 0:af0b7ccbbad8 | 57 | * > SerialHalfDuplex b(p28, p27); |
iou16 | 0:af0b7ccbbad8 | 58 | * > |
iou16 | 0:af0b7ccbbad8 | 59 | * > void b_rx() { // second device response |
iou16 | 0:af0b7ccbbad8 | 60 | * > b.putc(b.getc() + 4); |
iou16 | 0:af0b7ccbbad8 | 61 | * > } |
iou16 | 0:af0b7ccbbad8 | 62 | * > |
iou16 | 0:af0b7ccbbad8 | 63 | * > int main() { |
iou16 | 0:af0b7ccbbad8 | 64 | * > b.attach(&b_rx); |
iou16 | 0:af0b7ccbbad8 | 65 | * > for(int c = 'A'; c < 'Z'; c++) { |
iou16 | 0:af0b7ccbbad8 | 66 | * > a.putc(c); |
iou16 | 0:af0b7ccbbad8 | 67 | * > printf("sent [%c]\n", c); |
iou16 | 0:af0b7ccbbad8 | 68 | * > wait(0.5); // b should respond |
iou16 | 0:af0b7ccbbad8 | 69 | * > if(a.readable()) { |
iou16 | 0:af0b7ccbbad8 | 70 | * > printf("received [%c]\n", a.getc()); |
iou16 | 0:af0b7ccbbad8 | 71 | * > } |
iou16 | 0:af0b7ccbbad8 | 72 | * > } |
iou16 | 0:af0b7ccbbad8 | 73 | * > } |
iou16 | 0:af0b7ccbbad8 | 74 | * |
iou16 | 0:af0b7ccbbad8 | 75 | * For Simplex and Full-Duplex Serial communication, see <Serial> |
iou16 | 0:af0b7ccbbad8 | 76 | */ |
iou16 | 0:af0b7ccbbad8 | 77 | class SerialHalfDuplex : public Serial { |
iou16 | 0:af0b7ccbbad8 | 78 | |
iou16 | 0:af0b7ccbbad8 | 79 | public: |
iou16 | 0:af0b7ccbbad8 | 80 | /* Constructor: SerialHalfDuplex |
iou16 | 0:af0b7ccbbad8 | 81 | * Create a half-duplex serial port, connected to the specified transmit |
iou16 | 0:af0b7ccbbad8 | 82 | * and receive pins. |
iou16 | 0:af0b7ccbbad8 | 83 | * |
iou16 | 0:af0b7ccbbad8 | 84 | * These pins should be wired together, as well as to the target device |
iou16 | 0:af0b7ccbbad8 | 85 | * |
iou16 | 0:af0b7ccbbad8 | 86 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 87 | * tx - Transmit pin |
iou16 | 0:af0b7ccbbad8 | 88 | * rx - Receive pin |
iou16 | 0:af0b7ccbbad8 | 89 | */ |
iou16 | 0:af0b7ccbbad8 | 90 | SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL); |
iou16 | 0:af0b7ccbbad8 | 91 | |
iou16 | 0:af0b7ccbbad8 | 92 | #if 0 // Inherited from Serial class, for documentation |
iou16 | 0:af0b7ccbbad8 | 93 | /* Function: baud |
iou16 | 0:af0b7ccbbad8 | 94 | * Set the baud rate of the serial port |
iou16 | 0:af0b7ccbbad8 | 95 | * |
iou16 | 0:af0b7ccbbad8 | 96 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 97 | * baudrate - The baudrate of the serial port (default = 9600). |
iou16 | 0:af0b7ccbbad8 | 98 | */ |
iou16 | 0:af0b7ccbbad8 | 99 | void baud(int baudrate); |
iou16 | 0:af0b7ccbbad8 | 100 | |
iou16 | 0:af0b7ccbbad8 | 101 | enum Parity { |
iou16 | 0:af0b7ccbbad8 | 102 | None = 0 |
iou16 | 0:af0b7ccbbad8 | 103 | , Odd |
iou16 | 0:af0b7ccbbad8 | 104 | , Even |
iou16 | 0:af0b7ccbbad8 | 105 | , Forced1 |
iou16 | 0:af0b7ccbbad8 | 106 | , Forced0 |
iou16 | 0:af0b7ccbbad8 | 107 | }; |
iou16 | 0:af0b7ccbbad8 | 108 | |
iou16 | 0:af0b7ccbbad8 | 109 | /* Function: format |
iou16 | 0:af0b7ccbbad8 | 110 | * Set the transmission format used by the Serial port |
iou16 | 0:af0b7ccbbad8 | 111 | * |
iou16 | 0:af0b7ccbbad8 | 112 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 113 | * bits - The number of bits in a word (5-8; default = 8) |
iou16 | 0:af0b7ccbbad8 | 114 | * parity - The parity used (Serial::None, Serial::Odd, |
iou16 | 0:af0b7ccbbad8 | 115 | Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
iou16 | 0:af0b7ccbbad8 | 116 | * stop - The number of stop bits (1 or 2; default = 1) |
iou16 | 0:af0b7ccbbad8 | 117 | */ |
iou16 | 0:af0b7ccbbad8 | 118 | void format(int bits = 8, Parity parity = Serial::None, int stop_bits |
iou16 | 0:af0b7ccbbad8 | 119 | = 1); |
iou16 | 0:af0b7ccbbad8 | 120 | |
iou16 | 0:af0b7ccbbad8 | 121 | /* Function: putc |
iou16 | 0:af0b7ccbbad8 | 122 | * Write a character |
iou16 | 0:af0b7ccbbad8 | 123 | * |
iou16 | 0:af0b7ccbbad8 | 124 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 125 | * c - The character to write to the serial port |
iou16 | 0:af0b7ccbbad8 | 126 | */ |
iou16 | 0:af0b7ccbbad8 | 127 | int putc(int c); |
iou16 | 0:af0b7ccbbad8 | 128 | |
iou16 | 0:af0b7ccbbad8 | 129 | /* Function: getc |
iou16 | 0:af0b7ccbbad8 | 130 | * Read a character |
iou16 | 0:af0b7ccbbad8 | 131 | * |
iou16 | 0:af0b7ccbbad8 | 132 | * Read a character from the serial port. This call will block |
iou16 | 0:af0b7ccbbad8 | 133 | * until a character is available. For testing if a character is |
iou16 | 0:af0b7ccbbad8 | 134 | * available for reading, see <readable>. |
iou16 | 0:af0b7ccbbad8 | 135 | * |
iou16 | 0:af0b7ccbbad8 | 136 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 137 | * returns - The character read from the serial port |
iou16 | 0:af0b7ccbbad8 | 138 | */ |
iou16 | 0:af0b7ccbbad8 | 139 | int getc(); |
iou16 | 0:af0b7ccbbad8 | 140 | |
iou16 | 0:af0b7ccbbad8 | 141 | /* Function: printf |
iou16 | 0:af0b7ccbbad8 | 142 | * Write a formated string |
iou16 | 0:af0b7ccbbad8 | 143 | * |
iou16 | 0:af0b7ccbbad8 | 144 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 145 | * format - A printf-style format string, followed by the |
iou16 | 0:af0b7ccbbad8 | 146 | * variables to use in formating the string. |
iou16 | 0:af0b7ccbbad8 | 147 | */ |
iou16 | 0:af0b7ccbbad8 | 148 | int printf(const char* format, ...); |
iou16 | 0:af0b7ccbbad8 | 149 | |
iou16 | 0:af0b7ccbbad8 | 150 | /* Function: scanf |
iou16 | 0:af0b7ccbbad8 | 151 | * Read a formated string |
iou16 | 0:af0b7ccbbad8 | 152 | * |
iou16 | 0:af0b7ccbbad8 | 153 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 154 | * format - A scanf-style format string, |
iou16 | 0:af0b7ccbbad8 | 155 | * followed by the pointers to variables to store the results. |
iou16 | 0:af0b7ccbbad8 | 156 | */ |
iou16 | 0:af0b7ccbbad8 | 157 | int scanf(const char* format, ...); |
iou16 | 0:af0b7ccbbad8 | 158 | |
iou16 | 0:af0b7ccbbad8 | 159 | /* Function: readable |
iou16 | 0:af0b7ccbbad8 | 160 | * Determine if there is a character available to read |
iou16 | 0:af0b7ccbbad8 | 161 | * |
iou16 | 0:af0b7ccbbad8 | 162 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 163 | * returns - 1 if there is a character available to read, else 0 |
iou16 | 0:af0b7ccbbad8 | 164 | */ |
iou16 | 0:af0b7ccbbad8 | 165 | int readable(); |
iou16 | 0:af0b7ccbbad8 | 166 | |
iou16 | 0:af0b7ccbbad8 | 167 | /* Function: writeable |
iou16 | 0:af0b7ccbbad8 | 168 | * Determine if there is space available to write a character |
iou16 | 0:af0b7ccbbad8 | 169 | * |
iou16 | 0:af0b7ccbbad8 | 170 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 171 | * returns - 1 if there is space to write a character, else 0 |
iou16 | 0:af0b7ccbbad8 | 172 | */ |
iou16 | 0:af0b7ccbbad8 | 173 | int writeable(); |
iou16 | 0:af0b7ccbbad8 | 174 | |
iou16 | 0:af0b7ccbbad8 | 175 | /* Function: attach |
iou16 | 0:af0b7ccbbad8 | 176 | * Attach a function to call whenever a serial interrupt is generated |
iou16 | 0:af0b7ccbbad8 | 177 | * |
iou16 | 0:af0b7ccbbad8 | 178 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 179 | * fptr - A pointer to a void function, or 0 to set as none |
iou16 | 0:af0b7ccbbad8 | 180 | */ |
iou16 | 0:af0b7ccbbad8 | 181 | void attach(void (*fptr)(void)); |
iou16 | 0:af0b7ccbbad8 | 182 | |
iou16 | 0:af0b7ccbbad8 | 183 | /* Function: attach |
iou16 | 0:af0b7ccbbad8 | 184 | * Attach a member function to call whenever a serial interrupt is generated |
iou16 | 0:af0b7ccbbad8 | 185 | * |
iou16 | 0:af0b7ccbbad8 | 186 | * Variables: |
iou16 | 0:af0b7ccbbad8 | 187 | * tptr - pointer to the object to call the member function on |
iou16 | 0:af0b7ccbbad8 | 188 | * mptr - pointer to the member function to be called |
iou16 | 0:af0b7ccbbad8 | 189 | */ |
iou16 | 0:af0b7ccbbad8 | 190 | template<typename T> |
iou16 | 0:af0b7ccbbad8 | 191 | void attach(T* tptr, void (T::*mptr)(void)); |
iou16 | 0:af0b7ccbbad8 | 192 | |
iou16 | 0:af0b7ccbbad8 | 193 | #endif |
iou16 | 0:af0b7ccbbad8 | 194 | |
iou16 | 0:af0b7ccbbad8 | 195 | protected: |
iou16 | 0:af0b7ccbbad8 | 196 | PinName _txpin; |
iou16 | 0:af0b7ccbbad8 | 197 | |
iou16 | 0:af0b7ccbbad8 | 198 | virtual int _putc(int c); |
iou16 | 0:af0b7ccbbad8 | 199 | virtual int _getc(void); |
iou16 | 0:af0b7ccbbad8 | 200 | |
iou16 | 0:af0b7ccbbad8 | 201 | }; // End class SerialHalfDuplex |
iou16 | 0:af0b7ccbbad8 | 202 | |
iou16 | 0:af0b7ccbbad8 | 203 | } // End namespace |
iou16 | 0:af0b7ccbbad8 | 204 | |
iou16 | 0:af0b7ccbbad8 | 205 | #endif |
iou16 | 0:af0b7ccbbad8 | 206 | |
iou16 | 0:af0b7ccbbad8 | 207 | #endif |