Basic fading in and out of leds with light sensor.

Dependencies:   mbed

Committer:
mturner5
Date:
Mon Sep 19 03:24:58 2016 +0000
Revision:
0:cf7af2656659
Basic v1

Who changed what in which revision?

UserRevisionLine numberNew 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_SPISLAVE_H
mturner5 0:cf7af2656659 17 #define MBED_SPISLAVE_H
mturner5 0:cf7af2656659 18
mturner5 0:cf7af2656659 19 #include "platform.h"
mturner5 0:cf7af2656659 20
mturner5 0:cf7af2656659 21 #if DEVICE_SPISLAVE
mturner5 0:cf7af2656659 22
mturner5 0:cf7af2656659 23 #include "spi_api.h"
mturner5 0:cf7af2656659 24
mturner5 0:cf7af2656659 25 namespace mbed {
mturner5 0:cf7af2656659 26
mturner5 0:cf7af2656659 27 /** A SPI slave, used for communicating with a SPI Master device
mturner5 0:cf7af2656659 28 *
mturner5 0:cf7af2656659 29 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
mturner5 0:cf7af2656659 30 *
mturner5 0:cf7af2656659 31 * Example:
mturner5 0:cf7af2656659 32 * @code
mturner5 0:cf7af2656659 33 * // Reply to a SPI master as slave
mturner5 0:cf7af2656659 34 *
mturner5 0:cf7af2656659 35 * #include "mbed.h"
mturner5 0:cf7af2656659 36 *
mturner5 0:cf7af2656659 37 * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
mturner5 0:cf7af2656659 38 *
mturner5 0:cf7af2656659 39 * int main() {
mturner5 0:cf7af2656659 40 * device.reply(0x00); // Prime SPI with first reply
mturner5 0:cf7af2656659 41 * while(1) {
mturner5 0:cf7af2656659 42 * if(device.receive()) {
mturner5 0:cf7af2656659 43 * int v = device.read(); // Read byte from master
mturner5 0:cf7af2656659 44 * v = (v + 1) % 0x100; // Add one to it, modulo 256
mturner5 0:cf7af2656659 45 * device.reply(v); // Make this the next reply
mturner5 0:cf7af2656659 46 * }
mturner5 0:cf7af2656659 47 * }
mturner5 0:cf7af2656659 48 * }
mturner5 0:cf7af2656659 49 * @endcode
mturner5 0:cf7af2656659 50 */
mturner5 0:cf7af2656659 51 class SPISlave {
mturner5 0:cf7af2656659 52
mturner5 0:cf7af2656659 53 public:
mturner5 0:cf7af2656659 54
mturner5 0:cf7af2656659 55 /** Create a SPI slave connected to the specified pins
mturner5 0:cf7af2656659 56 *
mturner5 0:cf7af2656659 57 * Pin Options:
mturner5 0:cf7af2656659 58 * (5, 6, 7i, 8) or (11, 12, 13, 14)
mturner5 0:cf7af2656659 59 *
mturner5 0:cf7af2656659 60 * mosi or miso can be specfied as NC if not used
mturner5 0:cf7af2656659 61 *
mturner5 0:cf7af2656659 62 * @param mosi SPI Master Out, Slave In pin
mturner5 0:cf7af2656659 63 * @param miso SPI Master In, Slave Out pin
mturner5 0:cf7af2656659 64 * @param sclk SPI Clock pin
mturner5 0:cf7af2656659 65 * @param ssel SPI chip select pin
mturner5 0:cf7af2656659 66 * @param name (optional) A string to identify the object
mturner5 0:cf7af2656659 67 */
mturner5 0:cf7af2656659 68 SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
mturner5 0:cf7af2656659 69
mturner5 0:cf7af2656659 70 /** Configure the data transmission format
mturner5 0:cf7af2656659 71 *
mturner5 0:cf7af2656659 72 * @param bits Number of bits per SPI frame (4 - 16)
mturner5 0:cf7af2656659 73 * @param mode Clock polarity and phase mode (0 - 3)
mturner5 0:cf7af2656659 74 *
mturner5 0:cf7af2656659 75 * @code
mturner5 0:cf7af2656659 76 * mode | POL PHA
mturner5 0:cf7af2656659 77 * -----+--------
mturner5 0:cf7af2656659 78 * 0 | 0 0
mturner5 0:cf7af2656659 79 * 1 | 0 1
mturner5 0:cf7af2656659 80 * 2 | 1 0
mturner5 0:cf7af2656659 81 * 3 | 1 1
mturner5 0:cf7af2656659 82 * @endcode
mturner5 0:cf7af2656659 83 */
mturner5 0:cf7af2656659 84 void format(int bits, int mode = 0);
mturner5 0:cf7af2656659 85
mturner5 0:cf7af2656659 86 /** Set the spi bus clock frequency
mturner5 0:cf7af2656659 87 *
mturner5 0:cf7af2656659 88 * @param hz SCLK frequency in hz (default = 1MHz)
mturner5 0:cf7af2656659 89 */
mturner5 0:cf7af2656659 90 void frequency(int hz = 1000000);
mturner5 0:cf7af2656659 91
mturner5 0:cf7af2656659 92 /** Polls the SPI to see if data has been received
mturner5 0:cf7af2656659 93 *
mturner5 0:cf7af2656659 94 * @returns
mturner5 0:cf7af2656659 95 * 0 if no data,
mturner5 0:cf7af2656659 96 * 1 otherwise
mturner5 0:cf7af2656659 97 */
mturner5 0:cf7af2656659 98 int receive(void);
mturner5 0:cf7af2656659 99
mturner5 0:cf7af2656659 100 /** Retrieve data from receive buffer as slave
mturner5 0:cf7af2656659 101 *
mturner5 0:cf7af2656659 102 * @returns
mturner5 0:cf7af2656659 103 * the data in the receive buffer
mturner5 0:cf7af2656659 104 */
mturner5 0:cf7af2656659 105 int read(void);
mturner5 0:cf7af2656659 106
mturner5 0:cf7af2656659 107 /** Fill the transmission buffer with the value to be written out
mturner5 0:cf7af2656659 108 * as slave on the next received message from the master.
mturner5 0:cf7af2656659 109 *
mturner5 0:cf7af2656659 110 * @param value the data to be transmitted next
mturner5 0:cf7af2656659 111 */
mturner5 0:cf7af2656659 112 void reply(int value);
mturner5 0:cf7af2656659 113
mturner5 0:cf7af2656659 114 protected:
mturner5 0:cf7af2656659 115 spi_t _spi;
mturner5 0:cf7af2656659 116
mturner5 0:cf7af2656659 117 int _bits;
mturner5 0:cf7af2656659 118 int _mode;
mturner5 0:cf7af2656659 119 int _hz;
mturner5 0:cf7af2656659 120 };
mturner5 0:cf7af2656659 121
mturner5 0:cf7af2656659 122 } // namespace mbed
mturner5 0:cf7af2656659 123
mturner5 0:cf7af2656659 124 #endif
mturner5 0:cf7af2656659 125
mturner5 0:cf7af2656659 126 #endif