DS1808LC: Add string based cons for Controller

Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
Adelin Dobre 2018-10-31 16:49:05 +02:00 committed by Stefan Andritoiu
parent 2bb89d47ca
commit 5837c2ecf7
2 changed files with 42 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include <cmath> #include <cmath>
#include "ds1808lc.hpp" #include "ds1808lc.hpp"
#include "mraa-utils.hpp" #include "mraa-utils.hpp"
#include "upm_string_parser.hpp"
#define DS1808_I2C_ADDR 0x28 #define DS1808_I2C_ADDR 0x28
#define DS1808_POT2_OFFSET 0x40 #define DS1808_POT2_OFFSET 0x40
@ -11,6 +12,10 @@
#define DS1808_LOW_VALUE 32 // Lowest pot value that the eye can differentiate from 0 #define DS1808_LOW_VALUE 32 // Lowest pot value that the eye can differentiate from 0
#define DS1808_HIGH_VALUE 51 // Highest pot value that the eye can differentiate from full #define DS1808_HIGH_VALUE 51 // Highest pot value that the eye can differentiate from full
static bool operator!(mraa::MraaIo &mraaIo)
{
return mraaIo.getMraaDescriptors() == NULL;
}
namespace upm { namespace upm {
DS1808LC::DS1808LC(int gpioPower, int i2cBus) DS1808LC::DS1808LC(int gpioPower, int i2cBus)
@ -22,8 +27,41 @@ DS1808LC::DS1808LC(int gpioPower, int i2cBus)
getBrightness(); getBrightness();
} }
DS1808LC::DS1808LC(std::string initStr) : mraaIo(initStr)
{
std::vector<std::string> upmTokens;
mraa_set_log_level(7);
if(!mraaIo.i2cs.empty()) {
i2c = &mraaIo.i2cs[0];
} else {
throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.init() failed");
}
status = i2c->address(DS1808_I2C_ADDR);
if(!mraaIo.getLeftoverStr().empty()) {
upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
}
for(std::string tok : upmTokens) {
if(tok.substr(0, 10) == "gpioPower:") {
int gpioPower = std::stoi(tok.substr(10), nullptr, 0);
pinPower = gpioPower;
}
if(tok.substr(0, 14) == "setBrightness:") {
int dutyPercent = std::stoi(tok.substr(14), nullptr, 0);
setBrightness(dutyPercent);
}
}
getBrightness();
}
DS1808LC::~DS1808LC() DS1808LC::~DS1808LC()
{ {
if(!mraaIo)
delete i2c;
} }
bool DS1808LC::isPowered() bool DS1808LC::isPowered()

View File

@ -21,9 +21,11 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#include <string>
#include "iLightController.hpp" #include "iLightController.hpp"
#include "mraa/i2c.hpp" #include "mraa/i2c.hpp"
#include "mraa/initio.hpp"
namespace upm namespace upm
{ {
@ -55,6 +57,7 @@ class DS1808LC : public upm::ILightController
{ {
public: public:
DS1808LC(int gpioPower, int i2cBus); DS1808LC(int gpioPower, int i2cBus);
DS1808LC(std::string initStr);
~DS1808LC(); ~DS1808LC();
const char* getModuleName() { return "ds1808lc"; } const char* getModuleName() { return "ds1808lc"; }
@ -72,6 +75,7 @@ private:
mraa::Result status; mraa::Result status;
mraa::I2c* i2c; mraa::I2c* i2c;
mraa::MraaIo mraaIo;
int pinPower; int pinPower;
}; };