-
Notifications
You must be signed in to change notification settings - Fork 10
/
RMTT_RGB.cpp
68 lines (58 loc) · 1.87 KB
/
RMTT_RGB.cpp
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
/*
* Copyright (C) 2020 DJI.
*
* SPDX-License-Identifier: BSD-3-Clause-Clear
*
* Change Logs:
* Date Author Notes
* 2020-08-25 robomaster first version
*/
#include "Arduino.h"
#include "RMTT_RGB.h"
// #define __RMTT_RGB_DEBUG__
void RMTT_RGB::Init(void)
{
// Setup timer and attach timer to a led pin
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcSetup(LEDC_CHANNEL_1, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcSetup(LEDC_CHANNEL_2, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcAttachPin(LED_PIN_R, LEDC_CHANNEL_0);
ledcAttachPin(LED_PIN_G, LEDC_CHANNEL_1);
ledcAttachPin(LED_PIN_B, LEDC_CHANNEL_2);
#ifdef __RMTT_RGB_DEBUG__
Serial.println("LEDC Init Successful!");
#endif
}
void RMTT_RGB::SetRed(uint32_t val, uint32_t valueMax)
{
LEDCAnalogWrite(LEDC_CHANNEL_0, val, valueMax);
LEDCAnalogWrite(LEDC_CHANNEL_1, 0, 255);
LEDCAnalogWrite(LEDC_CHANNEL_2, 0, 255);
}
void RMTT_RGB::SetGreen(uint32_t val, uint32_t valueMax)
{
LEDCAnalogWrite(LEDC_CHANNEL_1, val, valueMax);
LEDCAnalogWrite(LEDC_CHANNEL_0, 0, 255);
LEDCAnalogWrite(LEDC_CHANNEL_2, 0, 255);
}
void RMTT_RGB::SetBlue(uint32_t val, uint32_t valueMax)
{
LEDCAnalogWrite(LEDC_CHANNEL_2, val, valueMax);
LEDCAnalogWrite(LEDC_CHANNEL_0, 0, 255);
LEDCAnalogWrite(LEDC_CHANNEL_1, 0, 255);
}
void RMTT_RGB::SetRGB(uint32_t R, uint32_t G, uint32_t B, uint32_t valueMax)
{
LEDCAnalogWrite(LEDC_CHANNEL_0, R, valueMax);
LEDCAnalogWrite(LEDC_CHANNEL_1, G, valueMax);
LEDCAnalogWrite(LEDC_CHANNEL_2, B, valueMax);
}
// Arduino like analogWrite
// value has to be between 0 and valueMax
void RMTT_RGB::LEDCAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax)
{
// calculate duty, 8191 from 2 ^ 13 - 1
uint32_t duty = (8191 / valueMax) * min(value, valueMax);
// write duty to LEDC
ledcWrite(channel, duty);
}