Programme d'utilisation des AX12 avec rajout de l'MX12
Fork of test_carteAToutFaire_PR by
mbed/SerialHalfDuplex.h@1:f3f702086a30, 2017-05-11 (annotated)
- Committer:
- ClementBreteau
- Date:
- Thu May 11 11:49:50 2017 +0000
- Revision:
- 1:f3f702086a30
test de la carte ? tout faire du petit robot
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ClementBreteau | 1:f3f702086a30 | 1 | /* mbed Microcontroller Library - SerialHalfDuplex |
ClementBreteau | 1:f3f702086a30 | 2 | * Copyright (c) 2010-2011 ARM Limited. All rights reserved. |
ClementBreteau | 1:f3f702086a30 | 3 | */ |
ClementBreteau | 1:f3f702086a30 | 4 | |
ClementBreteau | 1:f3f702086a30 | 5 | #ifndef MBED_SERIALHALFDUPLEX_H |
ClementBreteau | 1:f3f702086a30 | 6 | #define MBED_SERIALHALFDUPLEX_H |
ClementBreteau | 1:f3f702086a30 | 7 | |
ClementBreteau | 1:f3f702086a30 | 8 | #include "device.h" |
ClementBreteau | 1:f3f702086a30 | 9 | |
ClementBreteau | 1:f3f702086a30 | 10 | #if DEVICE_SERIAL |
ClementBreteau | 1:f3f702086a30 | 11 | |
ClementBreteau | 1:f3f702086a30 | 12 | #include "Serial.h" |
ClementBreteau | 1:f3f702086a30 | 13 | #include "PinNames.h" |
ClementBreteau | 1:f3f702086a30 | 14 | #include "PeripheralNames.h" |
ClementBreteau | 1:f3f702086a30 | 15 | |
ClementBreteau | 1:f3f702086a30 | 16 | namespace mbed { |
ClementBreteau | 1:f3f702086a30 | 17 | |
ClementBreteau | 1:f3f702086a30 | 18 | /* Class: SerialHalfDuplex |
ClementBreteau | 1:f3f702086a30 | 19 | * A serial port (UART) for communication with other devices using |
ClementBreteau | 1:f3f702086a30 | 20 | * Half-Duplex, allowing transmit and receive on a single |
ClementBreteau | 1:f3f702086a30 | 21 | * shared transmit and receive line. Only one end should be transmitting |
ClementBreteau | 1:f3f702086a30 | 22 | * at a time. |
ClementBreteau | 1:f3f702086a30 | 23 | * |
ClementBreteau | 1:f3f702086a30 | 24 | * Both the tx and rx pin should be defined, and wired together. |
ClementBreteau | 1:f3f702086a30 | 25 | * This is in addition to them being wired to the other serial |
ClementBreteau | 1:f3f702086a30 | 26 | * device to allow both read and write functions to operate. |
ClementBreteau | 1:f3f702086a30 | 27 | * |
ClementBreteau | 1:f3f702086a30 | 28 | * Example: |
ClementBreteau | 1:f3f702086a30 | 29 | * > // Send a byte to a second HalfDuplex device, and read the response |
ClementBreteau | 1:f3f702086a30 | 30 | * > |
ClementBreteau | 1:f3f702086a30 | 31 | * > #include "mbed.h" |
ClementBreteau | 1:f3f702086a30 | 32 | * > |
ClementBreteau | 1:f3f702086a30 | 33 | * > // p9 and p10 should be wired together to form "a" |
ClementBreteau | 1:f3f702086a30 | 34 | * > // p28 and p27 should be wired together to form "b" |
ClementBreteau | 1:f3f702086a30 | 35 | * > // p9/p10 should be wired to p28/p27 as the Half Duplex connection |
ClementBreteau | 1:f3f702086a30 | 36 | * > |
ClementBreteau | 1:f3f702086a30 | 37 | * > SerialHalfDuplex a(p9, p10); |
ClementBreteau | 1:f3f702086a30 | 38 | * > SerialHalfDuplex b(p28, p27); |
ClementBreteau | 1:f3f702086a30 | 39 | * > |
ClementBreteau | 1:f3f702086a30 | 40 | * > void b_rx() { // second device response |
ClementBreteau | 1:f3f702086a30 | 41 | * > b.putc(b.getc() + 4); |
ClementBreteau | 1:f3f702086a30 | 42 | * > } |
ClementBreteau | 1:f3f702086a30 | 43 | * > |
ClementBreteau | 1:f3f702086a30 | 44 | * > int main() { |
ClementBreteau | 1:f3f702086a30 | 45 | * > b.attach(&b_rx); |
ClementBreteau | 1:f3f702086a30 | 46 | * > for(int c = 'A'; c < 'Z'; c++) { |
ClementBreteau | 1:f3f702086a30 | 47 | * > a.putc(c); |
ClementBreteau | 1:f3f702086a30 | 48 | * > printf("sent [%c]\n", c); |
ClementBreteau | 1:f3f702086a30 | 49 | * > wait(0.5); // b should respond |
ClementBreteau | 1:f3f702086a30 | 50 | * > if(a.readable()) { |
ClementBreteau | 1:f3f702086a30 | 51 | * > printf("received [%c]\n", a.getc()); |
ClementBreteau | 1:f3f702086a30 | 52 | * > } |
ClementBreteau | 1:f3f702086a30 | 53 | * > } |
ClementBreteau | 1:f3f702086a30 | 54 | * > } |
ClementBreteau | 1:f3f702086a30 | 55 | * |
ClementBreteau | 1:f3f702086a30 | 56 | * For Simplex and Full-Duplex Serial communication, see <Serial> |
ClementBreteau | 1:f3f702086a30 | 57 | */ |
ClementBreteau | 1:f3f702086a30 | 58 | class SerialHalfDuplex : public Serial { |
ClementBreteau | 1:f3f702086a30 | 59 | |
ClementBreteau | 1:f3f702086a30 | 60 | public: |
ClementBreteau | 1:f3f702086a30 | 61 | /* Constructor: SerialHalfDuplex |
ClementBreteau | 1:f3f702086a30 | 62 | * Create a half-duplex serial port, connected to the specified transmit |
ClementBreteau | 1:f3f702086a30 | 63 | * and receive pins. |
ClementBreteau | 1:f3f702086a30 | 64 | * |
ClementBreteau | 1:f3f702086a30 | 65 | * These pins should be wired together, as well as to the target device |
ClementBreteau | 1:f3f702086a30 | 66 | * |
ClementBreteau | 1:f3f702086a30 | 67 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 68 | * tx - Transmit pin |
ClementBreteau | 1:f3f702086a30 | 69 | * rx - Receive pin |
ClementBreteau | 1:f3f702086a30 | 70 | */ |
ClementBreteau | 1:f3f702086a30 | 71 | SerialHalfDuplex(PinName tx, PinName rx, const char *name = NULL); |
ClementBreteau | 1:f3f702086a30 | 72 | |
ClementBreteau | 1:f3f702086a30 | 73 | #if 0 // Inherited from Serial class, for documentation |
ClementBreteau | 1:f3f702086a30 | 74 | /* Function: baud |
ClementBreteau | 1:f3f702086a30 | 75 | * Set the baud rate of the serial port |
ClementBreteau | 1:f3f702086a30 | 76 | * |
ClementBreteau | 1:f3f702086a30 | 77 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 78 | * baudrate - The baudrate of the serial port (default = 9600). |
ClementBreteau | 1:f3f702086a30 | 79 | */ |
ClementBreteau | 1:f3f702086a30 | 80 | void baud(int baudrate); |
ClementBreteau | 1:f3f702086a30 | 81 | |
ClementBreteau | 1:f3f702086a30 | 82 | enum Parity { |
ClementBreteau | 1:f3f702086a30 | 83 | None = 0 |
ClementBreteau | 1:f3f702086a30 | 84 | , Odd |
ClementBreteau | 1:f3f702086a30 | 85 | , Even |
ClementBreteau | 1:f3f702086a30 | 86 | , Forced1 |
ClementBreteau | 1:f3f702086a30 | 87 | , Forced0 |
ClementBreteau | 1:f3f702086a30 | 88 | }; |
ClementBreteau | 1:f3f702086a30 | 89 | |
ClementBreteau | 1:f3f702086a30 | 90 | /* Function: format |
ClementBreteau | 1:f3f702086a30 | 91 | * Set the transmission format used by the Serial port |
ClementBreteau | 1:f3f702086a30 | 92 | * |
ClementBreteau | 1:f3f702086a30 | 93 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 94 | * bits - The number of bits in a word (5-8; default = 8) |
ClementBreteau | 1:f3f702086a30 | 95 | * parity - The parity used (Serial::None, Serial::Odd, |
ClementBreteau | 1:f3f702086a30 | 96 | Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
ClementBreteau | 1:f3f702086a30 | 97 | * stop - The number of stop bits (1 or 2; default = 1) |
ClementBreteau | 1:f3f702086a30 | 98 | */ |
ClementBreteau | 1:f3f702086a30 | 99 | void format(int bits = 8, Parity parity = Serial::None, int stop_bits |
ClementBreteau | 1:f3f702086a30 | 100 | = 1); |
ClementBreteau | 1:f3f702086a30 | 101 | |
ClementBreteau | 1:f3f702086a30 | 102 | /* Function: putc |
ClementBreteau | 1:f3f702086a30 | 103 | * Write a character |
ClementBreteau | 1:f3f702086a30 | 104 | * |
ClementBreteau | 1:f3f702086a30 | 105 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 106 | * c - The character to write to the serial port |
ClementBreteau | 1:f3f702086a30 | 107 | */ |
ClementBreteau | 1:f3f702086a30 | 108 | int putc(int c); |
ClementBreteau | 1:f3f702086a30 | 109 | |
ClementBreteau | 1:f3f702086a30 | 110 | /* Function: getc |
ClementBreteau | 1:f3f702086a30 | 111 | * Read a character |
ClementBreteau | 1:f3f702086a30 | 112 | * |
ClementBreteau | 1:f3f702086a30 | 113 | * Read a character from the serial port. This call will block |
ClementBreteau | 1:f3f702086a30 | 114 | * until a character is available. For testing if a character is |
ClementBreteau | 1:f3f702086a30 | 115 | * available for reading, see <readable>. |
ClementBreteau | 1:f3f702086a30 | 116 | * |
ClementBreteau | 1:f3f702086a30 | 117 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 118 | * returns - The character read from the serial port |
ClementBreteau | 1:f3f702086a30 | 119 | */ |
ClementBreteau | 1:f3f702086a30 | 120 | int getc(); |
ClementBreteau | 1:f3f702086a30 | 121 | |
ClementBreteau | 1:f3f702086a30 | 122 | /* Function: printf |
ClementBreteau | 1:f3f702086a30 | 123 | * Write a formated string |
ClementBreteau | 1:f3f702086a30 | 124 | * |
ClementBreteau | 1:f3f702086a30 | 125 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 126 | * format - A printf-style format string, followed by the |
ClementBreteau | 1:f3f702086a30 | 127 | * variables to use in formating the string. |
ClementBreteau | 1:f3f702086a30 | 128 | */ |
ClementBreteau | 1:f3f702086a30 | 129 | int printf(const char* format, ...); |
ClementBreteau | 1:f3f702086a30 | 130 | |
ClementBreteau | 1:f3f702086a30 | 131 | /* Function: scanf |
ClementBreteau | 1:f3f702086a30 | 132 | * Read a formated string |
ClementBreteau | 1:f3f702086a30 | 133 | * |
ClementBreteau | 1:f3f702086a30 | 134 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 135 | * format - A scanf-style format string, |
ClementBreteau | 1:f3f702086a30 | 136 | * followed by the pointers to variables to store the results. |
ClementBreteau | 1:f3f702086a30 | 137 | */ |
ClementBreteau | 1:f3f702086a30 | 138 | int scanf(const char* format, ...); |
ClementBreteau | 1:f3f702086a30 | 139 | |
ClementBreteau | 1:f3f702086a30 | 140 | /* Function: readable |
ClementBreteau | 1:f3f702086a30 | 141 | * Determine if there is a character available to read |
ClementBreteau | 1:f3f702086a30 | 142 | * |
ClementBreteau | 1:f3f702086a30 | 143 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 144 | * returns - 1 if there is a character available to read, else 0 |
ClementBreteau | 1:f3f702086a30 | 145 | */ |
ClementBreteau | 1:f3f702086a30 | 146 | int readable(); |
ClementBreteau | 1:f3f702086a30 | 147 | |
ClementBreteau | 1:f3f702086a30 | 148 | /* Function: writeable |
ClementBreteau | 1:f3f702086a30 | 149 | * Determine if there is space available to write a character |
ClementBreteau | 1:f3f702086a30 | 150 | * |
ClementBreteau | 1:f3f702086a30 | 151 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 152 | * returns - 1 if there is space to write a character, else 0 |
ClementBreteau | 1:f3f702086a30 | 153 | */ |
ClementBreteau | 1:f3f702086a30 | 154 | int writeable(); |
ClementBreteau | 1:f3f702086a30 | 155 | |
ClementBreteau | 1:f3f702086a30 | 156 | /* Function: attach |
ClementBreteau | 1:f3f702086a30 | 157 | * Attach a function to call whenever a serial interrupt is generated |
ClementBreteau | 1:f3f702086a30 | 158 | * |
ClementBreteau | 1:f3f702086a30 | 159 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 160 | * fptr - A pointer to a void function, or 0 to set as none |
ClementBreteau | 1:f3f702086a30 | 161 | */ |
ClementBreteau | 1:f3f702086a30 | 162 | void attach(void (*fptr)(void)); |
ClementBreteau | 1:f3f702086a30 | 163 | |
ClementBreteau | 1:f3f702086a30 | 164 | /* Function: attach |
ClementBreteau | 1:f3f702086a30 | 165 | * Attach a member function to call whenever a serial interrupt is generated |
ClementBreteau | 1:f3f702086a30 | 166 | * |
ClementBreteau | 1:f3f702086a30 | 167 | * Variables: |
ClementBreteau | 1:f3f702086a30 | 168 | * tptr - pointer to the object to call the member function on |
ClementBreteau | 1:f3f702086a30 | 169 | * mptr - pointer to the member function to be called |
ClementBreteau | 1:f3f702086a30 | 170 | */ |
ClementBreteau | 1:f3f702086a30 | 171 | template<typename T> |
ClementBreteau | 1:f3f702086a30 | 172 | void attach(T* tptr, void (T::*mptr)(void)); |
ClementBreteau | 1:f3f702086a30 | 173 | |
ClementBreteau | 1:f3f702086a30 | 174 | #endif |
ClementBreteau | 1:f3f702086a30 | 175 | |
ClementBreteau | 1:f3f702086a30 | 176 | protected: |
ClementBreteau | 1:f3f702086a30 | 177 | PinName _txpin; |
ClementBreteau | 1:f3f702086a30 | 178 | |
ClementBreteau | 1:f3f702086a30 | 179 | virtual int _putc(int c); |
ClementBreteau | 1:f3f702086a30 | 180 | virtual int _getc(void); |
ClementBreteau | 1:f3f702086a30 | 181 | |
ClementBreteau | 1:f3f702086a30 | 182 | }; // End class SerialHalfDuplex |
ClementBreteau | 1:f3f702086a30 | 183 | |
ClementBreteau | 1:f3f702086a30 | 184 | } // End namespace |
ClementBreteau | 1:f3f702086a30 | 185 | |
ClementBreteau | 1:f3f702086a30 | 186 | #endif |
ClementBreteau | 1:f3f702086a30 | 187 | |
ClementBreteau | 1:f3f702086a30 | 188 | #endif |