Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SPIWriter.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * SPIWriter.h
17  * This writes data to a SPI device.
18  * Copyright (C) 2013 Simon Newton
19  */
20 
21 #ifndef PLUGINS_SPI_SPIWRITER_H_
22 #define PLUGINS_SPI_SPIWRITER_H_
23 
24 #include <ola/ExportMap.h>
25 #include <ola/thread/Mutex.h>
26 #include <stdint.h>
27 #include <string>
28 
29 namespace ola {
30 namespace plugin {
31 namespace spi {
32 
37  public:
38  virtual ~SPIWriterInterface() {}
39 
40  virtual std::string DevicePath() const = 0;
41  virtual bool Init() = 0;
42  virtual bool WriteSPIData(const uint8_t *data, unsigned int length) = 0;
43 };
44 
48 class SPIWriter : public SPIWriterInterface {
49  public:
53  struct Options {
54  uint32_t spi_speed;
55  bool cs_enable_high;
56 
57  Options() : spi_speed(1000000), cs_enable_high(false) {}
58  };
59 
60  SPIWriter(const std::string &spi_device, const Options &options,
61  ExportMap *export_map);
62  ~SPIWriter();
63 
64  std::string DevicePath() const { return m_device_path; }
65 
70  bool Init();
71 
72  bool WriteSPIData(const uint8_t *data, unsigned int length);
73 
74  private:
75  const std::string m_device_path;
76  const uint32_t m_spi_speed;
77  const bool m_cs_enable_high;
78  int m_fd;
79  UIntMap *m_error_map_var;
80  UIntMap *m_write_map_var;
81 
82  static const uint8_t SPI_MODE;
83  static const uint8_t SPI_BITS_PER_WORD;
84  static const char SPI_DEVICE_KEY[];
85  static const char SPI_ERROR_VAR[];
86  static const char SPI_WRITE_VAR[];
87 };
88 } // namespace spi
89 } // namespace plugin
90 } // namespace ola
91 #endif // PLUGINS_SPI_SPIWRITER_H_