summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/hlg150h/hlg150h.cxx
blob: bfc8cd6f0ca9fce3f15e8261b4750dbf93c9acac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <stdexcept>
#include <unistd.h>
#include "hlg150h.hpp"
#include "mraa-utils.hpp"

#define PWM_PERIOD 3333

using namespace upm;

HLG150H::HLG150H(int pinRelay, int pinPWM)
{
   int dutyPercent = 0;
   status = mraa::SUCCESS;
   this->pinRelay = pinRelay;
   isPoweredShadow = false;
   pwmBrightness = new mraa::Pwm(pinPWM);
   status = pwmBrightness->enable(true);
   status = pwmBrightness->period_us(PWM_PERIOD);
   if (status != mraa::SUCCESS)
      UPM_THROW("pwm config failed.");
   dutyPercent = getBrightness();
   isPoweredShadow = dutyPercent > 10;
}

HLG150H::~HLG150H()
{
   delete pwmBrightness;
}

void HLG150H::setPowerOn()
{
   isPoweredShadow = true;
   MraaUtils::setGpio(pinRelay, 0);
}

void HLG150H::setPowerOff()
{
   isPoweredShadow = false;
   MraaUtils::setGpio(pinRelay, 1);
}

bool HLG150H::isPowered()
{
   // Can't read GPIO state as setting in to input mode turns off relay
   // Instead we return a shadow variable
   /*
   int level;
   if (MraaUtils::getGpio(pinRelay, &level) == MRAA_SUCCESS)
      return level == 1;
   else
      return false;
   */
   return isPoweredShadow;
}


// If duty is less than 10% light will flicker
void HLG150H::setBrightness(int dutyPercent)
{
   if (dutyPercent < 10)
      dutyPercent = 10;
   int dutyUs = (PWM_PERIOD * dutyPercent) / 100;
   dutyUs = PWM_PERIOD - dutyUs;
   status = pwmBrightness->pulsewidth_us(dutyUs);
   // std::cout << "Brightness = " << dutyPercent << "%, duty = " << dutyUs << "us" << std::endl;
   if (status != mraa::SUCCESS)
      UPM_THROW("setBrightness failed");

}


int HLG150H::getBrightness()
{
   float duty = pwmBrightness->read();
   int dutyPercent = static_cast<int>(100.0 * (1.0 - duty) + 0.5);
   return dutyPercent;
}