Exploring BeagleBone  V1.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PWM.h
Go to the documentation of this file.
1 /*
2  * PWM.h Created on: 29 Apr 2014
3  * Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie)
4  * Made available for the book "Exploring BeagleBone"
5  * See: www.exploringbeaglebone.com
6  * Licensed under the EUPL V.1.1
7  *
8  * This Software is provided to You under the terms of the European
9  * Union Public License (the "EUPL") version 1.1 as published by the
10  * European Union. Any use of this Software, other than as authorized
11  * under this License is strictly prohibited (to the extent such use
12  * is covered by a right of the copyright holder of this Software).
13  *
14  * This Software is provided under the License on an "AS IS" basis and
15  * without warranties of any kind concerning the Software, including
16  * without limitation merchantability, fitness for a particular purpose,
17  * absence of defects or errors, accuracy, and non-infringement of
18  * intellectual property rights other than copyright. This disclaimer
19  * of warranty is an essential part of the License and a condition for
20  * the grant of any rights to this Software.
21  *
22  * For more details, see http://www.derekmolloy.ie/
23  */
24 
25 #ifndef PWM_H_
26 #define PWM_H_
27 #include<string>
28 using std::string;
29 
30 #define PWM_PATH "/sys/devices/ocp.3/"
31 #define PWM_PERIOD "period"
32 #define PWM_DUTY "duty"
33 #define PWM_POLARITY "polarity"
34 #define PWM_RUN "run"
35 
36 namespace exploringBB {
37 
43 class PWM {
44 public:
46 
47 private:
48  string name, path;
49  float analogFrequency; //defaults to 100,000 Hz
50  float analogMax; //defaults to 3.3V
51 
52 public:
53  PWM(string pinName);
54 
55  virtual int setPeriod(unsigned int period_ns);
56  virtual unsigned int getPeriod();
57  virtual int setFrequency(float frequency_hz);
58  virtual float getFrequency();
59  virtual int setDutyCycle(unsigned int duration_ns);
60  virtual int setDutyCycle(float percentage);
61  virtual unsigned int getDutyCycle();
62  virtual float getDutyCyclePercent();
63 
64  virtual int setPolarity(PWM::POLARITY);
65  virtual void invertPolarity();
66  virtual PWM::POLARITY getPolarity();
67 
68  virtual void setAnalogFrequency(float frequency_hz) { this->analogFrequency = frequency_hz; }
69  virtual int calibrateAnalogMax(float analogMax); //must be between 3.2 and 3.4
70  virtual int analogWrite(float voltage);
71 
72  virtual int run();
73  virtual bool isRunning();
74  virtual int stop();
75 
76  virtual ~PWM();
77 private:
78  float period_nsToFrequency(unsigned int);
79  unsigned int frequencyToPeriod_ns(float);
80 };
81 
82 } /* namespace exploringBB */
83 
84 #endif /* PWM_H_ */
virtual void invertPolarity()
Definition: PWM.cpp:90
virtual float getFrequency()
Definition: PWM.cpp:60
virtual ~PWM()
Definition: PWM.cpp:127
virtual PWM::POLARITY getPolarity()
Definition: PWM.cpp:95
A class to control a basic PWM output – you must know the exact sysfs filename for the PWM output...
Definition: PWM.h:43
virtual int stop()
Definition: PWM.cpp:123
virtual void setAnalogFrequency(float frequency_hz)
Definition: PWM.h:68
virtual int setDutyCycle(unsigned int duration_ns)
Definition: PWM.cpp:64
virtual int setFrequency(float frequency_hz)
Definition: PWM.cpp:56
virtual unsigned int getPeriod()
Definition: PWM.cpp:42
virtual int setPeriod(unsigned int period_ns)
Definition: PWM.cpp:38
Definition: PWM.h:45
virtual unsigned int getDutyCycle()
Definition: PWM.cpp:76
Definition: BusDevice.cpp:27
virtual int analogWrite(float voltage)
Definition: PWM.cpp:106
virtual int run()
Definition: PWM.cpp:114
virtual float getDutyCyclePercent()
Definition: PWM.cpp:80
virtual int setPolarity(PWM::POLARITY)
Definition: PWM.cpp:86
PWM(string pinName)
Definition: PWM.cpp:31
virtual int calibrateAnalogMax(float analogMax)
Definition: PWM.cpp:100
virtual bool isRunning()
Definition: PWM.cpp:118
POLARITY
Definition: PWM.h:45
Definition: PWM.h:45