#!/usr/bin/python
# Run some simple tets on the ifp module and put the iFP device thorugh its
# paces. Nothing special here, just an example of using the ifp module
# 
# $Id: pyifp-test,v 1.5 2005/01/29 06:15:48 milamber Exp $
#
import ifp
import sys
import os

def simple_dir_print(file_type,filesize,filename):
    t = ""
    if file_type == ifp.IFP_FILE:
        t = "f"
    else:
        t = "d"
        
    print "%s %8d %s" % (t,filesize,filename)
    print "User cancel! This should be the end of the listing"
    return 1
        
def file_transfer_print(transfer_status):
    filename = transfer_status.file_name
    file_total = transfer_status.file_total
    file_bytes = transfer_status.file_bytes
    batch_bytes = transfer_status.batch_bytes
    batch_total = transfer_status.batch_total
    files_count = transfer_status.files_count
    files_total = transfer_status.files_total
    is_batch = transfer_status.is_batch

    print """%s %d of %d bytes.""" % (filename,file_bytes,file_total),
    if is_batch:
        print """%d of %d total bytes (%d of %d)""" % (batch_bytes,batch_total,files_count+1,files_total),
    print "\r",
    return 0
    
if __name__ == "__main__":
    my_ifp = ifp.iFP()
    try:
        my_ifp.init_device();
    except IOError,e:
        print "Unable to find iFP device. Perhaps it's unpluged or turned off"
        sys.exit(0);
    capacity = my_ifp.get_capacity()
    freespace = my_ifp.get_free_space()
    if capacity > 1024 * 1024 * 1024:
        cap_string = "%3.2dGig" % (float(capacity) / float(1024 * 1024 * 1024))
    elif capacity > 1024 * 1024:
        cap_string = "%3.2dMeg" % (float(capacity) / float(1024 * 1024))
    else:
        cap_string = "%dK" % (float(capacity) / float(1024))
        
    if freespace > 1024 * 1024 * 1024:
        free_string = "%3.2dGig" % (float(freespace) / float(1024 * 1024 * 1024))
    elif freespace > 1024 * 1024:
        free_string = "%3.2dMeg" % (float(freespace) / float(1024 * 1024))
    else:
        free_string = "%dK" % (float(freespace) / float(1024))
        
    print "Capacity =",cap_string
    print "Free = %s (%.2f%% used)" % (free_string,100 * (1 - (float(freespace) / float(capacity))))
    print "Battery level is",my_ifp.get_battery()
    print "Model =",my_ifp.get_model()
    print "Device =",my_ifp.get_device_info()
    print "Firmware Version =",my_ifp.get_firmware_version()
    
    filename = "test.mp3"
    #my_ifp.mkdir("\\test4")
    #my_ifp.upload_file(filename,"\\test\\" + filename)
    #stat = os.stat(filename)
    #print "statObj = %r" % stat
    #filesize = stat[6]
    #print "file size = %d" % filesize
    #my_ifp.write_open("\\" + filename,filesize)
    #infile = file(filename,"rb")
    #my_ifp.write_data(infile.read())
    #my_ifp.write_close()
    #my_ifp.upload_file_with_progress(filename,"\\" + filename,file_transfer_print)
    #print
    #my_ifp.rename("\\" + filename,"\\test2.mp3")
    #my_ifp.download_file_with_progress("\\test2.mp3","test2.mp3",file_transfer_print)
    #print
    try:
        my_ifp.upload("test","test_upload",callback = file_transfer_print)
    except:
        pass
    # list the root directory
    my_ifp.listdir("\\music",simple_dir_print)
    print "Now listing with no callback"
    print my_ifp.listdir("\\music")
    #my_ifp.rmdir("\\test_upload",true)
    #my_ifp.unlink("\\test2.mp3")
    #my_ifp.rename("\\testdir","\\test2dir")
    #for x in range(0,20):
    #    (call,freq) = my_ifp.get_preset(x)
    #    print "Station %d: Call sign: %s, freq: %.1fMhz" % (x,call,freq)
    #my_ifp.set_preset(0,"KHITS",96.3) # set the first preset
    my_ifp.close()
