mbed robotracer for education.
Robotracer (line follower robot ) using stepper motor.
/media/uploads/hayama/mbedrobotracer-manual-english.pdf
/media/uploads/hayama/mbedrobotracer-manual-japanese.pdf
/media/uploads/hayama/eagle-design-robotracer.zip
movie -> https://www.youtube.com/watch?v=INwun8gSds4
(for competition->) https://www.youtube.com/watch?v=l_gP2pUt4w0
mbed/Serial.h@1:200aad416161, 2013-10-02 (annotated)
- Committer:
- hayama
- Date:
- Wed Oct 02 01:09:55 2013 +0000
- Revision:
- 1:200aad416161
- Parent:
- 0:da22b0b4395a
mbed robotracer for education.
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hayama | 0:da22b0b4395a | 1 | /* mbed Microcontroller Library |
hayama | 0:da22b0b4395a | 2 | * Copyright (c) 2006-2013 ARM Limited |
hayama | 0:da22b0b4395a | 3 | * |
hayama | 0:da22b0b4395a | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hayama | 0:da22b0b4395a | 5 | * you may not use this file except in compliance with the License. |
hayama | 0:da22b0b4395a | 6 | * You may obtain a copy of the License at |
hayama | 0:da22b0b4395a | 7 | * |
hayama | 0:da22b0b4395a | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
hayama | 0:da22b0b4395a | 9 | * |
hayama | 0:da22b0b4395a | 10 | * Unless required by applicable law or agreed to in writing, software |
hayama | 0:da22b0b4395a | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
hayama | 0:da22b0b4395a | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hayama | 0:da22b0b4395a | 13 | * See the License for the specific language governing permissions and |
hayama | 0:da22b0b4395a | 14 | * limitations under the License. |
hayama | 0:da22b0b4395a | 15 | */ |
hayama | 0:da22b0b4395a | 16 | #ifndef MBED_SERIAL_H |
hayama | 0:da22b0b4395a | 17 | #define MBED_SERIAL_H |
hayama | 0:da22b0b4395a | 18 | |
hayama | 0:da22b0b4395a | 19 | #include "platform.h" |
hayama | 0:da22b0b4395a | 20 | |
hayama | 0:da22b0b4395a | 21 | #if DEVICE_SERIAL |
hayama | 0:da22b0b4395a | 22 | |
hayama | 0:da22b0b4395a | 23 | #include "Stream.h" |
hayama | 0:da22b0b4395a | 24 | #include "FunctionPointer.h" |
hayama | 0:da22b0b4395a | 25 | #include "serial_api.h" |
hayama | 0:da22b0b4395a | 26 | |
hayama | 0:da22b0b4395a | 27 | namespace mbed { |
hayama | 0:da22b0b4395a | 28 | |
hayama | 0:da22b0b4395a | 29 | /** A serial port (UART) for communication with other serial devices |
hayama | 0:da22b0b4395a | 30 | * |
hayama | 0:da22b0b4395a | 31 | * Can be used for Full Duplex communication, or Simplex by specifying |
hayama | 0:da22b0b4395a | 32 | * one pin as NC (Not Connected) |
hayama | 0:da22b0b4395a | 33 | * |
hayama | 0:da22b0b4395a | 34 | * Example: |
hayama | 0:da22b0b4395a | 35 | * @code |
hayama | 0:da22b0b4395a | 36 | * // Print "Hello World" to the PC |
hayama | 0:da22b0b4395a | 37 | * |
hayama | 0:da22b0b4395a | 38 | * #include "mbed.h" |
hayama | 0:da22b0b4395a | 39 | * |
hayama | 0:da22b0b4395a | 40 | * Serial pc(USBTX, USBRX); |
hayama | 0:da22b0b4395a | 41 | * |
hayama | 0:da22b0b4395a | 42 | * int main() { |
hayama | 0:da22b0b4395a | 43 | * pc.printf("Hello World\n"); |
hayama | 0:da22b0b4395a | 44 | * } |
hayama | 0:da22b0b4395a | 45 | * @endcode |
hayama | 0:da22b0b4395a | 46 | */ |
hayama | 0:da22b0b4395a | 47 | class Serial : public Stream { |
hayama | 0:da22b0b4395a | 48 | |
hayama | 0:da22b0b4395a | 49 | public: |
hayama | 0:da22b0b4395a | 50 | /** Create a Serial port, connected to the specified transmit and receive pins |
hayama | 0:da22b0b4395a | 51 | * |
hayama | 0:da22b0b4395a | 52 | * @param tx Transmit pin |
hayama | 0:da22b0b4395a | 53 | * @param rx Receive pin |
hayama | 0:da22b0b4395a | 54 | * |
hayama | 0:da22b0b4395a | 55 | * @note |
hayama | 0:da22b0b4395a | 56 | * Either tx or rx may be specified as NC if unused |
hayama | 0:da22b0b4395a | 57 | */ |
hayama | 0:da22b0b4395a | 58 | Serial(PinName tx, PinName rx, const char *name=NULL); |
hayama | 0:da22b0b4395a | 59 | |
hayama | 0:da22b0b4395a | 60 | /** Set the baud rate of the serial port |
hayama | 0:da22b0b4395a | 61 | * |
hayama | 0:da22b0b4395a | 62 | * @param baudrate The baudrate of the serial port (default = 9600). |
hayama | 0:da22b0b4395a | 63 | */ |
hayama | 0:da22b0b4395a | 64 | void baud(int baudrate); |
hayama | 0:da22b0b4395a | 65 | |
hayama | 0:da22b0b4395a | 66 | enum Parity { |
hayama | 0:da22b0b4395a | 67 | None = 0, |
hayama | 0:da22b0b4395a | 68 | Odd, |
hayama | 0:da22b0b4395a | 69 | Even, |
hayama | 0:da22b0b4395a | 70 | Forced1, |
hayama | 0:da22b0b4395a | 71 | Forced0 |
hayama | 0:da22b0b4395a | 72 | }; |
hayama | 0:da22b0b4395a | 73 | |
hayama | 0:da22b0b4395a | 74 | enum IrqType { |
hayama | 0:da22b0b4395a | 75 | RxIrq = 0, |
hayama | 0:da22b0b4395a | 76 | TxIrq |
hayama | 0:da22b0b4395a | 77 | }; |
hayama | 0:da22b0b4395a | 78 | |
hayama | 0:da22b0b4395a | 79 | /** Set the transmission format used by the Serial port |
hayama | 0:da22b0b4395a | 80 | * |
hayama | 0:da22b0b4395a | 81 | * @param bits The number of bits in a word (5-8; default = 8) |
hayama | 0:da22b0b4395a | 82 | * @param parity The parity used (Serial::None, Serial::Odd, Serial::Even, Serial::Forced1, Serial::Forced0; default = Serial::None) |
hayama | 0:da22b0b4395a | 83 | * @param stop The number of stop bits (1 or 2; default = 1) |
hayama | 0:da22b0b4395a | 84 | */ |
hayama | 0:da22b0b4395a | 85 | void format(int bits = 8, Parity parity=Serial::None, int stop_bits=1); |
hayama | 0:da22b0b4395a | 86 | |
hayama | 0:da22b0b4395a | 87 | /** Determine if there is a character available to read |
hayama | 0:da22b0b4395a | 88 | * |
hayama | 0:da22b0b4395a | 89 | * @returns |
hayama | 0:da22b0b4395a | 90 | * 1 if there is a character available to read, |
hayama | 0:da22b0b4395a | 91 | * 0 otherwise |
hayama | 0:da22b0b4395a | 92 | */ |
hayama | 0:da22b0b4395a | 93 | int readable(); |
hayama | 0:da22b0b4395a | 94 | |
hayama | 0:da22b0b4395a | 95 | /** Determine if there is space available to write a character |
hayama | 0:da22b0b4395a | 96 | * |
hayama | 0:da22b0b4395a | 97 | * @returns |
hayama | 0:da22b0b4395a | 98 | * 1 if there is space to write a character, |
hayama | 0:da22b0b4395a | 99 | * 0 otherwise |
hayama | 0:da22b0b4395a | 100 | */ |
hayama | 0:da22b0b4395a | 101 | int writeable(); |
hayama | 0:da22b0b4395a | 102 | |
hayama | 0:da22b0b4395a | 103 | /** Attach a function to call whenever a serial interrupt is generated |
hayama | 0:da22b0b4395a | 104 | * |
hayama | 0:da22b0b4395a | 105 | * @param fptr A pointer to a void function, or 0 to set as none |
hayama | 0:da22b0b4395a | 106 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
hayama | 0:da22b0b4395a | 107 | */ |
hayama | 0:da22b0b4395a | 108 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
hayama | 0:da22b0b4395a | 109 | |
hayama | 0:da22b0b4395a | 110 | /** Attach a member function to call whenever a serial interrupt is generated |
hayama | 0:da22b0b4395a | 111 | * |
hayama | 0:da22b0b4395a | 112 | * @param tptr pointer to the object to call the member function on |
hayama | 0:da22b0b4395a | 113 | * @param mptr pointer to the member function to be called |
hayama | 0:da22b0b4395a | 114 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
hayama | 0:da22b0b4395a | 115 | */ |
hayama | 0:da22b0b4395a | 116 | template<typename T> |
hayama | 0:da22b0b4395a | 117 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
hayama | 0:da22b0b4395a | 118 | if((mptr != NULL) && (tptr != NULL)) { |
hayama | 0:da22b0b4395a | 119 | _irq[type].attach(tptr, mptr); |
hayama | 0:da22b0b4395a | 120 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
hayama | 0:da22b0b4395a | 121 | } |
hayama | 0:da22b0b4395a | 122 | } |
hayama | 0:da22b0b4395a | 123 | |
hayama | 0:da22b0b4395a | 124 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
hayama | 0:da22b0b4395a | 125 | |
hayama | 0:da22b0b4395a | 126 | protected: |
hayama | 0:da22b0b4395a | 127 | virtual int _getc(); |
hayama | 0:da22b0b4395a | 128 | virtual int _putc(int c); |
hayama | 0:da22b0b4395a | 129 | |
hayama | 0:da22b0b4395a | 130 | serial_t _serial; |
hayama | 0:da22b0b4395a | 131 | FunctionPointer _irq[2]; |
hayama | 0:da22b0b4395a | 132 | }; |
hayama | 0:da22b0b4395a | 133 | |
hayama | 0:da22b0b4395a | 134 | } // namespace mbed |
hayama | 0:da22b0b4395a | 135 | |
hayama | 0:da22b0b4395a | 136 | #endif |
hayama | 0:da22b0b4395a | 137 | |
hayama | 0:da22b0b4395a | 138 | #endif |