Thursday 15 January 2015

Unicorn Hat progress meter for SD card cloning

Having the ability to clone 4 SD cards at once is great but I still find myself forgetting that I've started the process running in a background window and missing when the job completes and it is time to swap in another 4 for copying.  Sounds like a job for a Unicorn Hat!

So I made good on my threat to write a simple Python script to read the logfile generated by dcfldd and lighting up the leds as the cloning progresses. The leds change colour every time an 1/8th  of the total is completed and then start flashing bright white when the job is done.


Here's the script I used. The logfile is updated with a line like

[98% of 300Mb] 9488 blocks (296Mb) written. 00:00:00 remaining.

every time dcfldd finishes copying a block (of size set by the statusinterval argument).

import unicornhat as UH
import time
# light leds based on x,y grid
def lightem3(xin,yin,colour):
    for y in range(8):
        for x in range(yin):
                UH.set_pixel(x,y,colour[0],colour[1],colour[2])
    for y in range(xin):
        for x in range(yin+1):
                UH.set_pixel(x,y,colour[0],colour[1],colour[2])  
    UH.show()
#convert % to /164ths
def map_percent(p):
  p_grid = int(p/1.5625)
  y = int(p_grid/8)
  x = p_grid%8
    return x,y
#map colour gradient onto completed %ages
def colour_map(p):
    if p < 9:
        colour = (229,0,2)
    elif p < 17:
        colour = (216,131,40)
    elif p < 25:
        colour = (218,125,0)
    elif p < 33:
        colour = (212,185,0)
    elif p < 41:
        colour = (173,210,0)
    elif p < 49:
        colour = (110,200,0)
    elif p < 57:
        colour = (50,196,0)
    elif p < 99:
        colour = (0, 255, 0)
    elif p >= 99:
colour = (255,255,255)
    return colour
#main block
UH.off()
#initially all leds blue
for y in range(8):
  for x in range(8):
    UH.set_pixel(x,y,0,0,255)
    UH.show()
time.sleep(2)
line_prev = ''
while True:
        f=open('log_file','r')
        line = f.readline()
        if line != line_prev:
                print line
                line_prev = line
                fields = line.split()
                percent = fields[len(fields)-9][1:-1]
                print percent
                col = colour_map(int(int(percent)/1.5625))
                grids = map_percent(int(percent))
                lightem3(grids[0],grids[1],col) if percent:
    #flash white when complete
if int(percent) >= 99:
lightem3(8,7,(255,255,255))
UH.show()
time.sleep(0.3)
UH.off()
        time.sleep(0.5)

No comments:

Post a Comment