Mark Turner
/
lightsense_kl46z_basic_v1
Basic fading in and out of leds with light sensor.
mbed/SerialBase.h@0:cf7af2656659, 2016-09-19 (annotated)
- Committer:
- mturner5
- Date:
- Mon Sep 19 03:24:58 2016 +0000
- Revision:
- 0:cf7af2656659
Basic v1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mturner5 | 0:cf7af2656659 | 1 | /* mbed Microcontroller Library |
mturner5 | 0:cf7af2656659 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mturner5 | 0:cf7af2656659 | 3 | * |
mturner5 | 0:cf7af2656659 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mturner5 | 0:cf7af2656659 | 5 | * you may not use this file except in compliance with the License. |
mturner5 | 0:cf7af2656659 | 6 | * You may obtain a copy of the License at |
mturner5 | 0:cf7af2656659 | 7 | * |
mturner5 | 0:cf7af2656659 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mturner5 | 0:cf7af2656659 | 9 | * |
mturner5 | 0:cf7af2656659 | 10 | * Unless required by applicable law or agreed to in writing, software |
mturner5 | 0:cf7af2656659 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mturner5 | 0:cf7af2656659 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mturner5 | 0:cf7af2656659 | 13 | * See the License for the specific language governing permissions and |
mturner5 | 0:cf7af2656659 | 14 | * limitations under the License. |
mturner5 | 0:cf7af2656659 | 15 | */ |
mturner5 | 0:cf7af2656659 | 16 | #ifndef MBED_SERIALBASE_H |
mturner5 | 0:cf7af2656659 | 17 | #define MBED_SERIALBASE_H |
mturner5 | 0:cf7af2656659 | 18 | |
mturner5 | 0:cf7af2656659 | 19 | #include "platform.h" |
mturner5 | 0:cf7af2656659 | 20 | |
mturner5 | 0:cf7af2656659 | 21 | #if DEVICE_SERIAL |
mturner5 | 0:cf7af2656659 | 22 | |
mturner5 | 0:cf7af2656659 | 23 | #include "Stream.h" |
mturner5 | 0:cf7af2656659 | 24 | #include "FunctionPointer.h" |
mturner5 | 0:cf7af2656659 | 25 | #include "serial_api.h" |
mturner5 | 0:cf7af2656659 | 26 | |
mturner5 | 0:cf7af2656659 | 27 | namespace mbed { |
mturner5 | 0:cf7af2656659 | 28 | |
mturner5 | 0:cf7af2656659 | 29 | /** A base class for serial port implementations |
mturner5 | 0:cf7af2656659 | 30 | * Can't be instantiated directly (use Serial or RawSerial) |
mturner5 | 0:cf7af2656659 | 31 | */ |
mturner5 | 0:cf7af2656659 | 32 | class SerialBase { |
mturner5 | 0:cf7af2656659 | 33 | |
mturner5 | 0:cf7af2656659 | 34 | public: |
mturner5 | 0:cf7af2656659 | 35 | /** Set the baud rate of the serial port |
mturner5 | 0:cf7af2656659 | 36 | * |
mturner5 | 0:cf7af2656659 | 37 | * @param baudrate The baudrate of the serial port (default = 9600). |
mturner5 | 0:cf7af2656659 | 38 | */ |
mturner5 | 0:cf7af2656659 | 39 | void baud(int baudrate); |
mturner5 | 0:cf7af2656659 | 40 | |
mturner5 | 0:cf7af2656659 | 41 | enum Parity { |
mturner5 | 0:cf7af2656659 | 42 | None = 0, |
mturner5 | 0:cf7af2656659 | 43 | Odd, |
mturner5 | 0:cf7af2656659 | 44 | Even, |
mturner5 | 0:cf7af2656659 | 45 | Forced1, |
mturner5 | 0:cf7af2656659 | 46 | Forced0 |
mturner5 | 0:cf7af2656659 | 47 | }; |
mturner5 | 0:cf7af2656659 | 48 | |
mturner5 | 0:cf7af2656659 | 49 | enum IrqType { |
mturner5 | 0:cf7af2656659 | 50 | RxIrq = 0, |
mturner5 | 0:cf7af2656659 | 51 | TxIrq |
mturner5 | 0:cf7af2656659 | 52 | }; |
mturner5 | 0:cf7af2656659 | 53 | |
mturner5 | 0:cf7af2656659 | 54 | enum Flow { |
mturner5 | 0:cf7af2656659 | 55 | Disabled = 0, |
mturner5 | 0:cf7af2656659 | 56 | RTS, |
mturner5 | 0:cf7af2656659 | 57 | CTS, |
mturner5 | 0:cf7af2656659 | 58 | RTSCTS |
mturner5 | 0:cf7af2656659 | 59 | }; |
mturner5 | 0:cf7af2656659 | 60 | |
mturner5 | 0:cf7af2656659 | 61 | /** Set the transmission format used by the serial port |
mturner5 | 0:cf7af2656659 | 62 | * |
mturner5 | 0:cf7af2656659 | 63 | * @param bits The number of bits in a word (5-8; default = 8) |
mturner5 | 0:cf7af2656659 | 64 | * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None) |
mturner5 | 0:cf7af2656659 | 65 | * @param stop The number of stop bits (1 or 2; default = 1) |
mturner5 | 0:cf7af2656659 | 66 | */ |
mturner5 | 0:cf7af2656659 | 67 | void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1); |
mturner5 | 0:cf7af2656659 | 68 | |
mturner5 | 0:cf7af2656659 | 69 | /** Determine if there is a character available to read |
mturner5 | 0:cf7af2656659 | 70 | * |
mturner5 | 0:cf7af2656659 | 71 | * @returns |
mturner5 | 0:cf7af2656659 | 72 | * 1 if there is a character available to read, |
mturner5 | 0:cf7af2656659 | 73 | * 0 otherwise |
mturner5 | 0:cf7af2656659 | 74 | */ |
mturner5 | 0:cf7af2656659 | 75 | int readable(); |
mturner5 | 0:cf7af2656659 | 76 | |
mturner5 | 0:cf7af2656659 | 77 | /** Determine if there is space available to write a character |
mturner5 | 0:cf7af2656659 | 78 | * |
mturner5 | 0:cf7af2656659 | 79 | * @returns |
mturner5 | 0:cf7af2656659 | 80 | * 1 if there is space to write a character, |
mturner5 | 0:cf7af2656659 | 81 | * 0 otherwise |
mturner5 | 0:cf7af2656659 | 82 | */ |
mturner5 | 0:cf7af2656659 | 83 | int writeable(); |
mturner5 | 0:cf7af2656659 | 84 | |
mturner5 | 0:cf7af2656659 | 85 | /** Attach a function to call whenever a serial interrupt is generated |
mturner5 | 0:cf7af2656659 | 86 | * |
mturner5 | 0:cf7af2656659 | 87 | * @param fptr A pointer to a void function, or 0 to set as none |
mturner5 | 0:cf7af2656659 | 88 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
mturner5 | 0:cf7af2656659 | 89 | */ |
mturner5 | 0:cf7af2656659 | 90 | void attach(void (*fptr)(void), IrqType type=RxIrq); |
mturner5 | 0:cf7af2656659 | 91 | |
mturner5 | 0:cf7af2656659 | 92 | /** Attach a member function to call whenever a serial interrupt is generated |
mturner5 | 0:cf7af2656659 | 93 | * |
mturner5 | 0:cf7af2656659 | 94 | * @param tptr pointer to the object to call the member function on |
mturner5 | 0:cf7af2656659 | 95 | * @param mptr pointer to the member function to be called |
mturner5 | 0:cf7af2656659 | 96 | * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) |
mturner5 | 0:cf7af2656659 | 97 | */ |
mturner5 | 0:cf7af2656659 | 98 | template<typename T> |
mturner5 | 0:cf7af2656659 | 99 | void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { |
mturner5 | 0:cf7af2656659 | 100 | if((mptr != NULL) && (tptr != NULL)) { |
mturner5 | 0:cf7af2656659 | 101 | _irq[type].attach(tptr, mptr); |
mturner5 | 0:cf7af2656659 | 102 | serial_irq_set(&_serial, (SerialIrq)type, 1); |
mturner5 | 0:cf7af2656659 | 103 | } |
mturner5 | 0:cf7af2656659 | 104 | } |
mturner5 | 0:cf7af2656659 | 105 | |
mturner5 | 0:cf7af2656659 | 106 | /** Generate a break condition on the serial line |
mturner5 | 0:cf7af2656659 | 107 | */ |
mturner5 | 0:cf7af2656659 | 108 | void send_break(); |
mturner5 | 0:cf7af2656659 | 109 | |
mturner5 | 0:cf7af2656659 | 110 | #if DEVICE_SERIAL_FC |
mturner5 | 0:cf7af2656659 | 111 | /** Set the flow control type on the serial port |
mturner5 | 0:cf7af2656659 | 112 | * |
mturner5 | 0:cf7af2656659 | 113 | * @param type the flow control type (Disabled, RTS, CTS, RTSCTS) |
mturner5 | 0:cf7af2656659 | 114 | * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS) |
mturner5 | 0:cf7af2656659 | 115 | * @param flow2 the second flow control pin (CTS for RTSCTS) |
mturner5 | 0:cf7af2656659 | 116 | */ |
mturner5 | 0:cf7af2656659 | 117 | void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC); |
mturner5 | 0:cf7af2656659 | 118 | #endif |
mturner5 | 0:cf7af2656659 | 119 | |
mturner5 | 0:cf7af2656659 | 120 | static void _irq_handler(uint32_t id, SerialIrq irq_type); |
mturner5 | 0:cf7af2656659 | 121 | |
mturner5 | 0:cf7af2656659 | 122 | protected: |
mturner5 | 0:cf7af2656659 | 123 | SerialBase(PinName tx, PinName rx); |
mturner5 | 0:cf7af2656659 | 124 | virtual ~SerialBase() { |
mturner5 | 0:cf7af2656659 | 125 | } |
mturner5 | 0:cf7af2656659 | 126 | |
mturner5 | 0:cf7af2656659 | 127 | int _base_getc(); |
mturner5 | 0:cf7af2656659 | 128 | int _base_putc(int c); |
mturner5 | 0:cf7af2656659 | 129 | |
mturner5 | 0:cf7af2656659 | 130 | serial_t _serial; |
mturner5 | 0:cf7af2656659 | 131 | FunctionPointer _irq[2]; |
mturner5 | 0:cf7af2656659 | 132 | int _baud; |
mturner5 | 0:cf7af2656659 | 133 | }; |
mturner5 | 0:cf7af2656659 | 134 | |
mturner5 | 0:cf7af2656659 | 135 | } // namespace mbed |
mturner5 | 0:cf7af2656659 | 136 | |
mturner5 | 0:cf7af2656659 | 137 | #endif |
mturner5 | 0:cf7af2656659 | 138 | |
mturner5 | 0:cf7af2656659 | 139 | #endif |