Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAN training #2

Merged
merged 26 commits into from
Mar 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor RGB LED
  • Loading branch information
ducky64 committed Mar 6, 2017
commit 9069686ba44a1bf1cfd62de038c97e088bb7a6cb
9 changes: 0 additions & 9 deletions lab2.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ Start with this code skeleton:

#include "ledutils.h"

PwmOut ledR(P0_5);
PwmOut ledG(P0_6);
PwmOut ledB(P0_7);

DigitalOut led1(P0_3);
DigitalOut led2(P0_9);

Expand All @@ -77,11 +73,6 @@ RawSerial serial(P0_8, NC, 115200);
CAN can(RXD_PIN, TXD_PIN);

int main() {
// Start with RGB LED off
ledR = 1;
ledG = 1;
ledB = 1;

// Initialize CAN controller at 1 Mbaud
can.frequency(1000000);

Expand Down
9 changes: 0 additions & 9 deletions solutions/lab2.2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

#include "ledutils.h"

PwmOut ledR(P0_5);
PwmOut ledG(P0_6);
PwmOut ledB(P0_7);

DigitalOut led1(P0_3);
DigitalOut led2(P0_9);

Expand All @@ -16,11 +12,6 @@ RawSerial serial(P0_8, NC, 115200);
CAN can(P0_28, P0_29);

int main() {
// Start with RGB LED off
ledR = 1;
ledG = 1;
ledB = 1;

// Initialize CAN controller at 1 Mbaud
can.frequency(1000000);

Expand Down
17 changes: 2 additions & 15 deletions solutions/lab2master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

#include "ledutils.h"

PwmOut ledR(P0_5);
PwmOut ledG(P0_6);
PwmOut ledB(P0_7);
RGBPwmOut rgbLed(P0_5, P0_6, P0_7);

DigitalOut led1(P0_3);
DigitalOut led2(P0_9);
Expand All @@ -19,11 +17,6 @@ const uint16_t DEBOUNCE_TIME_MS = 50;
const uint16_t RGB_LED_UPDATE_MS = 20; // a respectable 50 Hz

int main() {
// Start with RGB LED off
ledR = 1;
ledG = 1;
ledB = 1;

// LED Blink State
int32_t blinkLengthMs;
Timer ledTimer; // used to track blink length
Expand Down Expand Up @@ -84,13 +77,7 @@ int main() {
hue += RGB_LED_UPDATE_MS * 10;
hue = hue % 36000; // 360 degree * 100

uint16_t r, g, b;
hsv_to_rgb_pwm_uint16(hue, 65535, 65535, &r, &g, &b);

// Set outputs.
ledR.pulsewidth_us((uint32_t)r * 500 / 65535);
ledG.pulsewidth_us((uint32_t)g * 500 / 65535);
ledB.pulsewidth_us((uint32_t)b * 500 / 65535);
rgbLed.hsv_uint16(hue, 65535, 65535);

// Update CAN with new hue.
uint8_t data[2];
Expand Down
9 changes: 4 additions & 5 deletions src/ledutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ void hsv_to_rgb_uint16(uint16_t h_cdeg, uint16_t s, uint16_t v,
*b_out = *b_out + m;
}

void hsv_to_rgb_pwm_uint16(uint16_t h_cdeg, uint16_t s, uint16_t v,
uint16_t *r_out, uint16_t *g_out, uint16_t *b_out) {
void RGBPwmOut::hsv_uint16(uint16_t h_cdeg, uint16_t s, uint16_t v) {
uint16_t C = (uint32_t)v * s / 65535;
int32_t h_millipct = (((int32_t)h_cdeg * 10 / 60) % 2000) - 1000; // 1000x
uint16_t X = C * (1000 - abs(h_millipct)) / 1000;
Expand Down Expand Up @@ -92,7 +91,7 @@ void hsv_to_rgb_pwm_uint16(uint16_t h_cdeg, uint16_t s, uint16_t v,
b = 65535 - b;

// Write outputs.
*r_out = r;
*g_out = g;
*b_out = b;
ledR.pulsewidth_us((uint32_t)r * PWM_PERIOD_US / 65535);
ledG.pulsewidth_us((uint32_t)r * PWM_PERIOD_US / 65535);
ledB.pulsewidth_us((uint32_t)r * PWM_PERIOD_US / 65535);
}
32 changes: 27 additions & 5 deletions src/ledutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,33 @@ void hsv_to_rgb_float(float h_deg, float s, float v,
void hsv_to_rgb_uint16(uint16_t h_cdeg, uint16_t s, uint16_t v,
uint16_t *r_out, uint16_t *g_out, uint16_t *b_out);

/* Converts H (centidegrees, [0, 36000)), S, V (in [0, 65535]),
* to R, G, B (all in [0, 65535]) PWM, accounting for polarity inversion and
* human perceived brightness
/* Common-anode RGB LED controlled by PWM, with fixed-point options.
*/
void hsv_to_rgb_pwm_uint16(uint16_t h_cdeg, uint16_t s, uint16_t v,
uint16_t *r_out, uint16_t *g_out, uint16_t *b_out);
class RGBPwmOut {
RGBPwmOut(PinName pinR, PinName pinG, PinName pinB):
ledR(pinR), ledG(pinG), ledB(pinB) {
ledR.period_us(PWM_PERIOD_US);
ledG.period_us(PWM_PERIOD_US);
ledB.period_us(PWM_PERIOD_US);

// Initialize LEDs to off
ledR.pulsewidth_us(PWM_PERIOD_US);
ledG.pulsewidth_us(PWM_PERIOD_US);
ledB.pulsewidth_us(PWM_PERIOD_US);
}

/* Writes a HSV value to the RGB LED, accounting for polarity inversion and
* human perceived brightness
* H is in centidegrees, [0, 36000), S, V are in fixed point [0, 65535].
*/
void hsv_uint16(uint16_t h_cdeg, uint16_t s, uint16_t v);

protected:
static const uint16_t PWM_PERIOD_US = 500;

PwmOut ledR;
PwmOut ledG;
PwmOut b;
};

#endif /* LEDUTILS_H_ */