@@ -24,26 +24,6 @@ I2C i2c(PICO_EXPLORER);
24
24
BreakoutPotentiometer pot (&i2c);
25
25
bool toggle = false ;
26
26
27
- // HSV Conversion expects float inputs in the range of 0.00-1.00 for each channel
28
- // Outputs are rgb in the range 0-255 for each channel
29
- void from_hsv (float h, float s, float v, uint8_t &r, uint8_t &g, uint8_t &b) {
30
- float i = floor (h * 6 .0f );
31
- float f = h * 6 .0f - i;
32
- v *= 255 .0f ;
33
- uint8_t p = v * (1 .0f - s);
34
- uint8_t q = v * (1 .0f - f * s);
35
- uint8_t t = v * (1 .0f - (1 .0f - f) * s);
36
-
37
- switch (int (i) % 6 ) {
38
- case 0 : r = v; g = t; b = p; break ;
39
- case 1 : r = q; g = v; b = p; break ;
40
- case 2 : r = p; g = v; b = t; break ;
41
- case 3 : r = p; g = q; b = v; break ;
42
- case 4 : r = t; g = p; b = v; break ;
43
- case 5 : r = v; g = p; b = q; break ;
44
- }
45
- }
46
-
47
27
int main () {
48
28
#ifdef PICO_DEFAULT_LED_PIN
49
29
gpio_init (PICO_DEFAULT_LED_PIN);
@@ -66,9 +46,8 @@ int main() {
66
46
float percent = pot.read ();
67
47
68
48
printf (" Percent: %d\n " , (int )(percent * 100 ));
69
- uint8_t r = 0 , g = 0 , b = 0 ;
70
- from_hsv (percent, 1 .0f , 1 .0f , r, g, b);
71
- pot.set_led (r, g, b);
49
+ RGB p = RGB::from_hsv (percent, 1 .0f , 1 .0f );
50
+ pot.set_led (p.r , p.g , p.b );
72
51
73
52
graphics.set_pen (BLACK);
74
53
graphics.clear ();
@@ -77,7 +56,7 @@ int main() {
77
56
graphics.set_pen (RED);
78
57
std::ostringstream ss;
79
58
ss << " R = " ;
80
- ss << (int )r ;
59
+ ss << (int )(p. r ) ;
81
60
std::string s (ss.str ());
82
61
graphics.text (s, Point (10 , 10 ), 220 , 6 );
83
62
}
@@ -86,7 +65,7 @@ int main() {
86
65
graphics.set_pen (GREEN);
87
66
std::ostringstream ss;
88
67
ss << " G = " ;
89
- ss << (int )g ;
68
+ ss << (int )(p. g ) ;
90
69
std::string s (ss.str ());
91
70
graphics.text (s, Point (10 , 70 ), 220 , 6 );
92
71
}
@@ -95,20 +74,20 @@ int main() {
95
74
graphics.set_pen (BLUE);
96
75
std::ostringstream ss;
97
76
ss << " B = " ;
98
- ss << (int )b ;
77
+ ss << (int )(p. b ) ;
99
78
std::string s (ss.str ());
100
79
graphics.text (s, Point (10 , 130 ), 220 , 6 );
101
80
}
102
81
103
82
{
104
83
// Shouldn't really use create_pen in-line.
105
84
// In default (RGB332) palette mode this will lookup the nearest 8-bit colour
106
- graphics.set_pen (graphics.create_pen (r, g, b));
85
+ graphics.set_pen (graphics.create_pen (p. r , p. g , p. b ));
107
86
std::ostringstream ss;
108
87
ss << " #" ;
109
- ss << std::uppercase << std::hex << std::setfill (' 0' ) << std::setw (2 ) << (int )r ;
110
- ss << std::uppercase << std::hex << std::setfill (' 0' ) << std::setw (2 ) << (int )g ;
111
- ss << std::uppercase << std::hex << std::setfill (' 0' ) << std::setw (2 ) << (int )b ;
88
+ ss << std::uppercase << std::hex << std::setfill (' 0' ) << std::setw (2 ) << (int )(p. r ) ;
89
+ ss << std::uppercase << std::hex << std::setfill (' 0' ) << std::setw (2 ) << (int )(p. g ) ;
90
+ ss << std::uppercase << std::hex << std::setfill (' 0' ) << std::setw (2 ) << (int )(p. b ) ;
112
91
std::string s (ss.str ());
113
92
graphics.text (s, Point (10 , 190 ), 220 , 5 );
114
93
}
0 commit comments