Open Lighting Architecture
 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 
33 using std::string;
34 
39  public:
40  virtual ~SPIWriterInterface() {}
41 
42  virtual string DevicePath() const = 0;
43  virtual bool Init() = 0;
44  virtual bool WriteSPIData(const uint8_t *data, unsigned int length) = 0;
45 };
46 
50 class SPIWriter : public SPIWriterInterface {
51  public:
55  struct Options {
56  uint32_t spi_speed;
57  bool cs_enable_high;
58 
59  Options() : spi_speed(1000000), cs_enable_high(false) {}
60  };
61 
62  SPIWriter(const string &spi_device, const Options &options,
63  ExportMap *export_map);
64  ~SPIWriter();
65 
66  string DevicePath() const { return m_device_path; }
67 
72  bool Init();
73 
74  bool WriteSPIData(const uint8_t *data, unsigned int length);
75 
76  private:
77  const string m_device_path;
78  const uint32_t m_spi_speed;
79  const bool m_cs_enable_high;
80  int m_fd;
81  UIntMap *m_error_map_var;
82  UIntMap *m_write_map_var;
83 
84  static const uint8_t SPI_MODE;
85  static const uint8_t SPI_BITS_PER_WORD;
86  static const char SPI_DEVICE_KEY[];
87  static const char SPI_ERROR_VAR[];
88  static const char SPI_WRITE_VAR[];
89 };
90 } // namespace spi
91 } // namespace plugin
92 } // namespace ola
93 #endif // PLUGINS_SPI_SPIWRITER_H_