Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Readers > Amazon Kindle > Kindle Developer's Corner

Notices

Reply
 
Thread Tools Search this Thread
Old 04-04-2014, 02:36 PM   #1
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
shell script to randomize files in directory

Hi,
I've tried "sort -R" solutions, and other solutions that use $RANDOM which doesn't appear to be available in the busybox version of /bin/sh or /bin/bash on kindle PW2.

Anyone know a way to loop through random (png) files with a bash shell script on a jailbroken Kindle PW2?

Thanks!
Justin
Eradicatore is offline   Reply With Quote
Old 04-04-2014, 02:39 PM   #2
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
Here was my script that worked on my Mac, but not the kindle:


#!/bin/sh
FILES=(*.png)

while true
do
n=${#FILES[@]}
f="${FILES[RANDOM % n]}"
while [ "$f" = "$last" ]
do
f="${FILES[RANDOM % n]}"
done
last=$f
echo "WALLPAPER: $f"
sleep 2
done
Eradicatore is offline   Reply With Quote
 
Enthusiast
Old 04-04-2014, 05:57 PM   #3
knc1
Embedded Cheerleader
knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.
 
knc1's Avatar
 
Posts: 5,776
Karma: 5722276
Join Date: Feb 2012
Device: Too many.
Quote:
Originally Posted by Eradicatore View Post
Here was my script that worked on my Mac, but not the kindle:


#!/bin/sh
FILES=(*.png)

while true
do
n=${#FILES[@]}
f="${FILES[RANDOM % n]}"
while [ "$f" = "$last" ]
do
f="${FILES[RANDOM % n]}"
done
last=$f
echo "WALLPAPER: $f"
sleep 2
done
Different command processor.
The Kindle uses Busybox-ash.
(Hint: Ash does not do arrays, but Bash does.)

So run Busybox-ash on your Mac when testing scripts for the Kindles.
knc1 is offline   Reply With Quote
Old 04-04-2014, 06:41 PM   #4
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
Thanks knc! That will help for the future for sure.

So I gave up and learned enough python (I've done PERL for 15 years now) to do it.



!/usr/bin/python
import time;
import glob;
import random;
import subprocess;

last = "";
while 1 == 1:
file = random.choice(glob.glob('*.png'));
while file == last:
file = random.choice(glob.glob('*.png'));

last = file;
#print file;
subprocess.call(['eips','-f','-g',file]);
time.sleep(60*60); # switch backgrounds every hour



(btw, what's the way to show code on this forum?)

Last edited by Eradicatore; 04-04-2014 at 06:43 PM. Reason: code formatting bad
Eradicatore is offline   Reply With Quote
Old 04-04-2014, 07:57 PM   #5
knc1
Embedded Cheerleader
knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.
 
knc1's Avatar
 
Posts: 5,776
Karma: 5722276
Join Date: Feb 2012
Device: Too many.
Quote:
Originally Posted by Eradicatore View Post

(btw, what's the way to show code on this forum?)
Code:
!/usr/bin/python
import time;
import glob;
import random;
import subprocess;

last = "";
while 1 == 1:
    file = random.choice(glob.glob('*.png'));
    while file == last:
        file = random.choice(glob.glob('*.png'));

    last = file;
    #print file;
    subprocess.call(['eips','-f','-g',file]);
    time.sleep(60*60); # switch backgrounds every hour
Use the "advanced editor" button - or
wrap with either 'code' or 'php' tags.
knc1 is offline   Reply With Quote
Old 04-04-2014, 10:06 PM   #6
NiLuJe
BLAM!
NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.
 
NiLuJe's Avatar
 
Posts: 4,025
Karma: 4131199
Join Date: Jun 2010
Location: Paris, France
Device: Kindle 2i, 3g, 4, 5w, PW & PW2
Yup, see the shuffle logic of the ScreenSavers hack for other crazy ideas to deal with this issue, but, yep, it was fun, and I'm bundling a coreutils sort to ease the pain .
NiLuJe is online now   Reply With Quote
Old 04-04-2014, 11:58 PM   #7
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 now the next step is of course to have this python script run somehow either automatically or when I do some event. I'm checking if the "eventHandler" hack will help me just call this python script in the background on a suspend event.

Just FYI, I posted that question here.
Eradicatore is offline   Reply With Quote
Old 04-05-2014, 02:18 AM   #8
knc1
Embedded Cheerleader
knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.knc1 ought to be getting tired of karma fortunes by now.
 
knc1's Avatar
 
Posts: 5,776
Karma: 5722276
Join Date: Feb 2012
Device: Too many.
Quote:
Originally Posted by Eradicatore View Post
So now the next step is of course to have this python script run somehow either automatically or when I do some event. I'm checking if the "eventHandler" hack will help me just call this python script in the background on a suspend event.

Just FYI, I posted that question here.
The easiest way to get started would be to run it from the KUAL menu.
knc1 is offline   Reply With Quote
Old 04-05-2014, 04:21 PM   #9
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
Hi knc,
So I installed KUAL, and have it working (like I can toggle USBnet hack etc). Great advice on using that to run my scripts. I also installed the KUAL helper scripts and used that as an example for running my own shell scripts. Seemed like it was going to be a cakewalk. But then I got a synxtax error on the menu.json. I tried using template 1 from this KUAL help link also but that didn't work either.

So my issue is I see in the KUAL main menu "KUAL <big bold dot> 1 <submenu arrow>" as the first item. Then if I click in there I see a button second to last with the label "<big bold dot> Syntax quotes/menu.json"

I made a sub folder in extensions called "quotes" and in there I have menu.json, config.xml, and bin folder (with my sh scripts). So basically a copy of "helper".

My new menu.json looks like this:

Code:
{
	"items": [
	{
		"name": "QuoteRotation",
		"priority": 0,
		"items": [
		{
			"if": "\"KindleTouch\" -m \"KindlePaperWhite\" -m \"KindlePaperWhite2\" -m || ||",
			"name": "Start",
			"priority": 1,
			"action": "bin/wrap_ssloop.sh"
		},
		{
			"name": "Exit",
			"priority": 2,
			"action": "bin/rm_ssloop_lock.sh",
		}
		]
	}
	]
}

Any ideas? Are there logs somewhere from KUAL that will give more details on the syntax error perhaps?
Eradicatore is offline   Reply With Quote
Old 04-05-2014, 04:44 PM   #10
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
Wow, that's embarrassing. I forgot to remove the extra comma. :-)
Eradicatore is offline   Reply With Quote
Old 04-06-2014, 08:06 AM   #11
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
Ok, so I got KUAL setup to start it, everything is looking great, if I set the timeout between screens at something small to test (8 seconds), it looks to work perfect. Then, I set it to 45 minutes and leave it. Nothing. I figure, ok, maybe the python script died for some reason. Nope. SSH in and ps -ef and there it is.

So time to add some logging. Just figured I would document it here. Does Kindle go into ultra low sleep and so my "sleep" call in python never finishes?
Eradicatore is offline   Reply With Quote
Old 04-06-2014, 09:47 AM   #12
NiLuJe
BLAM!
NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.
 
NiLuJe's Avatar
 
Posts: 4,025
Karma: 4131199
Join Date: Jun 2010
Location: Paris, France
Device: Kindle 2i, 3g, 4, 5w, PW & PW2
@Eradicatore: Yup. cf. http://www.mobileread.com/forums/sho...d.php?t=236104
NiLuJe is online now   Reply With Quote
Old 04-06-2014, 09:30 PM   #13
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
@NiLuJe, thanks for the link (again). I finally realized I had been reinventing the wheel here, but that was probably good anyway since I got to learn a lot about my way around your community here and all the great things you're working on.

Also, mine is really just a dumbed down version of what he's got, but also I don't need to use WiFi so it may last a bit longer. I'm planning on just preloading a bunch of quotes and maybe updating once in a while after I gather more I want and create them in Gimp. I see the function I need from his work. The wait_for() is perfect. Just setup the RTC of the system and trigger a wake that way. Brilliant!!! I'm used to working on raspberry pi where there is no RTC. :-)

Anyway, thanks again. I really really appreciate your helping out a NOOB to this Kindle hacking stuff.
Eradicatore is offline   Reply With Quote
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
Old 04-08-2014, 08:41 AM   #15
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
Investigating this thread right now:

http://www.mobileread.com/forums/sho...ight=low+power

To start learning about the /etc/upstart configurations.
Eradicatore is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
invoking sql in a shell script Mingyar Kobo Developer's Corner 17 05-31-2013 06:50 PM
pdf to txt shell script Mr.Castro0o PDF 0 03-27-2013 07:39 AM
Shell script to wait for user input from K3g keyboard jmseight Kindle Developer's Corner 33 04-01-2012 04:32 PM
A way to execute files without shell access?!? scotsman iRex 8 03-13-2009 07:35 AM
Script to convert a directory full of files? mdibella Kindle Formats 2 10-14-2008 01:21 PM


All times are GMT -4. The time now is 11:38 AM.


MobileRead.com is a privately owned, operated and funded community.