maa: change complex C++ write calls to CamelCase for spi & i2c
[contrib/mraa.git] / api / mraa / spi.hpp
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #pragma once
26
27 #include "spi.h"
28
29 namespace mraa {
30
31 /**
32  * @brief C++ API to System Packet Interface
33  *
34  * This file defines the SPI C++ interface for libmraa
35  *
36  * @snippet Spi-pot.cpp Interesting
37  */
38 class Spi {
39     public:
40         /**
41          * Initialise SPI object using the board mapping to set muxes
42          *
43          * @param bus to use, as listed in the platform definition, normally 0
44          */
45         Spi(int bus) {
46             m_spi = mraa_spi_init(bus);
47         }
48         /**
49          * Closes spi bus
50          */
51         ~Spi() {
52             mraa_spi_stop(m_spi);
53         }
54         /**
55          * Set the SPI device mode. see spidev0-3
56          *
57          * @param mode the mode. See Linux spidev doc
58          * @return Result of operation
59          */
60         mraa_result_t mode(unsigned short mode) {
61             return mraa_spi_mode(m_spi, mode);
62         }
63         /**
64          * Set the SPI device operating clock frequency
65          *
66          * @param hz the frequency to set in hz
67          * @return Result of operation
68          */
69         mraa_result_t frequency(int hz) {
70             return mraa_spi_frequency(m_spi, hz);
71         }
72         /**
73          * Write single byte to the SPI device
74          *
75          * @param data the byte to send
76          * @return data received on the miso line
77          */
78         unsigned char write(uint8_t data) {
79             return (unsigned char) mraa_spi_write(m_spi, data);
80         }
81         /**
82          * Write buffer of bytes to SPI device
83          *
84          * @param data buffer to send
85          * @param length size of buffer to send
86          * @return char* data received on the miso line. Same length as passed in
87          */
88         unsigned char* write(uint8_t* data, int length) {
89             return (unsigned char*) mraa_spi_write_buf(m_spi, data, length);
90         }
91         /**
92          * Change the SPI lsb mode
93          *
94          * @param lsb Use least significant bit transmission - 0 for msbi
95          * @return Result of operation
96          */
97         mraa_result_t lsbmode(bool lsb) {
98             return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
99         }
100         /**
101          * Set bits per mode on transaction, default is 8
102          *
103          * @param bits bits per word
104          * @return Result of operation
105          */
106         mraa_result_t bitPerWord(unsigned int bits) {
107             return mraa_spi_bit_per_word(m_spi, bits);
108         }
109             private:
110                 mraa_spi_context m_spi;
111         };
112 }