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