View Single Post
Old 04-06-2012, 11:54 AM   #59
kaminkatze
Member
kaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with otherskaminkatze plays well with others
 
Posts: 17
Karma: 2600
Join Date: Mar 2012
Device: Kindle 3
Quote:
Originally Posted by geekmaster View Post
I prevented this in my earlier version by pausing (killall -stop) and resuming (killall -cont) the kindle framework. That code was removed when kaminkatze added updates, because he calls this from a parent script (that presumable does the framework pause and resume). You can download the v1.1 code above to see the differences.
Must have been in the unpublished v1.0. v1.1 linked in the original post has no killall. But yes, it's very likely that I would have removed it and forgotten to put it in again.

Added backslash duplicates so that a single '\' is preserved by read L.

New version uses IFS="" because IFS="\n" somehow removed 'n's at the end of a line.

Spoiler:

Code:
#!/bin/sh
#=============================
# titty - tiny tty (v1.5)
# display tiny text on eink
# requirements: titty.png.
# usage:
#   titty
#     cat self
#   titty [x y] [-k] [--] -|STRING|-f FILE
#     displays "STDIN", "echo STRING" or "cat FILE"
#       [x y]  initialize cursor at x y
#       [-k]   clear lines
#       [--]   treat as string
#   titty -s Y H S
#     move H-S lines S lines up, beginning with line Y+S
#   titty -mx|-my
#     return maximal x y coordinates
#   titty -v
#     return version
# hint: This runs at TTY speed.
#------------------------------
# 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.5   2012-04-06 kaminkatze   fix IFS "end of line" and '\' bug
# 1.4   2012-04-05 geekmaster   fix "end of page" bug
# 1.3   2012-04-05 kaminkatze   scrolling and options
# 1.2   2012-04-04 kaminkatze   k3 support and per line hexdump
# 1.1   2012-04-01 geekmaster   first release

# WARNING: If you don't stop using titty, you will go blind! :D

#===========================
# initvar - init global vars
#---------------------------
initvar() {
  V=1.5 # version
  eips -g "$0.png" # char map image
  DN=/dev/null DF=/dev/fb0 # devices
  set $(eips -i|grep res:); FW=$2 FH=$4 # fb0 width, height
  set $(eips -i|grep line); FS=$4 # fb0 stride
  CW=$((4*FS/FW)) CH=6 LH=7 # char width, height, line height
  LM=2 TM=$((LH*2)) # left, top margin px
  MX=$((FW*FS/FW-CW)) MY=$((FH-LH)) # max X, Y
  CX=$LM CY=$TM # cursor coords
  DD="dd if=$DF of=$DF bs=1 count=$CW" # ddblit
  DL="dd if=$DF of=$DF bs=1 count=$((FS*LH)) skip=$((FS*CH))" # clear line
  Q=false # quit flag
  K=false # clear line flag
  JFS=$IFS # normal field separator
}

#====================
# usage: scroll y h s
#--------------------
scroll() {
  dd if=$DF of=$DF bs=1 count=$((FS*LH*($2-$3))) \
    skip=$((FS*(LH*($1+$3)+TM))) seek=$((FS*(LH*$1+TM))) 2>$DN
  eips ''
}

#=================
# titty - tiny tty
# displays STDIN
#------------------
titty() {
  sed 's/\\/\\\\/g' | until $Q ; do IFS=''; read L || Q=true; IFS=$JFS
    $K && [[ $CX -eq $LM ]] && $DL seek=$((CY*FS)) 2>$DN
    for A in $(echo -n "$L" | hexdump -ve '/1 "%d\n"'); do
      if [[ $A -ne 32 ]]; then
        RO=$(((A-32)*CW)) FO=$((CY*FS+CX))
        for y in $(seq 1 $CH);do
          $DD skip=$RO seek=$FO 2>$DN; RO=$((RO+FS)) FO=$((FO+FS))
        done
      fi
      CX=$((CX+CW)); [[ $((CX%64)) -eq $LM ]] && eips ''
      if [[ $CX -gt $MX ]]; then CX=$LM CY=$((CY+LH))
        [[ $CY -gt $MY ]] && CY=$TM CX=$LM
        $K && $DL seek=$((CY*FS)) 2>$DN; fi
    done; eips ''
    CX=$LM CY=$((CY+LH)); [[ $CY -gt $MY ]] && break
  done
}

# Do stuff!  :-)
initvar
if [[ "$1" == "" ]]; then
  K=true; cat "$0" | titty
else while [[ "$1" ]]; do
  case "$1" in
    -s) scroll $2 $3 $4; shift;shift;shift;shift;;
    -mx) echo $((FS/CW-CW)); shift;;
    -my) echo $(((FH-TM)/LH-1)); shift;;
    -f) cat "$2"|titty; shift;shift;;
    -) titty; shift;;
    -k) if [[ $CX -gt $LM ]]; then
          for y in $(seq 0 $((LH-1))); do
            dd if=$DF of=$DF bs=1 count=$((FS-CX)) \
              skip=$((FS*CH)) seek=$(((CY+y)*FS+CX)) 2>$DN
          done
        fi; K=true; shift;;
    --) echo -n "$2"|titty; shift;shift;;
    -v) echo $V; shift;;
    *) if [[ -n "$3" ]]
       then CX=$((LM+$1*CW)) CY=$((TM+$2*LH)); shift;shift
       else echo -n "$1"|titty; shift
       fi;;
  esac
done; fi
kaminkatze is offline   Reply With Quote