Project

General

Profile

Raspberry PI GPIO Scol plugin
rpiPCA9685.cpp
1#include "rpiPCA9685.h"
2#include "plugin.h"
3
4#include <string>
5#include <algorithm>
6#include <iostream>
7
8#ifdef RPI
9#include <wiringPi.h>
10#include <pca9685.h>
11#endif
12
13//
14//Class RpiPCA9685
15//
16
17RpiPCA9685::RpiPCA9685(int deviceId, int pinBase, int frequency):
18 mDeviceId(deviceId),
19 mPinBase(pinBase)
20{
21 mFileHandler = 0;
22#ifdef RPI
23 mFileHandler = pca9685Setup(pinBase, deviceId, frequency);
24#endif
25}
26
27RpiPCA9685::~RpiPCA9685()
28{
29#ifdef RPI
30 if (mFileHandler != 0)
31 {
32 Reset();
33 pca9685Unset(mFileHandler, mPinBase);
34 }
35#endif
36}
37
38int RpiPCA9685::PwmRead(int pin)
39{
40#ifdef RPI
41 if (mFileHandler != 0)
42 {
43 int on;
44 pca9685PWMRead(mFileHandler, pin, &on, 0);
45 return on;
46 }
47#endif
48 return 0;
49}
50
51void RpiPCA9685::PwmWrite(int pin, int value)
52{
53#ifdef RPI
54 if (mFileHandler != 0)
55 {
56 if (value >= 4096)
57 pca9685FullOn(mFileHandler, pin, 1);
58 else if (value > 0)
59 pca9685PWMWrite(mFileHandler, pin, 0, value); // (Deactivates full-on and off by itself)
60 else
61 pca9685FullOff(mFileHandler, pin, 1);
62 }
63#endif
64}
65
66void RpiPCA9685::Reset()
67{
68#ifdef RPI
69 if (mFileHandler != 0)
70 {
71 pca9685PWMReset(mFileHandler);
72 }
73#endif
74}
75
76void RpiPCA9685::SetFrequency(int frequency)
77{
78#ifdef RPI
79 if (mFileHandler != 0)
80 {
81 pca9685PWMFreq(mFileHandler, frequency);
82 }
83#endif
84}
85
86void RpiPCA9685::FullOn(int pin)
87{
88#ifdef RPI
89 if (mFileHandler != 0)
90 {
91 pca9685FullOn(mFileHandler, pin, 1);
92 }
93#endif
94}
95
96void RpiPCA9685::FullOff(int pin)
97{
98#ifdef RPI
99 if (mFileHandler != 0)
100 {
101 pca9685FullOff(mFileHandler, pin, 1);
102 }
103#endif
104}