Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
MCP4561.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 21.09.2013 12:48:23 CEST
3 // File: MCP4561.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __MCP4561_HEADER
12 #define __MCP4561_HEADER
13 
14 #include "../com/i2c.hpp"
15 
16 /*
17 
18 MCP Pins Arduino Pins
19 1 adr mod GND, VCC or nothing (then it's the same as VCC)
20 2 SCL A5
21 3 SDA A4
22 4 GND GND
23 5 Poti A GND or other GND <= X <= VCC
24 6 Wimper 0
25 7 Poti B VCC or other GND <= X <= VCC
26 8 VCC VCC
27 
28 */
29 
30 namespace device {
31  class MCP4561_class {
32  enum{write = 0, incr = 1, decr = 2, read = 3};
33  enum{ volatile_wimper_0 = 0x00
34  , volatile_wimper_1 = 0x01
35  , eeprom_wimper_0 = 0x02
36  , eeprom_wimper_1 = 0x03
37  , tcon_register = 0x04
38  , status_register = 0x05
39  };
40  public:
41  //------------------- ctors -------------------
42  MCP4561_class(uint8_t adr = 46): dev_adr_(adr) { // ard = 46 + adr_mod
43  }
44 
45  uint8_t cmd(uint8_t const & reg, uint8_t const & _cmd) const {
46  return (reg << 4) | (_cmd << 2);
47  }
48 
49  operator uint8_t() const {
50  uint16_t res = 0;
51  com::i2cout(dev_adr_) << cmd(volatile_wimper_0, read) << ustd::endl(false);
52  com::i2cin(dev_adr_) >> res;
53  return (res >> 8);
54  }
55  MCP4561_class & operator=(uint8_t const & val) {
56  com::i2cout(dev_adr_) << cmd(volatile_wimper_0, write) << val << ustd::endl;
57  return (*this);
58  }
60  if((*this) < 255)
61  com::i2cout(dev_adr_) << cmd(volatile_wimper_0, incr) << ustd::endl;
62  return (*this);
63  }
65  com::i2cout(dev_adr_) << cmd(volatile_wimper_0, decr) << ustd::endl;
66  return (*this);
67  }
68  void save() {
69  uint8_t val = uint8_t((*this)); //read volatile register
70  com::i2cout(dev_adr_) << cmd(eeprom_wimper_0, write) << val << ustd::endl; //and write it to eeprom
71  }
72  private:
73  uint8_t dev_adr_;
74  };
75 
76 }//end namespace device
77 
78 #endif //__MCP4561_HEADER
class com::i2cout_class i2cout
MCP4561_class(uint8_t adr=46)
Definition: MCP4561.hpp:42
MCP4561_class & operator--()
Definition: MCP4561.hpp:64
void save()
Definition: MCP4561.hpp:68
uint8_t cmd(uint8_t const &reg, uint8_t const &_cmd) const
Definition: MCP4561.hpp:45
MCP4561_class & operator=(uint8_t const &val)
Definition: MCP4561.hpp:55
struct ustd::endl_class endl
MCP4561_class & operator++()
Definition: MCP4561.hpp:59
Definition: MCP4561.hpp:31
class com::i2cin_class i2cin