initial commit
[ETG_Helmet] / SOFTWARE / UTILS / fast-gpio / src / fastpwm.cpp
1 #include <fastpwm.h>
2
3 FastPwm::FastPwm(void)
4 {
5         Reset();
6         if (strcmp(DEVICE_TYPE, "ramips") == 0) {
7                 gpio = new FastGpioOmega2();
8         } else {
9                 gpio = new FastGpioOmega();
10         }
11 }
12
13 FastPwm::FastPwm(int freq, int duty, unsigned int cycles)
14 {
15         Reset();
16         
17         // setup the pwm info
18         _SetupPeriods(freq, duty, cycles);
19
20         //Instantiate the GPIO object
21                 // object setup
22         if (strcmp(DEVICE_TYPE, "ramips") == 0) {
23                 gpio = new FastGpioOmega2();
24         } else {
25                 gpio = new FastGpioOmega();
26         }
27 }
28
29 FastPwm::~FastPwm(void)
30 {
31         // nothing for now
32 }
33
34 void FastPwm::Reset(void)
35 {
36         bSetup  = 0;
37 }
38
39
40 void FastPwm::_SetupPeriods(int frequency, int duty, unsigned int cycles)
41 {
42         double  dutyCycleInv;
43
44         // convert the datatypes
45         freq            = (double) frequency;
46         dutyCycle       = (double)duty / 100.0f;
47
48         // find the period (in ms)
49         period          = (1.0f/freq) * 10000; // 1000
50
51         // find the low and high periods based on the duty-cycle
52         periodHigh      = period * dutyCycle;
53         periodLow       = period - periodHigh; //can also be: period * (1.0f - dutyCycle);
54
55         if ( cycles != 0 ) myCycles     = cycles; else myCycles = 0;
56
57         // note that setup has occured
58         bSetup = 1;
59
60         if (verbosityLevel > 0) {
61                 printf (        "PWM Setup:: frequency = %d, duty-cycle = %d%%\nperiod = %.2f, period hi = %.2f, period lo = %.2f\n", 
62                                         frequency,
63                                         duty,
64                                         period,
65                                         periodHigh,
66                                         periodLow
67                                 );
68         }
69 }
70
71 void FastPwm::Pwm (int pinNum)
72 {
73         if (bSetup == 0) {
74                 _SetupPeriods(DEFAULT_FREQ, DEFAULT_DUTY_CYCLE, 0);
75         }
76
77         // run the pwm
78         _Pwm(pinNum);
79 }
80
81 void FastPwm::Pwm (int pinNum, int freq, int duty, unsigned int cycles)
82 {
83         _SetupPeriods(freq, duty, cycles);
84
85         // run the pwm
86         _Pwm(pinNum);
87 }
88
89 void FastPwm::_Pwm (int pinNum)
90 {
91         // set the pin to output
92         gpio->SetDirection(pinNum, 1);
93
94         // start the loop
95         while (1) {
96
97                 if ( myCycles != 0 ) myCycles --;
98                 if ( myCycles == 1 ) break;
99
100                 //// HIGH part of cycle
101                 gpio->Set(pinNum, 1);
102                 _Sleep(periodHigh);
103
104                 // LOW part of cycle
105                 gpio->Set(pinNum, 0);
106                 _Sleep(periodLow);
107         }
108 }
109
110 void FastPwm::_Sleep (double length)
111 {
112         // sleep function uses microseconds
113         int value       = (int)(length * 1000); 
114
115         usleep(value);
116 }
Contact me: dev (at) shalnoff (dot) com
PGP fingerprint: A6B8 3B23 6013 F18A 0C71 198B 83D8 C64D 917A 5717