View Single Post
Old 04-15-2014, 06:42 PM   #25
Eradicatore
i void warranties
Eradicatore can extract oil from cheeseEradicatore can extract oil from cheeseEradicatore can extract oil from cheeseEradicatore can extract oil from cheeseEradicatore can extract oil from cheeseEradicatore can extract oil from cheeseEradicatore can extract oil from cheeseEradicatore can extract oil from cheese
 
Eradicatore's Avatar
 
Posts: 22
Karma: 1038
Join Date: Apr 2014
Location: Wisconsin
Device: Kindle PW2
Latest version with some nice updates

Figured I would share this version. Eventually I should put it all on github, with all the png's I've made so far with quotes from Carlin, Lennon, Wilde, Ford, Berra, and others.

I added:
- low batt warning, shows a png at 7% or less and waits for charger insert
- only update from 8am to 5pm
- added logging so I could just be sure of the update timing

Code:
#!/usr/bin/python
import time;
import glob;
import random;
import subprocess;

def mainLoop ():
   fo = open("/mnt/us/extensions/quoteRotation/bin/log.txt", "a")
   fo.write("\n=== starting new rotation ===\n");

   # first make sure the screen saver is showing
   state = subprocess.check_output(['lipc-get-prop', 'com.lab126.powerd', 'state']).rstrip();
   while (state == "active"):
      subprocess.call(['lipc-set-prop', 'com.lab126.powerd', 'powerButton', '1']);
      time.sleep(1);
      state = subprocess.check_output(['lipc-get-prop', 'com.lab126.powerd', 'state']);

   last = "";

   while 1 == 1:
      # User wants to stop, so they pressed "wake" button
      state = subprocess.check_output(['lipc-get-prop', 'com.lab126.powerd', 'state']).rstrip();
      if (state ==  "active"):
          # maybe rm .lock file here?
          break;

      # Check if we should show low battery warning
      blevel = int(subprocess.check_output(['lipc-get-prop', 'com.lab126.powerd', 'battLevel']).rstrip());
      if (blevel < 8):
         subprocess.call(['eips','-f','-g','/mnt/us/extensions/quoteRotation/bin/png/lowBatt.png']);
         # Seems dangerous, but wait here for a charging event without a -s timeout.
         subprocess.check_output("lipc-wait-event com.lab126.powerd charging", shell=True)

      # 1 for charging, 0 if not 
      #cstate =  int(subprocess.check_output(['lipc-get-prop', 'com.lab126.powerd', 'isCharging']).rstrip());

      file = random.choice(glob.glob('/mnt/us/extensions/quoteRotation/bin/png/*.png'));
      # should add check for empty list of png's here
      while file == last:
          file = random.choice(glob.glob('/mnt/us/extensions/quoteRotation/bin/png/*.png'));
          
      last = file;
          
      tstring = time.strftime("%H:%M:%S")
      mstring = int(time.strftime("%M")) 
      hstring = int(time.strftime("%H")) #24Hour
                  
      # want to sleep from 5pm to 8am
      if (hstring < 8):
         sleepTimeMin = (60 - mstring) + ((7 - hstring) * 60)
      elif (hstring > 16):
         sleepTimeMin = ((60 - mstring) + ((23 - hstring) * 60)) + (8 * 60)
      else:
         sleepTimeMin = 60
         
      fo.write ("{}: sleep for {} minutes => screensaver path: {}\n".format(tstring,sleepTimeMin,file));
      fo.flush();
                     
      subprocess.call(['eips','-f','-g',file]);
      sleepForMin(sleepTimeMin);
                                                                      

def getCurrTimeInSec ():
   "This function returns current time in seconds"
   return int(subprocess.check_output(['date','+%s']).rstrip())

def sleepForMin (mins):
   "this function will use the RTC to wake back up after so many minutes"
   endTime = str(getCurrTimeInSec() + (mins * 60)); 
   
   # disable/reset current alarm
   subprocess.call("echo 0 > /sys/class/rtc/rtc0/wakealarm", shell=True)
   
   # set new alarm
   subprocess.call("echo {} > /sys/class/rtc/rtc0/wakealarm".format(endTime), shell=True)
   
   alarmContents = (subprocess.check_output(['cat','/sys/class/rtc/rtc0/wakealarm']).rstrip())
   
   if (endTime == alarmContents):
      while getCurrTimeInSec() < int(endTime):
         remainingWait = int(endTime) - getCurrTimeInSec()
         if (remainingWait > 0):
            # wait for device to suspend or to resume - this covers the sleep period during which the
            # time counting does not work reliably
            subprocess.check_output("lipc-wait-event -s {} com.lab126.powerd resuming || true".format(str(remainingWait)), shell=True)
   else:
      print "failed compare"

   # not sure whether this is required
   #subprocess.check_output("lipc-set-prop com.lab126.powerd -i deferSuspend 1", shell=True)
   return

mainLoop()
Eradicatore is offline   Reply With Quote