mraa  0.4.5
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
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 
38 class Spi {
39  public:
45  Spi(int bus) {
46  m_spi = mraa_spi_init(bus);
47  }
51  ~Spi() {
52  mraa_spi_stop(m_spi);
53  }
61  return mraa_spi_mode(m_spi, mode);
62  }
70  return mraa_spi_frequency(m_spi, hz);
71  }
78  char write(char data) {
79  return (char) mraa_spi_write(m_spi, (uint8_t) data);
80  }
88  char* write(char* data, size_t length) {
89  return (char*) mraa_spi_write_buf(m_spi, (uint8_t *) data, (int) length);
90  }
97  mraa_result_t lsbmode(bool lsb) {
98  return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
99  }
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 }
unsigned int mraa_boolean_t
Definition: common.h:42
mraa_result_t frequency(int hz)
Definition: spi.hpp:69
mraa_result_t mraa_spi_lsbmode(mraa_spi_context dev, mraa_boolean_t lsb)
mraa_result_t mraa_spi_frequency(mraa_spi_context dev, int hz)
char write(char data)
Definition: spi.hpp:78
mraa_result_t mraa_spi_bit_per_word(mraa_spi_context dev, unsigned int bits)
struct _spi * mraa_spi_context
Definition: spi.h:59
System Packet Interface.
char * write(char *data, size_t length)
Definition: spi.hpp:88
mraa_result_t mode(mraa_spi_mode_t mode)
Definition: spi.hpp:60
mraa_result_t mraa_spi_stop(mraa_spi_context dev)
mraa_spi_mode_t
Definition: spi.h:49
mraa_result_t bitPerWord(unsigned int bits)
Definition: spi.hpp:106
Spi(int bus)
Definition: spi.hpp:45
mraa_result_t lsbmode(bool lsb)
Definition: spi.hpp:97
mraa_spi_context mraa_spi_init(int bus)
uint8_t mraa_spi_write(mraa_spi_context dev, uint8_t data)
mraa_result_t
Definition: types.h:49
uint8_t * mraa_spi_write_buf(mraa_spi_context dev, uint8_t *data, int length)
mraa_result_t mraa_spi_mode(mraa_spi_context dev, mraa_spi_mode_t mode)
API to System Packet Interface.
Definition: spi.hpp:38
~Spi()
Definition: spi.hpp:51