@@ -24,26 +24,6 @@ I2C i2c(PICO_EXPLORER);
2424BreakoutPotentiometer pot (&i2c);
2525bool toggle = false ;
2626
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-
4727int main () {
4828#ifdef PICO_DEFAULT_LED_PIN
4929 gpio_init (PICO_DEFAULT_LED_PIN);
@@ -66,9 +46,8 @@ int main() {
6646 float percent = pot.read ();
6747
6848 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 );
7251
7352 graphics.set_pen (BLACK);
7453 graphics.clear ();
@@ -77,7 +56,7 @@ int main() {
7756 graphics.set_pen (RED);
7857 std::ostringstream ss;
7958 ss << " R = " ;
80- ss << (int )r ;
59+ ss << (int )(p. r ) ;
8160 std::string s (ss.str ());
8261 graphics.text (s, Point (10 , 10 ), 220 , 6 );
8362 }
@@ -86,7 +65,7 @@ int main() {
8665 graphics.set_pen (GREEN);
8766 std::ostringstream ss;
8867 ss << " G = " ;
89- ss << (int )g ;
68+ ss << (int )(p. g ) ;
9069 std::string s (ss.str ());
9170 graphics.text (s, Point (10 , 70 ), 220 , 6 );
9271 }
@@ -95,20 +74,20 @@ int main() {
9574 graphics.set_pen (BLUE);
9675 std::ostringstream ss;
9776 ss << " B = " ;
98- ss << (int )b ;
77+ ss << (int )(p. b ) ;
9978 std::string s (ss.str ());
10079 graphics.text (s, Point (10 , 130 ), 220 , 6 );
10180 }
10281
10382 {
10483 // Shouldn't really use create_pen in-line.
10584 // 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 ));
10786 std::ostringstream ss;
10887 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 ) ;
11291 std::string s (ss.str ());
11392 graphics.text (s, Point (10 , 190 ), 220 , 5 );
11493 }
0 commit comments