View Single Post
Old 04-08-2014, 08:27 AM   #14
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
So I have the following python script, with the RTC wake alarm ported over from the bash shell that Peterson did for his onlinescreensaver tool. Again, I just want to make a simplified all in one python script that I can launch from KUAL. I have it make sure the powerd state is screenSaver to start with, and then stop if the powerd state is ever active again.

So I've verified lipc-wait-event is working for other events like goingToScreenSaver. I've confirmed that the rtc0 seems to be working, and that the wake events in my testing are 60 seconds in the future when compared to "since_epoch". And if I sit there with usb attached (so Kindle never goes to deep sleep) then the -s part of lipc-wait-event works to trigger my updates every minute. I'm doing every minute just as a test. So without deep sleep, all is working. Great

Then comes the issue. I tried then to exit the ssh and remove the usb cable and it stopped. So then I turned off usb networking and used KUAL to start the script. Again, only one update and then died. At first I assumed something about the RTC was just not working, to send "resuming" events to powerd. But then I noticed that if I logged back in with ssh I could NOT see my python script process anymore. It is running a forever loop so it should be.

What I don't get is nothing about Peterson's script seems to do anything differently to keep his while 1 loop going. The only difference I can tell is that one is a bash script and one is python, but that seems like it would be a don't care.

Any thoughts on how to prevent kindle deep sleep from apparently killing my background process?


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']);
   while state.find("screenSaver"):
      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:
      state = subprocess.check_output(['lipc-get-prop', 'com.lab126.powerd', 'state']);
      if state.find("screenSaver"):
          # maybe rm .lock file here?
          break;
       
      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")
      fo.write ("{}: screensaver path: {}\n".format(tstring,file));
   
      subprocess.call(['eips','-f','-g',file]);
      
      sleepForMin(1);
      print "got back to main loop"
            

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)); 
   
   print "mins:", mins
   print "currtime:", str(getCurrTimeInSec())
   print "endtime:", endTime
   
   # 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())
   #print "alarmContents:", alarmContents
   #print "      endTime:", endTime
   
   if (endTime == alarmContents):
      print "they were equal"
      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
            print "about to lipc wait!!!  remainingWait:", remainingWait
            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