-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm.h
89 lines (72 loc) · 2.03 KB
/
pwm.h
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
81
82
83
84
85
86
87
88
89
#pragma once
#include <functional>
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
#include <stdexcept>
#include <cstdlib>
#include <ctime>
#include <cstdint>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include "common.h"
namespace pwm {
class PwmOutput {
public:
virtual ~PwmOutput() {}
virtual void enable(bool en) = 0;
virtual void setPeriodNs(unsigned long int value) = 0;
virtual void setDutyCycleNs(unsigned long int value) = 0;
};
class LinuxPwmOutput : public PwmOutput {
public:
LinuxPwmOutput(const std::string& basePath, unsigned int pwmchip, unsigned int pwm);
void enable(bool en) override;
void setPeriodNs(unsigned long int value) override;
void setDutyCycleNs(unsigned long int value) override;
private:
void exportPwm();
void setProperty(const std::string& prop, const std::string& value);
private:
std::string basePath;
unsigned int pwmchip;
unsigned int pwm;
};
class PwmBeeper : public common::Beeper {
public:
using SleepFunction = std::function<void(uint16_t)>;
PwmBeeper(PwmOutput& pwmOutput, SleepFunction sleepFunction);
void setVolume(uint8_t volume) override;
uint8_t getVolume() override;
void playTone(const common::BeeperTone& tone) override;
private:
PwmOutput& pwmOutput;
SleepFunction sleepFunction;
uint8_t volume{50};
};
class PwmRgbLed : public common::RgbLight {
public:
PwmRgbLed(PwmOutput& pwmRed, PwmOutput& pwmGreen, PwmOutput& pwmBlue);
/**
* This maximum is due to wrong hardware dimensioning (LEDs get too hot).
*/
static constexpr double DUTY_CYCLE_MAX_RATIO{0.8};
/**
* For LEDs the period may remain constant.
* It is fine-tuned in order to avoid audible crosstalk effects.
*/
unsigned long getDefaultPeriodNs();
void set(common::LightSetting value);
common::LightSetting get();
private:
void setChannel(PwmOutput& pwmOutput, unsigned long value);
void setChannelDefaults(PwmOutput& pwmOutput);
private:
common::LightSetting bufferedSetting;
PwmOutput& pwmRed;
PwmOutput& pwmGreen;
PwmOutput& pwmBlue;
};
} // namespace pwm