HW10

pdf

School

San Jose State University *

*We aren’t endorsed by this school

Course

30

Subject

Computer Science

Date

Feb 20, 2024

Type

pdf

Pages

4

Uploaded by MinisterBravery13388

Report
10HW - Dictionaries Let us explore the RGB LEDs (neoPixels) on your CPX board. Red, green and blue (RGB) are called primary colors because all other colors can be formed through the mixing of these three. On the CPX the neoPixels are 24-bit, i.e. each color has 8 bits or 256 (= 2 8 ) values (0... 255). Some colors are important enough that got their own names, although some are more famous than others. Not every color has a name, otherwise we'd need 2 24 (that's 16,777,216) different color names to name them all. Given the color table provided below (as a dictionary): cmap = { "black" : [ 0 , 0 , 0 ], "white" : [ 255 , 255 , 255 ], "red" : [ 255 , 0 , 0 ], "lime" : [ 0 , 255 , 0 ], "blue" : [ 0 , 0 , 255 ], "yellow" : [ 255 , 255 , 0 ], "cyan" : [ 0 , 255 , 255 ], "magenta" :[ 255 , 0 , 255 ], "silver" : [ 192 , 192 , 192 ], "gray" : [ 128 , 128 , 128 ], "maroon" : [ 128 , 0 , 0 ], "olive" : [ 128 , 128 , 0 ], "green" : [ 0 , 256 , 0 ], "purple" : [ 128 , 0 , 128 ], "teal" : [ 0 , 128 , 128 ], "navy" : [ 0 , 0 , 128 ], } Your task, as will become clear, is to animate a color sequence on the RGB LED given a list of the colors' names and the duration of time (in seconds) that that color should be displayed. Note that "Black" is the way to render "no color" on the LED. spec = [ [ "MyFavorite" , 4.0 ], [ "BLACK" , 2.0 ], [ "Red" , 0.5 ], [ "White" , 0.5 ], [ "blue" , 0.5 ], [ "Black" , 2.0 ], [ "Yellow" , 0.25 ], [ "salmon" , 1.0 ], [ "lime" , 0.25 ], [ "Cyan" , 0.25 ], [ "teal" , 0.25 ], [ "blue" , 0.25 ], [ "Magenta" , 0.75 ], [ "purple" , 0.75 ], [ "maroon" , 0.75 ], [ "BLACK" , 0.75 ], [ "Red" , 0.1 ], [ "BLACK" , 0.1 ], [ "red" , 0.1 ], [ "black" , 0.1 ], [ "Red" , 0.1 ], [ "Black" , 0.1 ],
[ "Red" , 0.1 ], [ "black" , 1.0 ], [ "NotMyFavorite" , 1.0 ], [ "black" , 1.0 ] ] Keep in mind the following: Make no distinction between upper case and lower case characters - e.g. treat "Black", "BLACK" and "black" as the same. Problem 10.1 In the color map dictinary (cmap) given, add two colors of your choice "MyFavorite" and "NotMyFavorite". You have the freedom to define them any way you want. Problem 10.2 Write a function process_spec that takes a dictionary (cmap), a list (spec) and a Boolean variable (Button A) as arguments and returns False if: if the spec has a color that is not defined in the cmap or if Button A was pressed to stop the animation Otherwise return True Complete the TO DO part of the code below: In [ ]: cmap = { "black" : [ 0 , 0 , 0 ], "white" : [ 255 , 255 , 255 ], "red" : [ 255 , 0 , 0 ], "lime" : [ 0 , 255 , 0 ], "blue" : [ 0 , 0 , 255 ], "yellow" : [ 255 , 255 , 0 ], "cyan" : [ 0 , 255 , 255 ], "magenta" : [ 255 , 0 , 255 ], "silver" : [ 192 , 192 , 192 ], "gray" : [ 128 , 128 , 128 ], "maroon" : [ 128 , 0 , 0 ], "olive" : [ 128 , 128 , 0 ], "green" : [ 0 , 256 , 0 ], "purple" : [ 128 , 0 , 128 ], "teal" : [ 0 , 128 , 128 ], "navy" : [ 0 , 0 , 128 ], "MyFavorite" : [ 255 , 165 , 0 ], "NotMyFavorite" : [ 222 , 255 , 0 ] } In [ ]: from adafruit_circuitplayground import cp def process_spec ( cmap , spec , button ): if cp . button_a : print ( 'User request to end animation.' ) return False for color in spec : if color [ 0 ] . lower () not in cmap : print ( color [ 0 ] . lower ()) print ( 'Color not defined in cmap' )
Problem 10.3 If you get an error getting color name, "fix" it by looking up the missing color to get its R/G/B color values, then add it to cmap for the specified duration. Use this website to look-up the missing color: https://www.rapidtables.com/web/color/RGB_Color.html Problem 10.4 Time for the light show! Write a program that runs only if process_spec(cmap, spec, cp.button_a) is True . It examines the first color in the list spec , looks up the RGB values of the color in question from cmap (considering upper and lower case to be the same), assigns those vales to the first RGB LED ( cp.pixels[0] ) and turns it on for the duration given in spec. Then examines the next element in spec and so on. When the last LED is reached, the next element in spec is assigned to the first LED. This continues in an infinite loop. Submission return False return True In [ ]: cmap = { "black" : [ 0 , 0 , 0 ], "white" : [ 255 , 255 , 255 ], "red" : [ 255 , 0 , 0 ], "lime" : [ 0 , 255 , 0 ], "blue" : [ 0 , 0 , 255 ], "yellow" : [ 255 , 255 , 0 ], "cyan" : [ 0 , 255 , 255 ], "magenta" : [ 255 , 0 , 255 ], "silver" : [ 192 , 192 , 192 ], "gray" : [ 128 , 128 , 128 ], "maroon" : [ 128 , 0 , 0 ], "olive" : [ 128 , 128 , 0 ], "green" : [ 0 , 256 , 0 ], "purple" : [ 128 , 0 , 128 ], "teal" : [ 0 , 128 , 128 ], "navy" : [ 0 , 0 , 128 ], "MyFavorite" : [ 255 , 165 , 0 ], "NotMyFavorite" : [ 222 , 255 , 0 ] "salmon" : [ 250 , 128 , 114 ] In [ ]: while process_spec ( cmap , spec , button ): lightNumber = 0 for color in spec : cp . pixels [ lightNumber ] = cmap [ color [ 0 ] . lower ()] time . sleep ( color [ 1 ]) lightNumber += 1 if lightNumber == 10 : lightNumber = 0
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Submit an ipynb (copy your code from MU) and a pdf, including videos of the CPX, as needed