This program takes the HTML code from a websites and uses an algorithm to calculate a co-ordinate on the Unicorn HAT. This LED is then briefly flashed on and off. What this creates is a colour visualisation of a website. You could think of it as a digital light signature.
1. Coding a location for each LEDTo get started I originally began creating a dictionary of coordinates that related to each LED on the Unicorn and the corresponding letter in the Alphabet. For example letter A would be the co-ordinates 0,0 and the letter B would be 0, 1 and so on. However, this became complicated as I wanted a specific LED to light up for each individual letter. Thanks to @Gagetiod and his help, he suggested using the ord() function which converts a letter / character to its corresponding ASCII value. Dividing the value by 8 then calculates the a value for the x part of the LED co-ordinate and finding the modulus returns the y coordinate. Now every letter, charater or symbol can be converted to an X and Y location on the Unicorn HAT
|
|
2. Using an URL to pull down the HTML code for a Website
Download the Code Here
|
Once LEDs and the letters where assigned I created a simple string input to test the program. I then got carried away and began to wonder what the HTML code behind a website would look like as a set of colors on an 8 x 8 LED grid of bright light! Using the urllib2 and the code urllib2.urlopen(name of the website) the HTML could be added to a variable. Then each of the characters in the variable can be parsed through, the co-ordinates calculated and then the LED light up.
Letter to ASCII Vaue
index = ord(letter)-65 if index > 0: x = int(index/8.0) y = int(index%8) print (x, y) 3. SparklesIf the ASCII number is below 0 the value of zero then the LEDs could not be located. Therefore I used @Ben_Nuttal's random sparkle program to select a random LED and colour - adds more variety.
x = randint(0, 7), y = randint(0, 7), r = randint(0, 255)
g = randint(0, 255), b = randint(0, 255), UH.set_pixel(x, y, r, g, b) UH.show(), time.sleep(0.10), UH.clear() |