import socket
import time
import unicornhathd
try:
    from PIL import Image, ImageDraw, ImageFont
except ImportError:
    exit("This script requires the pillow module\nInstall with: sudo pip install pillow")

# get IP address
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
my_ip = s.getsockname()[0]
print(my_ip)
s.close()

colours = (255,255,0)


font_file = "/usr/share/fonts/truetype/freefont/FreeSansBold.ttf"
font_size = 12

width, height = unicornhathd.get_shape()  # 16,16 by default

text_x = width
text_y = 0

font = ImageFont.truetype(font_file, font_size)

text_width, text_height = width, 0
print(text_width)

w, h = font.getsize(my_ip)
text_width += w + width  # add some padding so the ip scrolls off the unicorn hat
text_height = max(text_height,h, 16)  # no more than the size of the unicorn hat

image = Image.new("RGB", (text_width, text_height), (0,0,0))
draw = ImageDraw.Draw(image)
draw.text((text_x, text_y), my_ip, colours, font=font)


# DISPLAY
unicornhathd.rotation(0)
for scroll in range(text_width - width):
    for x in range(width):
        for y in range(height):
            pixel = image.getpixel((x+scroll, y))
            r, g, b = [int(n) for n in pixel]
            unicornhathd.set_pixel(width-1-x, y, r, g, b)
    unicornhathd.show()
    time.sleep(0.02)
unicornhathd.off()



@@ -203,16 +203,34 @@
 llow%22)%0A%0A
+def get_ip():%0A    
 # get IP
@@ -238,16 +238,20 @@
 address%0A
+    
 s = sock
@@ -295,16 +295,20 @@
 _DGRAM)%0A
+    
 s.connec
@@ -326,16 +326,20 @@
 %22, 80))%0A
+    
 my_ip = 
@@ -357,16 +357,20 @@
 me()%5B0%5D%0A
+    
 print(my
@@ -374,16 +374,20 @@
 (my_ip)%0A
+    
 s.close(
@@ -388,17 +388,76 @@
 close()%0A
-%0A
+    return my_ip%0A%0A%0Adef create_image_from_text(in_text):%0A    
 colours 
@@ -471,13 +471,17 @@
 255,
+25
 0)%0A
-%0A%0A
+    
 font
@@ -542,16 +542,20 @@
 ld.ttf%22%0A
+    
 font_siz
@@ -565,170 +565,112 @@
  12%0A
-%0Awidth, height = unicornhathd.get_shape()  # 16,16 by default%0A%0Atext_x = width%0Atext_y = 0%0A%0Afont = ImageFont.truetype(font_file, font_size)%0A%0Atext_width, text_height
+    font = ImageFont.truetype(font_file, font_size)%0A    w, h = font.getsize(in_text)%0A%0A    text_x, text_y
  = w
@@ -677,22 +677,20 @@
 idth, 0%0A
-print(
+    
 text_wid
@@ -695,38 +695,38 @@
 idth
-)%0A%0Aw, h = font.getsize(my_ip)%0A
+, text_height = width, 0%0A%0A    
 text
@@ -803,16 +803,20 @@
 orn hat%0A
+    
 text_hei
@@ -889,16 +889,20 @@
 rn hat%0A%0A
+    
 image = 
@@ -954,16 +954,20 @@
 0,0,0))%0A
+    
 draw = I
@@ -987,16 +987,20 @@
 (image)%0A
+    
 draw.tex
@@ -1050,20 +1050,88 @@
 nt)%0A
-%0A%0A# DISPLAY%0A
+    return image, text_width%0A%0A# DISPLAY%0Adef scroll_txt((image,text_width)):%0A    
 unic
@@ -1151,16 +1151,20 @@
 tion(0)%0A
+    
 for scro
@@ -1196,24 +1196,28 @@
 width):%0A    
+ 
+   
 for x in ran
@@ -1231,24 +1231,28 @@
 h):%0A        
+  
+  
 for y in ran
@@ -1259,24 +1259,28 @@
 ge(height):%0A
+    
             
@@ -1329,16 +1329,20 @@
         
+    
 r, g, b 
@@ -1375,24 +1375,28 @@
             
+ 
+   
 unicornhathd
@@ -1429,24 +1429,28 @@
 , g, b)%0A    
+   
+ 
 unicornhathd
@@ -1457,16 +1457,20 @@
 .show()%0A
+    
     time
@@ -1482,16 +1482,20 @@
 p(0.02)%0A
+    
 unicornh
@@ -1507,8 +1507,307 @@
 off()%0A%0A%0A
+# one stop call for scrolling text%0Adef scroll_text(in_txt):%0A    scroll_txt(create_image_from_text(in_txt))%0A%0Awidth, height = unicornhathd.get_shape()  # 16,16 by default%0Amy_ip = get_ip()%0A# my_image, text_width = create_image_from_text(my_ip)%0A# scroll_txt(my_image,text_width)%0Ascroll_text(my_ip)%0A%0A%0A%0A%0A%0A

organise code into functions for better reusability