View Single Post
Old 03-02-2012, 11:36 PM   #1
geekmaster
Всё гениальное просто.
geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.geekmaster ought to be getting tired of karma fortunes by now.
 
geekmaster's Avatar
 
Posts: 5,066
Karma: 6789001
Join Date: Nov 2011
Location: Щедрость не имеет пределов.
Device: *.*
geekmaster simple touchscreen script support hack

UPDATE: Added MIT license (not in download files yet).

This simple "ascii finger paint" demo shows a simple hack to use kindle touchscreen and eInk support in a shell script. I need touchscreen support for the GUI elements in my kindle recovery script, but I used a simple "ascii art" paint program to test it.

Because it does not lock the touchscreen device to prevent touches from affecting other apps using the touchscreen, it is best to run it from outside the framework. I suggest testing in in diagnostics "Lock to HOME button" menu, which prevents selecting anything while using this program.

You can put this "touchpaint-1.3" script on your kindle USB drive, then run it from an SSH session (or launch it in the kindle touch launcher menu).

To exit the program, touch the upper-left corner. The screenshots were taken before the "touch here to exit" code was added to the source code, but those updates are included in the script source code below.

Here is a little guy I painted quickly with my finger. I touched around him to test "button support":


And here are some "quick and dirty" crude text characters I drew with my finger on the kindle touchscreen (there was a fat USB cable under the kindle and it was rocking a little while drawing this):


And here is is the touchpaint script code:
PHP Code:
#!/bin/sh
#======================================================================
# touchpaint-1.3 - Geekmaster Touchscreen Shell Paint
# (touchscreen ascii paint hack in a shell script)
# Copyright (C) 2012 geekmaster
# MIT License: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#----------------------------------------------------------------------
# Revision History (last change first):
# =ver= =date===== =author===== =description============================
# 1.3   2012-03-03 geekmaster   smaller, better comments, file save.
# 1.2   2012-03-03 geekmaster   change cut to shell string splits.
# 1.1   2012-03-03 geekmaster   replace filter func with exit status.
# 1.0   2012-03-02 geekmaster   first release

#======================================================================
# get_touch - get scaled touch coords
# usage: get_touch XMax YMax 
# returns: X Y S to STDOUT (S:1=press,2=move)
#----------------------------------------------------------------------
get_touch() {
  
XMax=$1YMax=$2X=0Y=0S=2
  
while :;do (
    
hexdump -n48 /dev/input/event3|head -n3|while read L; do
      [[ 
"$L== "" ]]&& exit # empty line, continue outer loop
      
set $LC=$((0x$7)); V=$((0x$8)) # set hex parms, extract decimal
      
[[ $C -eq 57 -a $V -eq 0 ]]&& S=# press (else move)
      
[[ $C -eq 53 ]]&& X=$((V*XMax/4096)) # X (scaled to XMax)
      
[[ $C -eq 54 ]]&& Y=$((V*YMax/4096)) # Y (scaled to YMax)
      
[[ $X -gt 0 -a $Y -gt 0 ]]&& echo "$X $Y $S&& exit # done
    
done )&& break
  
done
}

#======================================================================
# character-mode touchscreen paint program
# usage: run from ssh. draw with finger. upper-left corner to exit.
# returns: (saves touchpaint.raw on USB drive)
#----------------------------------------------------------------------                                                                      
T=0eips -c
eips  0 39 
"touchpaint-1.3  Geekmaster Touchscreen Shell Paint"
eips 10  2 "Draw stuff with your finger."
eips  0  0 "   |<~~~~ Touch here to exit."
eips  0  1 "___|"
while :;do
  
set $(get_touch 50 40); X=$1Y=$2S=$# get 50x40 touch coords
  
[[ $X -le 3 -a $Y -le 2 ]]&& break # touch upper-left corner? 
  
[[ $S -eq 1 ]]&& n=$(((n+1)%10)) && X0=$X && Y0=$Y # inc $n on press
  
eips $X $Y "$n# plot char $n at touched X,Y
  
while [[ $X0 -ne $X -o $Y0 -ne $Y ]];do # step to X,Y
    
eips $X0 $Y0 "$n# plot char $n at interpolated X,Y
    
[[ $X0 -lt $X ]]&& X0=$((X0+1))
    [[ 
$X0 -gt $X ]]&& X0=$((X0-1))
    [[ 
$Y0 -lt $Y ]]&& Y0=$((Y0+1))
    [[ 
$Y0 -gt $Y ]]&& Y0=$((Y0-1))
  
done
  
[[ $T -ne 1 ]]&& T=&& eips 10 2 "                            "
done
eips 0 1 
"    "eips 0 0 "                             "
cd /mnt/us # USB Drive for saved image
mv -f touchpaint.raw touchpaint.raw.bak
dd 
if=/dev/fb0 of=touchpaint.raw
eips 4 1 
"~~~~> touchpaint.raw saved on USB drive." 
EDIT: Notice that I wrapped the subprocess while loop in parentheses and check the exit status, to avoid the infamous "while loop local scope" problem. None of the "standard methods" worked in this version of busybox, so I had to "invent" this one. Also, I replaced the "cut" subprocess calls with much faster shell string processing.

Here is a safe place to test this touchscreen script:


This is what you see when you select that menu item:



To Do:
In my script code, I plan to use eips to put a screenshot of the kindle framework keyboard screens on the screen and use the touchscreen to detect keypresses. I have already taken keyboard screenshots for my script GUI, and I have all my script output piped through eips, using its screen text scrolling feature (just put a pair of parentheses around the entire script, and pipe that indirectly to eips through a script variable). With this and the above touchscreen code, I will have a simple onscreen terminal console (but with no term ESC controls until later).

You are welcome to make free use of my touchscreen script support hack, and if you figure out cool things to do with script touchscreen support, please post your code back to this thread. But you do not need to worry about that -- just use my part of functions in your scripts and it will "just work".

Thanks. Enjoy!

P.S. As the versions progress, the code is following a typical pattern for me, where I add features by removing limitations, which has the side-effect of making the code smaller and simpler. As of version 1.3, the artwork you create with this program is automatically saved to touchpaint.raw on your USB drive when you exit the program (the previous image is renamed to touchcpaint.raw.bak).

You can use IrfanView (in Windows, or the Gimp in linux) to import raw framebuffer images as 608 wide by 800 high, 8 bits-per-pixel, then clip it to 600x800 and reduce it to 300x400 (eips text is pixel-doubled), and save it in your favorite file format. If it appears skewed when you load the raw image, try loading it as 600 wide instead. The virtual x_res can vary in different screen modes. You can find the current screen mode settings with:
eips -i

(I forgot to clip the above screenshot images before resizing, so they are 304 px wide instead of 300.)
Attached Files
File Type: gz gmpaint-1.0.gz (811 Bytes, 92 views)
File Type: gz gmpaint-1.2.gz (887 Bytes, 73 views)
File Type: gz touchpaint-1.3.gz (1.1 KB, 88 views)

Last edited by geekmaster; 04-04-2012 at 12:32 PM. Reason: version 1.3 update
geekmaster is offline   Reply With Quote