1 /*
2 * Author: Jon Trulson <jtrulson@ics.com>
3 * Copyright (c) 2015 Intel Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <iostream>
26 #include <string>
27 #include <stdexcept>
28
29 #include "cjq4435.h"
30
31 using namespace upm;
32 using namespace std;
33
CJQ4435(int pin)34 CJQ4435::CJQ4435(int pin)
35 {
36 if ( !(m_pwm = mraa_pwm_init(pin)) )
37 {
38 throw std::invalid_argument(std::string(__FUNCTION__) +
39 ": mraa_pwm_init() failed, invalid pin?");
40 return;
41 }
42
43 m_enabled = false;
44 }
45
~CJQ4435()46 CJQ4435::~CJQ4435()
47 {
48 if (m_enabled)
49 mraa_pwm_enable(m_pwm, 0);
50
51 mraa_pwm_close(m_pwm);
52 }
53
setPeriodUS(int us)54 void CJQ4435::setPeriodUS(int us)
55 {
56 if (mraa_pwm_period_us(m_pwm, us) != MRAA_SUCCESS)
57 cerr << __FUNCTION__ << ": period specified is not supported"
58 << endl;
59 }
60
setPeriodMS(int ms)61 void CJQ4435::setPeriodMS(int ms)
62 {
63 if (mraa_pwm_period_ms(m_pwm, ms) != MRAA_SUCCESS)
64 cerr << __FUNCTION__ << ": period specified is not supported"
65 << endl;
66 }
67
setPeriodSeconds(float seconds)68 void CJQ4435::setPeriodSeconds(float seconds)
69 {
70 if (mraa_pwm_period(m_pwm, seconds) != MRAA_SUCCESS)
71 cerr << __FUNCTION__ << ": period specified is not supported"
72 << endl;
73 }
74
enable(bool enable)75 void CJQ4435::enable(bool enable)
76 {
77 m_enabled = enable;
78 mraa_pwm_enable(m_pwm, ((enable) ? 1 : 0));
79 }
80
setDutyCycle(float dutyCycle)81 void CJQ4435::setDutyCycle(float dutyCycle)
82 {
83 if (dutyCycle < 0.0)
84 dutyCycle = 0.0;
85
86 if (dutyCycle > 1.0)
87 dutyCycle = 1.0;
88
89 mraa_pwm_write(m_pwm, dutyCycle);
90 }
91
on()92 void CJQ4435::on()
93 {
94 // set a 1 second period, with 100% duty cycle
95
96 enable(false);
97 setPeriodUS(1000);
98 setDutyCycle(1.0);
99 enable(true);
100 }
101
off()102 void CJQ4435::off()
103 {
104 // set a 1 second period, with 0% duty cycle
105
106 enable(false);
107 setPeriodUS(1000);
108 setDutyCycle(0.0);
109 enable(true);
110 }
111
112