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-06-2012, 01:59 PM   #61
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 knc1 View Post
Not "somehow" but "required behavior" -

The character(s) in the Input Field Seperator (IFS) string are always removed.
Translation: IFS characters are never passed unless escaped or quoted.
Yes, but I think the idea was to match only the newline character "\n" and not the normal "n".

Oooh, I see. I changed the IFS=$'\n' to $"\n", which isn't the same.

Edit: IFS=$'\n' isn't working on the K3.

Last edited by kaminkatze; 04-06-2012 at 02:28 PM.
kaminkatze is offline   Reply With Quote
Old 04-06-2012, 02:44 PM   #62
knc1
Going Viral
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: 17,212
Karma: 18210809
Join Date: Feb 2012
Location: Central Texas
Device: No K1, PW2, KV, KOA
Quote:
Originally Posted by kaminkatze View Post
Yes, but I think the idea was to match only the newline character "\n" and not the normal "n".

Oooh, I see. I changed the IFS=$'\n' to $"\n", which isn't the same.

Edit: IFS=$'\n' isn't working on the K3.
It might also depend on the application that is being used as "/bin/sh" -
Not all implementations behave the same for various reasons and then even some implementations vary from one release to another.

One thing to try - see if the make/model/version/etc of the "shell" program will let you write the newline character as hex:

IFS=$'\x0A'

If your using the "shell" program provided by Busybox, then there are some more chances for "gotcha's" ....

Over the years (and versions) Busybox has provided more than one "shell" application.
So with some versions, it depends on which one was selected at build time.

More recently, the 'ash' shell application has been the prefered one in Busybox builds. That one is the most complete and correct.
But that doesn't mean it is the one lab126 choose to build.

And of course, as can be seen in the Amazon source catalog, not all builds for all Kindles used the same version of Busybox.
(Also, you can see that Amazon did not ship the .config file "as built" with their source code releases which would let us just look-up which shell application got built.)

But putting in characters by their Hex or Octal code __usually__ works.

Last edited by knc1; 04-06-2012 at 02:50 PM.
knc1 is offline   Reply With Quote
Advert
Old 04-06-2012, 03:15 PM   #63
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: 13,477
Karma: 26012492
Join Date: Jun 2010
Location: Paris, France
Device: Kindle 2i, 3g, 4, 5w, PW, PW2, PW5; Kobo H2O, Forma, Elipsa, Sage, C2E
AFAIR, The K3's busybox uses ash, but *without* the bash compatibilty option (which 'breaks' a bunch of substring fun stuff, among other things).
NiLuJe is offline   Reply With Quote
Old 04-06-2012, 03:19 PM   #64
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 knc1 View Post
IFS=$'\x0A'
No, looks like the whole $'string' syntax is not supported.

IFS=$(echo -ne '\n') or IFS='' works.
kaminkatze is offline   Reply With Quote
Old 04-06-2012, 06:41 PM   #65
geekmaster
Carpe diem, c'est la vie.
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: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
I tried all kinds of options the *should* have worked (but didn't on the busybox versions I was using). I finally settled on something that *did* work (for the application I used it in). I have published a number of different solutions (with and without IFS) that each worked for *that* program. Substring stuff that works on k4 and touch does not work on k3, and some IFS stuff that works on k3 does not work on touch. It makes things a bit difficult to write code that works on all the kindle models.
geekmaster is offline   Reply With Quote
Advert
Old 04-11-2012, 11:42 AM   #66
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
The CW/CH/LH variables are set according to the width and height of the png image.
Added support for some of the color/cursor movement escape sequences.
Changed the end of page behaviour to auto scrolling.

Spoiler:

Code:
#!/bin/sh
#=============================
# titty - tiny tty (v1.6)
# display tiny text on eink
# requirements: titty.png.
# usage:
#   titty
#     cat self
#   titty [x y] [--] -|STRING|-f FILE
#     displays "STDIN", "echo STRING" or "cat FILE"
#       [x y]  initialize cursor at x y
#       [--]   treat as string
#   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.6   2012-04-11 kaminkatze   png dimensions, invert, escape sequences
# 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.6 CM="$0.png"; eips -c -g "$CM" # version, 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=$((0x$(hexdump -s 16 -n 4 -ve '/1 "%02x"' "$CM")/96*FS/FW))
  CH=$((0x$(hexdump -s 20 -n 4 -ve '/1 "%02x"' "$CM")))
  LH=$((CH+1)) # 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
  MC=$((FS/CW-CW)) MR=$(((MY-TM)/LH)) # max column, row
  CX=$LM CY=$TM # cursor coords
  DD="dd if=$DF of=$DF bs=1 count=$CW" # ddblit
  C= # escape sequence
  I=false # inverted flag
  NL=$(echo -ne '\n') JFS=$IFS # normal field separator
}

#==============
# usage: scroll
#--------------
scroll() {
  dd if=$DF of=$DF bs=1 count=$((FS*LH*MR)) \
    skip=$((FS*(LH+TM))) seek=$((FS*TM)) 2>$DN
  dd if=$DF of=$DF bs=1 count=$((FS*LH)) \
    skip=$((FS*CH)) seek=$(((MR*LH+TM)*FS)) 2>$DN
  CY=$((CY-LH)); eips ''
}

#==============
# usage: invert
#--------------
invert() {
  dd bs=1 count=$((FS*CH)) if=$DF 2>$DN \
  | tr "\x00\x0f\xf0\xff" "\xff\xf0\x0f\x00" \
  | dd bs=1 count=$((FS*CH)) of=$DF 2>$DN
  $I && I=false || I=true
}

#================
# usage: killline
#----------------
killline() {
  if [[ $CX -eq $LM ]]
  then dd if=$DF of=$DF bs=1 count=$((FS*LH)) \
         skip=$((FS*CH)) seek=$((CY*FS)) 2>$DN
  else 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
}

#================
# usage: escape s
#----------------
escape() {
  case "$1" in
    '\e[0m') $I && invert;;
    *'m') $I || invert;;
    *'f'|*'H') IFS='\e[;fH'; set $(echo "$1"); IFS=$JFS
               CX=$((LM+$4*CW)) CY=$((TM+$5*LH));;
    *'A') IFS='\e[A'; set $(echo "$1"); IFS=$JFS
          CY=$((CY-$4*LH)); [[ $CY -lt 0 ]] && CY=0;;
    *'B') IFS='\e[A'; set $(echo "$1"); IFS=$JFS
          CY=$((CY+$4*LH)); [[ $CY -gt $MY ]] && CY=$((MR*LH));;
    *'C') IFS='\e[;C'; set $(echo "$1"); IFS=$JFS
          CX=$((CX+$4*CW)); [[ $CX -gt $MX ]] && CX=$((MC*CW));;
    *'D') IFS='\e[;C'; set $(echo "$1"); IFS=$JFS
          CX=$((CX-$4*CW)); [[ $CX -lt 0 ]] && CX=0;;
    '\e[K') killline;;
    '\e[2J') eips -c -g "$CM"; CX=$LM CY=$TM;;
    *) return 1;;
  esac; return 0
}
        
#=================
# titty - tiny tty
# displays STDIN
#------------------
titty() {
  awk '{gsub(/\\/,"\\\\");print $0}' | while IFS=$NL; read L; do IFS=$JFS
    for A in $(echo -n "$L" | hexdump -ve '/1 "%d\n"'); do
      if [[ -n "$C" ]]; then
        C=$C$(echo -e $(printf '\x%x' $A))
        escape $C && C=; continue
      elif [[ $A -eq 27 ]]; then C='\e'; continue; fi
      while [[ $CY -gt $MY ]]; do scroll; done
      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; 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; fi
    done; eips ''; CX=$LM CY=$((CY+LH))
  done
}

# Do stuff!  :-)
initvar
if [[ "$1" == "" ]]; then
  cat "$0" | titty
else while [[ "$1" ]]; do
  case "$1" in
    -mx) echo $MC; shift;;
    -my) echo $MR; shift;;
    -f) cat "$2" | titty; shift;shift;;
    -) titty; shift;;
    --) echo "$2" | titty; shift;shift;;
    -v) echo $V; shift;;
    *) if [[ -n "$3" ]]
       then CX=$((LM+$1*CW)) CY=$((TM+$2*LH)); shift;shift
       else echo "$1" | titty; shift
       fi;;
  esac
done; fi


Tinysh mod which uses the escape sequences to control titty.

Spoiler:

Code:
#!/bin/sh
#===================================================
# tinysh - tiny limited onscreen shell (v1.0) for K3
#---------------------------------------------------
# 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.

#===========================
# initvar - init global vars
#---------------------------
initvar() {
  SYM=126 ENT=28 SHFT=42 SPC=57 DEL=14
  LEFT=105 RIGHT=106 UP=103 DOWN=108
  KN="1234567890____qwertyuiop____asdfghjkl_____zxcvbnm_."
  KS="1234567890____QWERTYUIOP____ASDFGHJKL_____ZXCVBNM_,"
  KP='1234567890____~@#$%^&*()____<>[]{}?/\_____;:"|-+=_?'
  KM=$KN
  CMD= # cmd line
  killall -stop cvm launchpad 2>/dev/null
}

#========
# cleanup
#--------
trap "cleanup" SIGTERM SIGINT
cleanup() {
  killall -cont cvm launchpad 2>/dev/null
}

#=============================================
# shcmd - emit shell command, preceeded by pwd
#---------------------------------------------
shcmd() {
  case "$CMD" in
    exit) cleanup; exit;;
    clear) echo 'echo -e "\033[2J"';;
    keymap) echo "echo -e '$KN\n$KS\n$KP'";;
    *) echo "$CMD";;
  esac
  echo 'echo -e "$PWD \$"'
  LCMD=$CMD
  CMD=
}

#===================================================
# getkb - get translated ascii keys from K3 keyboard
#---------------------------------------------------
getkb() {
  CMD=keymap
  shcmd
  while :; do
    set $(waitforkey); K=$1 D=$2
    if [[ $D -eq 0 ]]; then # up
      case $K in
        $SHFT) KM=$KN;;
        $SYM) KM=$KN;;
     esac; continue
    else A= # down
      case $K in
        $ENT) shcmd && continue;;
        $SHFT) KM=$KS && continue;;
        $SYM) KM=$KP && continue;;
        $DEL) if [[ ${#CMD} -gt 1 ]]
              then CMD=$(echo "$CMD" | cut -b-$((${#CMD}-1)))
              else CMD=
              fi;;
        $SPC) A=' ';;
        $UP) CMD=$LCMD;;
        $DOWN) CMD=;;
        *) [[ $K -le 52 ]] && A="$(echo \"$KM\"|cut -b$K)";;
      esac
    fi
    CMD="$CMD$A"
    echo "printf \"\033[1A\033[\$((\${#PWD}+3))C\033[1m%s\033[0m \n\" '$CMD'"
  done
}

PATH=$PATH:$(dirname $0)
initvar # init global vars
getkb | sh 2>&1 | titty -
cleanup

Last edited by kaminkatze; 04-11-2012 at 11:44 AM.
kaminkatze is offline   Reply With Quote
Old 04-11-2012, 12:14 PM   #67
geekmaster
Carpe diem, c'est la vie.
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: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
Quote:
Originally Posted by kaminkatze View Post
The CW/CH/LH variables are set according to the width and height of the png image.
Added support for some of the color/cursor movement escape sequences.
Changed the end of page behaviour to auto scrolling.
---
Tinysh mod which uses the escape sequences to control titty.
---
Cool! I love it when people take things off my "To Do" list for me.

I plan to use this in my touchscreen code I have been working on, which will also support 5-way pads and keyboards on the K3 and DX. I do not have a K2 or K1 to test on, or I would support those too. Does anybody have a K2 or K1 collecting dust? If yes, then please PM me!

Let's replace that comment line containing this HUGE pile of incomprehensible legal mumbo jumbo:
Code:
# 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.
with this "License by URL" comment line:
Code:
# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
After some legal research, I did that for my new "getkernels" scripts, and I plan to go back to replace the license comment line in my old scripts too:


Thanks.
geekmaster is offline   Reply With Quote
Old 04-11-2012, 02:52 PM   #68
geekmaster
Carpe diem, c'est la vie.
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: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
Quote:
Originally Posted by kaminkatze View Post
I feel like I'm not ready for a cross-compiled language (besides I never really used C).

This is how far I got (with the eips calls, thus same update speed) .
...
The reason we started using eips in newer scripts is that it was the only way discovered (so far) to update the eink display from a script on the newer kindles (K4 and Touch).

On a K3 and earlier, eips is very slow, but there is a faster way. You can echo characters to the eink /proc files that can do different types of eink updates, some of which are much faster than using eips. This is how all the scripts did eink display updates before the K4 and touch were available.

You might want to use a script function to do the updates:
PHP Code:
#===========================
# eupd - eink update display
#---------------------------
eupd() {
  echo 
>/proc/eink_fb/update_display # (for k3 and earlier)
#  eips '' # (for k4 and newer)
}

# sample eink "update display" call
line $X1 $Y1 $X2 $Y2eupd # update display 
Different numbers written to that /proc do different things. 0 is a clear screen (like "eips -c"). 1 is a fast update. 2 is a full flash update. Obviously, we want the fast update for these scripts.

EDIT: I am now using this eupd function in my new scripts.


Last edited by geekmaster; 04-11-2012 at 03:27 PM.
geekmaster is offline   Reply With Quote
Old 04-29-2012, 09:50 PM   #69
PoP
 curly᷂͓̫̙᷊̥̮̾ͯͤͭͬͦͨ ʎʌɹnɔ
PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.
 
PoP's Avatar
 
Posts: 3,002
Karma: 50506927
Join Date: Dec 2010
Location: ♁ ᴺ₄₅°₃₀' ᵂ₇₃°₃₇' ±₆₀"
Device: K3₃.₄.₃ PW3&4₅.₁₃.₃
3D - An Interactive animated projection of an object on a 2D screen

Click image for larger version

Name:	3D.png
Views:	505
Size:	3.8 KB
ID:	85869

After seeing geekmaster's "eink algorithmic art" demos, I fancied and dusted off a 30 years old demo -- see the included original Byte Magazine extract! (with a bonus p486 rescan to fix original download URL )

Teaser code for illustrative purpose only,
Spoiler:
PHP Code:
#!/bin/sh
#=============================
# 3D - Projects a 3D object to a 2D screen
# Inspired by "eink algorithmic art" thread at https://www.mobileread.com/forums/showthread.php?t=172182
# usage: 3D
# Only tested on Kindle3 (firmware 3.3, Busybox 1.7.2 Almquist Shell)
#------------------------------
# Copyright (c) 2012 PoP under MIT License:
# Copyright (c) 2012 geekmaster under MIT License:
# http://www.opensource.org/licenses/mit-license.php
#------------------------------
# Revision History:
# v4.0  2012-04-05 PoP            ported to Kindle3                  
# v3.0  1986-12-20 PoP            ported to IBM PC
# v2.0  1983-09-14 PoP            ported to C=64
# v1.0  1982-11-11 PoP            ported to Apple][
# v0.0  1981-01-01 A. Pickholtz   see pp 474-505 BYTE magazine: http://malus.exotica.org.uk/~buzz/byte/pdf/BYTE%20Vol%2007-11%201982-11%20Graphics.pdf
#------------------------------
# Global vars:
# B    bank -PI..PI scaled
# CB    cos(bank)
# CH    cos(heading)
# CM    var C of 3d matrix  scaled
# CP    cos(pitch)
# BLOCK vectors of this object scaled
# D    distance from camera scaled
# DF    device frame buffer
# DK    device keyboard
# DM    var D of 3d matrix scaled
# DN    device null
# DODECA vectors of this object scaled
# EM    var E of 3d matrix scaled
# EDGE    absolute value of EDGEi
# EDGEi    OBJ current edge
# EDGE$i OBJ $i'th edge $i i-1..NE
# FP    floating point scaling 10^4
# FH    floating point rounding FP/2
# FM    var F of 3d matrix scaled
# GM    var G of 3d matrix scaled
# H    heading -PI..PI scaled
# HX    middle x screen pos 300
# HY    middle y screen pos 400
# HM    var H of 3d matrix scaled
# IM    var I of 3d matrix scaled
# KQ    bytes per pixel, 2 for Kindle3
# MX    max x pos 0:599
# MY    max y pos 0:799
# NE    OBJ number of edges
# NV    OBJ number of points
# OBJ   current object scaled
# OCTA  vectors of this object
# P    pitch -PI..PI scaled
# P2    2*PI scaled
# PI    PI scaled
# PN    -PI scaled
# SB    sin(bank)
# SH    sin(heading)
# SP    sin(pitch)
# TK    device temporary key
# TETRA vectors of this object scaled
# U    computing 2D projection scaled
# V    computing 2D projection scaled
# VECTX$i OBJ $i'th X coord i=1..NV scaled
# VECTY$i OBJ $i'th Y coord i=1..NV scaled
# VECTZ$i OBJ $i'th Z coord i=1..NV scaled
# VX    virtual x size, 300 for Kindle 3
# XX    computing 2D projection scaled
# YY    computing 2D projection scaled
# ZZ    computing 2D projection scaled
# x1    temp coordinate scaled
# x2    temp coordinate scaled
# X3    computing 2D projection scaled
# y1    temp coordinate scaled
# y2    temp coordinate scaled
# Y3    computing 2D projection scaled
# Z3    computing 2D projection scaled
#-----------------------------

#=============================
# initvar - init global vars
#-----------------------------
initvar() {
  
DN=/dev/null DF=/dev/fb0 DK=/dev/input/event0 MX=599 MY=799 HX=300 HY=400
  TK
=/tmp/key; echo > $TK
  
set $(eips -i|grep line_length); VX=$4
  FP
=10000 # fixed-point scale factor
  
FH=5000 # FP/2 (for rounding)
  
PI=31416 # PI (scaled)
  
P2=62832 # 2*PI (scaled)
  
PN=-31416 # -PI (scaled)
  
KQ=# K3 pixels per byte
  
P=0 B=0 H=0 D=135000
  BLOCK
="
     8
      52500     -20000      32500
     -52500     -20000      32500
     -52500     -20000     -32500
      52500     -20000     -32500
      52500      20000      32500
     -52500      20000      32500
     -52500      20000     -32500
      52500      20000     -32500
     17
     -1 2 3 4 1 5 6 7 8 5
     -2 6 3 7 2
     -4 8"
  
DODECA="
     20
          0     -21408      56052
      34644     -34644      34644
      56052          0      21408
      34644      34644      34644
          0      21408      56052
     -34644      34644      34644
     -56052          0      21408
     -34644     -34644      34644
     -21408     -56052          0
      21408     -56052          0
      21408      56052          0
     -21408      56052          0
     -34644     -34644     -34644
          0     -21408     -56052
      34644     -34644     -34644
      56052          0     -21408
      34644      34644     -34644
          0      21408     -56052
     -34644      34644     -34644
     -56052          0     -21408
    40
     -1 2 3 4 5 6 7 8 1 5
     -14 15 16 17 18 19 20 13 14 18
     -8 9 10 2
     -13 9
     -15 10
     -4 11 12 6
     -17 11
     -19 12
     -16 3
     -7 20"
  
OCTA="
     6
          0      75000          0
          0          0     -25000
     -25000          0          0
          0          0      25000
      25000          0          0
          0     -75000          0
     14
     -1 2 6 4 1 3 6 5 1
     -2 3 4 5 2"
  
TETRA="
     4
     -45000     -45000     -45000
      45000     -45000     -45000
          0      45000          0
          0      45000     -45000
     8
     -1 2 3 1 4 2 3 4"
}

#===========================
# eupd - eink update display
#---------------------------
eupd() {
  echo 
>/proc/eink_fb/update_display # (for k3 and earlier)
#  eips '' # (for k4 and newer)
}

#=============================
# line - Bresenham's line algorithm
# usage: line x0 y0 x y
#-----------------------------
line() {
  
local x0=$(($1/$KQ)) y0=$2 x=$(($3/$KQ)) y=$4local dx dy sx sy e e2 xw xp=$x0
  
if [[ $x -gt $x0 ]];then dx=$((x-x0));sx=1; else dx=$((x0-x));sx=-1;fi
  
if [[ $y -gt $y0 ]];then dy=$((y0-y));sy=1; else dy=$((y-y0));sy=-1;fi
  e
=$((dx+dy)); echo -en "\xff${b#?}" | dd of=$DF bs=1 count=1 seek=$((y*VX+x)) 2>$DN
  
while [[ $x0 -ne $x -o $y0 -ne $y ]];do
    
xw=$((x0-xp)); [[ $xw -lt 0 ]]&& xw=$((-xw))
    echo -
en "\xff${b#?}" | dd  of=$DF bs=1 count=1 seek=$((y0*VX+x0)) 2>$DN
    
e2=$((e+e))
    if [[ 
$e2 -gt $dy ]];then e=$((e+dy));x0=$((x0+sx));fi
    
if [[ $e2 -lt $dx ]];then
      xp
=$x0;e=$((e+dx));y0=$((y0+sy))
    
fi
  done
;
}


#=============================
# sin - Taylor series sine function
# sin x ~= x - x^3/3! + x^5/5! - x^7/7!
# usage: sin x  (-PI..PI)
# STDOUT: sin(x)  (0..10K)
# O(5) @ +-90 deg, O(7) @ +-180 deg
# 10K fixed-point scale factor 
#------------------------------
sin() {
  local x=$1
  while [[ 
$x -gt $PI ]];do x=$((x-$P2));done
  while [[ 
$x -lt $PN ]];do x=$((x+$P2));done
  local a=
$x xs=$(((x*x+FH)/FP))
  local xn=$(((x*xs+FH)/FP)) fn=6 # O(3)
  a=$((a-(xn+fn/2)/fn)) xn=$(((xn*xs+FH)/FP)) fn=$((fn*20)) # O(5)
  a=$((a+(xn+fn/2)/fn)) xn=$(((xn*xs+FH)/FP)) fn=$((fn*42)) # O(7)
  a=$((a-(xn+fn/2)/fn)); echo 
$a
}

#=============================
# cos(x) = sin(pi/2-x) function
# usage: cos x  (-PI..PI)
# STDOUT: cos(x)  (0..10K)
# 10K fixed-point scale factor 
#------------------------------
cos() {
sin $((PI/2+$(mult -10000 $1)))
}

#=============================
# mult -  multiply scaled numbers
# usage: mult x y
# STDOUT: x * y  (0..10K)
# 10K fixed-point scale factor 
#------------------------------
mult() {
echo $((($1*$2+FH)/FP))
}

#=============================
# mat3d - compute projection matrix coefficients
# usage: mat3d 
#-----------------------------
mat3d() {
  CH=$(cos 
$H) SH=$(sin $H)
  CP=$(cos 
$P) SP=$(sin $P)
  CB=$(cos 
$B) SB=$(sin $B)
  AM=$(( $(mult CB CH) - $(mult $(mult SH SP) SB)));    #   cos(B) cos(H) - sin(P) sin(B) sin(H)
  BM=$((-$(mult CB SH) - $(mult $(mult SP CH) SB)));    # - cos(B) sin(H) - sin(P) sin(B) cos(H)
  CM=$(( $(mult CP SB)));                #   sin(B) cos(P)
  DM=$(( $(mult CP SH)));                #   sin(H) cos(P)
  EM=$(( $(mult CP CH)));                #   cos(P) cos(H)
  FM=
$SP;                        #   sin(P)
  GM=$((-$(mult SB CH) - $(mult $(mult SH SP) CB)));    # - cos(H) sin(B) - sin(H) sin(P) cos(B)
  HM=$(( $(mult SB SH) - $(mult $(mult CH SP) CB)));    #   sin(H) sin(B) - sin(P) cos(H) cos(B)
  IM=$(( $(mult CP CB)));                #   cos(P) cos(B)
}

#==============================
# proj2d - project a vector
# usage: proj2d 
#------------------------------
proj2d() {
  XX=$((XX-XV))
  YY=$((YY-YV))
  ZZ=$((ZZ-ZV))
  X3=$(($(mult AM XX)+$(mult BM YY)+$(mult CM ZZ)))
  Y3=$(($(mult DM XX)+$(mult EM YY)+$(mult FM ZZ)))
  Z3=$(($(mult GM XX)+$(mult HM YY)+$(mult IM ZZ)))
  U=$((HX+ $(mult $(mult 300000 D)/Y3 X3))); # 30 horiz ppu adjustment
  V=$((HY+ $(mult $(mult 300000 D)/Y3 Z3))); # 30 vert ppu adjustment
}

#=============================
# plot 3D object on 2D screen
# usage: plot object
#-----------------------------
plot() {
  # read object vectors and edges:
  NV=$1; shift; # number of points
  for i in $(seq 
$NV); do
    echo $((VECTX
$i=$1)) $((VECTY$i=$2)) $((VECTZ$i=$3)) > $DN
    shift; shift; shift
  done
  NE=$1; shift  # number of edges
  for i in $(seq 
$NE); do
    echo $((EDGE
$i=$1)) > $DN; shift
  done

  while :; do

    # project object:
    eips -c
    eips  0 39 "
P=$P H=$H B=$B D=$D"
    mat3d
    XV=$((-$(mult $(mult D CP) SH)))
    YV=$((-$(mult $(mult D CP) CH)))
    ZV=$((-$(mult D SP)))
    for i in $(seq 
$NE); do
      echo $((EDGEi=EDGE
$i))>$DN
      EDGE=
$EDGEi
      [[ 
$EDGE -le 0 ]]&& EDGE=$((-EDGE)) # ABS(EDGE)
      echo $((XX=VECTX
$EDGE))>$DN; echo $((YY=VECTY$EDGE))>$DN; echo $((ZZ=VECTZ$EDGE))>$DN
      proj2d
      if [[ 
$EDGEi -gt 0 ]]; then
        x2=
$U  y2=$V
        line 
$x1 $y1 $x2 $y2; eupd
      fi
      x1=
$U  y1=$V
    done

    if [[ -s 
$TK ]];then # get new Pitch, Heading, Bank, or Distance from keyboard input  
      set $(cat 
$TK); local k=$1; echo > $TK; waitforkey > $TK &
      case 
$k in
        25)    P=$((P+P2/36));;    # p +10dg
        35)    H=$((H+P2/36));;    # h +10dg
        48)    B=$((B+P2/36));;    # b +10dg
        44)    D=$((D-2*FP));;        # z zoom in
        22)    D=$((D+2*FP));;        # u zoom out
        19)    P=0 H=0 B=0 D=135000;;    # r home
        102)    return;;        # HOME quit program
      esac
    fi 
   
  done
}

#=============================
# main program entry point
#-----------------------------                  
lipc-set-prop com.lab126.powerd preventScreenSaver 1
killall -stop cvm # pause framework
initvar # init global vars
while :; do
  eips -c 
  eips 15  4 "    
_/_/_/    3D"
  eips 15  5 "         
_/  4.0 _/_/_/"
  eips 15  6 "    
_/_/    by  _/    _/"
  eips 15  7 "       
_/  PoP _/    _/"
  eips 15  8 "
_/_/_/        _/    _/"
  eips 15  9 "             
_/_/_/"
  eips 15 15 "
Select object to plot:"
  eips 15 16 "
Q    BLOCK"
  eips 15 17 "
W    OCTAHEDRON"
  eips 15 18 "
E    TETRAHEDRON"
  eips 15 19 "
R    DODECAHEDRON"
  eips 15 20 "
Home Quit"
  eips 15 28 "
While object is being plot:"
  eips 15 29 "
R      reset view"
  eips 15 30 "
P      pitch"
  eips 15 31 "
H      heading"
  eips 15 32 "
B      bank"
  eips 15 33 "
Z      zoom"
  eips 15 34 "
U      un zoom"
  eips 15 35 "
Home Select new object"
  waitforkey > 
$TK; set $(cat $TK); local k=$1;  echo > $TK;
  case 
$k in
    16)    plot 
$BLOCK;;
    17)    plot 
$OCTA;;
    18)    plot 
$TETRA;; 
    19)    plot 
$DODECA;;
   102) killall -cont cvm; lipc-set-prop com.lab126.powerd preventScreenSaver 0; exit;;
  esac 
done 
extract the attached zip to preserve tabs, escaped characters, etc.

In particular, note the echo $((VECTX$i=$1))>$DN construct to dynamically define and assign VECTOR$i variables in absence of shell arrays .

I have included definitions for a couple of objects. It should be somewhat easy to modify and define vectors for more.

Use the keyboard to interactively control viewing angles and distance. When in trouble, press Home key twice to exit.

WARNINGS:

Well, this script is certainly not fast, and only meant for as a fun demo in /bin/sh education. It will only work on the K3, when started from an ssh session or from a launchpad shortcut. I have not thoroughly tested it (I am a bit surprised that it even works at all given the /bin/sh quirks I encountered). A "C formula'42" implementation would be a relief.

It will fail displaying correctly when zooming too close. Proper clipping is not implemented.

And Oh, sorry for too much white space... I felt it was cryptic enough without "optimizing" further.
Attached Files
File Type: zip 3D_4.0.zip (3.38 MB, 193 views)
PoP is online now   Reply With Quote
Old 04-29-2012, 11:08 PM   #70
geekmaster
Carpe diem, c'est la vie.
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: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
Quote:
Originally Posted by PoP View Post

After seeing geekmaster's "eink algorithmic art" demos, I fancied and dusted off a 30 years old demo -- see the included original Byte Magazine extract! (with a bonus p486 rescan to fix original download URL )

Teaser code for illustrative purpose only,
Spoiler:
PHP Code:
#!/bin/sh
#=============================
# 3D - Projects a 3D object to a 2D screen
# Inspired by "eink algorithmic art" thread at https://www.mobileread.com/forums/showthread.php?t=172182
# usage: 3D
# Only tested on Kindle3 (firmware 3.3, Busybox 1.7.2 Almquist Shell)
#------------------------------
# Copyright (c) 2012 PoP under MIT License:
# Copyright (c) 2012 geekmaster under MIT License:
# http://www.opensource.org/licenses/mit-license.php
#------------------------------
# Revision History:
# v4.0  2012-04-05 PoP            ported to Kindle3                  
# v3.0  1986-12-20 PoP            ported to IBM PC
# v2.0  1983-09-14 PoP            ported to C=64
# v1.0  1982-11-11 PoP            ported to Apple][
# v0.0  1981-01-01 A. Pickholtz   see pp 474-505 BYTE magazine: http://malus.exotica.org.uk/~buzz/byte/pdf/BYTE%20Vol%2007-11%201982-11%20Graphics.pdf
#------------------------------
# Global vars:
# B    bank -PI..PI scaled
# CB    cos(bank)
# CH    cos(heading)
# CM    var C of 3d matrix  scaled
# CP    cos(pitch)
# BLOCK vectors of this object scaled
# D    distance from camera scaled
# DF    device frame buffer
# DK    device keyboard
# DM    var D of 3d matrix scaled
# DN    device null
# DODECA vectors of this object scaled
# EM    var E of 3d matrix scaled
# EDGE    absolute value of EDGEi
# EDGEi    OBJ current edge
# EDGE$i OBJ $i'th edge $i i-1..NE
# FP    floating point scaling 10^4
# FH    floating point rounding FP/2
# FM    var F of 3d matrix scaled
# GM    var G of 3d matrix scaled
# H    heading -PI..PI scaled
# HX    middle x screen pos 300
# HY    middle y screen pos 400
# HM    var H of 3d matrix scaled
# IM    var I of 3d matrix scaled
# KQ    bytes per pixel, 2 for Kindle3
# MX    max x pos 0:599
# MY    max y pos 0:799
# NE    OBJ number of edges
# NV    OBJ number of points
# OBJ   current object scaled
# OCTA  vectors of this object
# P    pitch -PI..PI scaled
# P2    2*PI scaled
# PI    PI scaled
# PN    -PI scaled
# SB    sin(bank)
# SH    sin(heading)
# SP    sin(pitch)
# TK    device temporary key
# TETRA vectors of this object scaled
# U    computing 2D projection scaled
# V    computing 2D projection scaled
# VECTX$i OBJ $i'th X coord i=1..NV scaled
# VECTY$i OBJ $i'th Y coord i=1..NV scaled
# VECTZ$i OBJ $i'th Z coord i=1..NV scaled
# VX    virtual x size, 300 for Kindle 3
# XX    computing 2D projection scaled
# YY    computing 2D projection scaled
# ZZ    computing 2D projection scaled
# x1    temp coordinate scaled
# x2    temp coordinate scaled
# X3    computing 2D projection scaled
# y1    temp coordinate scaled
# y2    temp coordinate scaled
# Y3    computing 2D projection scaled
# Z3    computing 2D projection scaled
#-----------------------------

#=============================
# initvar - init global vars
#-----------------------------
initvar() {
  
DN=/dev/null DF=/dev/fb0 DK=/dev/input/event0 MX=599 MY=799 HX=300 HY=400
  TK
=/tmp/key; echo > $TK
  
set $(eips -i|grep line_length); VX=$4
  FP
=10000 # fixed-point scale factor
  
FH=5000 # FP/2 (for rounding)
  
PI=31416 # PI (scaled)
  
P2=62832 # 2*PI (scaled)
  
PN=-31416 # -PI (scaled)
  
KQ=# K3 pixels per byte
  
P=0 B=0 H=0 D=135000
  BLOCK
="
     8
      52500     -20000      32500
     -52500     -20000      32500
     -52500     -20000     -32500
      52500     -20000     -32500
      52500      20000      32500
     -52500      20000      32500
     -52500      20000     -32500
      52500      20000     -32500
     17
     -1 2 3 4 1 5 6 7 8 5
     -2 6 3 7 2
     -4 8"
  
DODECA="
     20
          0     -21408      56052
      34644     -34644      34644
      56052          0      21408
      34644      34644      34644
          0      21408      56052
     -34644      34644      34644
     -56052          0      21408
     -34644     -34644      34644
     -21408     -56052          0
      21408     -56052          0
      21408      56052          0
     -21408      56052          0
     -34644     -34644     -34644
          0     -21408     -56052
      34644     -34644     -34644
      56052          0     -21408
      34644      34644     -34644
          0      21408     -56052
     -34644      34644     -34644
     -56052          0     -21408
    40
     -1 2 3 4 5 6 7 8 1 5
     -14 15 16 17 18 19 20 13 14 18
     -8 9 10 2
     -13 9
     -15 10
     -4 11 12 6
     -17 11
     -19 12
     -16 3
     -7 20"
  
OCTA="
     6
          0      75000          0
          0          0     -25000
     -25000          0          0
          0          0      25000
      25000          0          0
          0     -75000          0
     14
     -1 2 6 4 1 3 6 5 1
     -2 3 4 5 2"
  
TETRA="
     4
     -45000     -45000     -45000
      45000     -45000     -45000
          0      45000          0
          0      45000     -45000
     8
     -1 2 3 1 4 2 3 4"
}

#===========================
# eupd - eink update display
#---------------------------
eupd() {
  echo 
>/proc/eink_fb/update_display # (for k3 and earlier)
#  eips '' # (for k4 and newer)
}

#=============================
# line - Bresenham's line algorithm
# usage: line x0 y0 x y
#-----------------------------
line() {
  
local x0=$(($1/$KQ)) y0=$2 x=$(($3/$KQ)) y=$4local dx dy sx sy e e2 xw xp=$x0
  
if [[ $x -gt $x0 ]];then dx=$((x-x0));sx=1; else dx=$((x0-x));sx=-1;fi
  
if [[ $y -gt $y0 ]];then dy=$((y0-y));sy=1; else dy=$((y-y0));sy=-1;fi
  e
=$((dx+dy)); echo -en "\xff${b#?}" | dd of=$DF bs=1 count=1 seek=$((y*VX+x)) 2>$DN
  
while [[ $x0 -ne $x -o $y0 -ne $y ]];do
    
xw=$((x0-xp)); [[ $xw -lt 0 ]]&& xw=$((-xw))
    echo -
en "\xff${b#?}" | dd  of=$DF bs=1 count=1 seek=$((y0*VX+x0)) 2>$DN
    
e2=$((e+e))
    if [[ 
$e2 -gt $dy ]];then e=$((e+dy));x0=$((x0+sx));fi
    
if [[ $e2 -lt $dx ]];then
      xp
=$x0;e=$((e+dx));y0=$((y0+sy))
    
fi
  done
;
}


#=============================
# sin - Taylor series sine function
# sin x ~= x - x^3/3! + x^5/5! - x^7/7!
# usage: sin x  (-PI..PI)
# STDOUT: sin(x)  (0..10K)
# O(5) @ +-90 deg, O(7) @ +-180 deg
# 10K fixed-point scale factor 
#------------------------------
sin() {
  local x=$1
  while [[ 
$x -gt $PI ]];do x=$((x-$P2));done
  while [[ 
$x -lt $PN ]];do x=$((x+$P2));done
  local a=
$x xs=$(((x*x+FH)/FP))
  local xn=$(((x*xs+FH)/FP)) fn=6 # O(3)
  a=$((a-(xn+fn/2)/fn)) xn=$(((xn*xs+FH)/FP)) fn=$((fn*20)) # O(5)
  a=$((a+(xn+fn/2)/fn)) xn=$(((xn*xs+FH)/FP)) fn=$((fn*42)) # O(7)
  a=$((a-(xn+fn/2)/fn)); echo 
$a
}

#=============================
# cos(x) = sin(pi/2-x) function
# usage: cos x  (-PI..PI)
# STDOUT: cos(x)  (0..10K)
# 10K fixed-point scale factor 
#------------------------------
cos() {
sin $((PI/2+$(mult -10000 $1)))
}

#=============================
# mult -  multiply scaled numbers
# usage: mult x y
# STDOUT: x * y  (0..10K)
# 10K fixed-point scale factor 
#------------------------------
mult() {
echo $((($1*$2+FH)/FP))
}

#=============================
# mat3d - compute projection matrix coefficients
# usage: mat3d 
#-----------------------------
mat3d() {
  CH=$(cos 
$H) SH=$(sin $H)
  CP=$(cos 
$P) SP=$(sin $P)
  CB=$(cos 
$B) SB=$(sin $B)
  AM=$(( $(mult CB CH) - $(mult $(mult SH SP) SB)));    #   cos(B) cos(H) - sin(P) sin(B) sin(H)
  BM=$((-$(mult CB SH) - $(mult $(mult SP CH) SB)));    # - cos(B) sin(H) - sin(P) sin(B) cos(H)
  CM=$(( $(mult CP SB)));                #   sin(B) cos(P)
  DM=$(( $(mult CP SH)));                #   sin(H) cos(P)
  EM=$(( $(mult CP CH)));                #   cos(P) cos(H)
  FM=
$SP;                        #   sin(P)
  GM=$((-$(mult SB CH) - $(mult $(mult SH SP) CB)));    # - cos(H) sin(B) - sin(H) sin(P) cos(B)
  HM=$(( $(mult SB SH) - $(mult $(mult CH SP) CB)));    #   sin(H) sin(B) - sin(P) cos(H) cos(B)
  IM=$(( $(mult CP CB)));                #   cos(P) cos(B)
}

#==============================
# proj2d - project a vector
# usage: proj2d 
#------------------------------
proj2d() {
  XX=$((XX-XV))
  YY=$((YY-YV))
  ZZ=$((ZZ-ZV))
  X3=$(($(mult AM XX)+$(mult BM YY)+$(mult CM ZZ)))
  Y3=$(($(mult DM XX)+$(mult EM YY)+$(mult FM ZZ)))
  Z3=$(($(mult GM XX)+$(mult HM YY)+$(mult IM ZZ)))
  U=$((HX+ $(mult $(mult 300000 D)/Y3 X3))); # 30 horiz ppu adjustment
  V=$((HY+ $(mult $(mult 300000 D)/Y3 Z3))); # 30 vert ppu adjustment
}

#=============================
# plot 3D object on 2D screen
# usage: plot object
#-----------------------------
plot() {
  # read object vectors and edges:
  NV=$1; shift; # number of points
  for i in $(seq 
$NV); do
    echo $((VECTX
$i=$1)) $((VECTY$i=$2)) $((VECTZ$i=$3)) > $DN
    shift; shift; shift
  done
  NE=$1; shift  # number of edges
  for i in $(seq 
$NE); do
    echo $((EDGE
$i=$1)) > $DN; shift
  done

  while :; do

    # project object:
    eips -c
    eips  0 39 "
P=$P H=$H B=$B D=$D"
    mat3d
    XV=$((-$(mult $(mult D CP) SH)))
    YV=$((-$(mult $(mult D CP) CH)))
    ZV=$((-$(mult D SP)))
    for i in $(seq 
$NE); do
      echo $((EDGEi=EDGE
$i))>$DN
      EDGE=
$EDGEi
      [[ 
$EDGE -le 0 ]]&& EDGE=$((-EDGE)) # ABS(EDGE)
      echo $((XX=VECTX
$EDGE))>$DN; echo $((YY=VECTY$EDGE))>$DN; echo $((ZZ=VECTZ$EDGE))>$DN
      proj2d
      if [[ 
$EDGEi -gt 0 ]]; then
        x2=
$U  y2=$V
        line 
$x1 $y1 $x2 $y2; eupd
      fi
      x1=
$U  y1=$V
    done

    if [[ -s 
$TK ]];then # get new Pitch, Heading, Bank, or Distance from keyboard input  
      set $(cat 
$TK); local k=$1; echo > $TK; waitforkey > $TK &
      case 
$k in
        25)    P=$((P+P2/36));;    # p +10dg
        35)    H=$((H+P2/36));;    # h +10dg
        48)    B=$((B+P2/36));;    # b +10dg
        44)    D=$((D-2*FP));;        # z zoom in
        22)    D=$((D+2*FP));;        # u zoom out
        19)    P=0 H=0 B=0 D=135000;;    # r home
        102)    return;;        # HOME quit program
      esac
    fi 
   
  done
}

#=============================
# main program entry point
#-----------------------------                  
lipc-set-prop com.lab126.powerd preventScreenSaver 1
killall -stop cvm # pause framework
initvar # init global vars
while :; do
  eips -c 
  eips 15  4 "    
_/_/_/    3D"
  eips 15  5 "         
_/  4.0 _/_/_/"
  eips 15  6 "    
_/_/    by  _/    _/"
  eips 15  7 "       
_/  PoP _/    _/"
  eips 15  8 "
_/_/_/        _/    _/"
  eips 15  9 "             
_/_/_/"
  eips 15 15 "
Select object to plot:"
  eips 15 16 "
Q    BLOCK"
  eips 15 17 "
W    OCTAHEDRON"
  eips 15 18 "
E    TETRAHEDRON"
  eips 15 19 "
R    DODECAHEDRON"
  eips 15 20 "
Home Quit"
  eips 15 28 "
While object is being plot:"
  eips 15 29 "
R      reset view"
  eips 15 30 "
P      pitch"
  eips 15 31 "
H      heading"
  eips 15 32 "
B      bank"
  eips 15 33 "
Z      zoom"
  eips 15 34 "
U      un zoom"
  eips 15 35 "
Home Select new object"
  waitforkey > 
$TK; set $(cat $TK); local k=$1;  echo > $TK;
  case 
$k in
    16)    plot 
$BLOCK;;
    17)    plot 
$OCTA;;
    18)    plot 
$TETRA;; 
    19)    plot 
$DODECA;;
   102) killall -cont cvm; lipc-set-prop com.lab126.powerd preventScreenSaver 0; exit;;
  esac 
done 
extract the attached zip to preserve tabs, escaped characters, etc.

In particular, note the echo $((VECTX$i=$1))>$DN construct to dynamically define and assign VECTOR$i variables in absence of shell arrays .

I have included definitions for a couple of objects. It should be somewhat easy to modify and define vectors for more.

Use the keyboard to interactively control viewing angles and distance. When in trouble, press Home key twice to exit.

WARNINGS:

Well, this script is certainly not fast, and only meant for as a fun demo in /bin/sh education. It will only work on the K3, when started from an ssh session or from a launchpad shortcut. I have not thoroughly tested it (I am a bit surprised that it even works at all given the /bin/sh quirks I encountered). A "C formula'42" implementation would be a relief.

It will fail displaying correctly when zooming too close. Proper clipping is not implemented.

And Oh, sorry for too much white space... I felt it was cryptic enough without "optimizing" further.
I like the array idea. That should be faster than using "cut -b" to extract bytes from an "array" string.

There IS a C "formula 42" in the original dithermatron program. I have been developing faster and simpler replacements for it that also support the K4 in main boot mode (hybrid K3/K5 eink driver mode), and it does dithering to display 256 colors on the 16-color display (the framebuffer is always dithered 16-color on the K4 and K4 by the image display software, such as eips and screensavers).

The C "gmlib" code supports all eink kindles. I could wrap it into a simple replacement for eips, that you call to draw graphics primitives (lines, circles, etc.) and do eink updates. Go check out the demos in the dithermatron and newtrix threads.

I have in my "to do" list a gmlib function that you call "gmlib opcode params" that can do graphics stuff from shell scripts. I plan to add keyboard and touchscreen support to gmlib too, and later sound and network functions (for network video games -- turn-based games should work well, especially with the new 256-color dithered drawing functions I added).

With the (unfinished) gmlib program, it can be used in shell scripts to make all the existing "algorithmic art" scripts a LOT faster... I will do that soon, after I complete a couple C things I have in progress.
geekmaster is offline   Reply With Quote
Old 05-10-2012, 06:41 PM   #71
Dweia
Junior Member
Dweia plays well with othersDweia plays well with othersDweia plays well with othersDweia plays well with othersDweia plays well with othersDweia plays well with othersDweia plays well with othersDweia plays well with othersDweia plays well with othersDweia plays well with othersDweia plays well with others
 
Posts: 7
Karma: 2614
Join Date: May 2012
Device: Kindle Touch
Ants in the Kindle Touch!

Here's a little script that lets Ants (well, ONE Ant) crawl over the display of the Kindle Touch.

usage: ants [-c]
  • without parameter, the Ant will start crawling right over the displayed document or picture (interesting effect to let it run over a title-page of a book)
  • with parameter "-c", the screen is cleared before the Ant starts its work. In this case after about 12000 iterations (displayed in the top left corner), the Ant will build a "street" as any self-respecting ant will do.
  • the program will stop the framework while it runs and SHOULD reactivate the framework when it is killed or ends. (However, once or twice it didn't, so best to not try this without shell-access)
  • the program can be stopped with tapping the touchscreen or the home-button, with the only disadvantage being that the tap WILL be interpreted by the framework
  • when the Ant reaches one of the borders, it stops
  • when the Ant runs longer than the normal screensaver time, you will need to press the power-button, because the Kindle falls asleep immediately after end of the program.
PHP Code:
#!/bin/sh

#===========================
# cleanup - delete tmp files
#---------------------------
trap 'cleanup ; exit' SIGINT SIGTERM
cleanup
() {
  
killall -cont cvm Xorg 2>/dev/null # resume framework
  
killall waitforkey
  eips 
-v 2 2 "done"
}

#===========================
# initvar - init global vars
#---------------------------
initvar() {
  [ 
"$1" "-c" ] && eips -c
  set 
$(eips -i|grep xres) ; XM=$2 YM=$4 LL=$#XMax, YMax, LineLength
  
AX=$((XM/2)) AY=$((YM/2)) #Ant starts in the middle of the screen
  
AD=#Ant Direction: 0=up 1=right 2=down 3=left

  
WK=$(mktemp "/tmp/WK.XXXXXX"# file to watch for exit
  
waitforkey $WK &

  
DN=/dev/null DF=/dev/fb0 
}

#===================================
# usage: setpix x y color; color should be in hex: e.g. 66 AA etc.
#-----------------------------------
setpix() {
  
local x=$1 y=$2 c=$3
  
echo -ne "\x$cdd of=$DF bs=1 count=1 seek=$((y*LL+x)) 2>$DN
}

#===================================                                                            
# usage: getpix x y; return: pixel value in hex on stdout as AA 66 ...
#-----------------------------------                                                            
getpix() {                                                                                      
  
local x=$1 y=$2
  hexdump 
--$((y*LL+x)) -n 1 -'/1 "%02X"' $DF
}
    
#===================================
# loop to let the ant crawl
#-----------------------------------
loop() {
  
i=0
  
while [ ! -s $WK ] ; do # loop until file $WK is not empty
    
i=$((i+1))
    
p=$(getpix $AX $AY)
    case 
"$pin
      
"00"|"11"|"22"|"33"|"44"|"55"|"66"|"77")
        
setpix $AX $AY FF
        AD
=$((AD+1)); [ $AD -gt 3 ] && AD=;; 
      *)
        
setpix $AX $AY 00
        AD
=$((AD-1)); [ $AD -lt 0 ] && AD=;; 
    
esac
    
case "$ADin
      
"0"AY=$((AY-1)) ; [ $AY -lt 0 ]   && exit ;; 
      
"1"AX=$((AX+1)) ; [ $AX -gt $XM ] && exit ;;
      
"2"AY=$((AY+1)) ; [ $AY -gt $YM ] && exit ;;
      *)   
AX=$((AX-1)) ; [ $AX -lt 0 ]   && exit ;;
    
esac
    
[ $((i%100)) -eq 0 ] && (eips -d l=0,w=110,h=60eips -v 2 1 "$i")
    [ $((
i%100)) ] && eips ''
  
done
  rm $WK 
}

killall -stop Xorg cvm 2>/dev/null # pause framework

initvar $1
loop
cleanup

killall 
-cont cvm Xorg 2>/dev/null # resume framework 
Dweia is offline   Reply With Quote
Old 05-10-2012, 07:05 PM   #72
geekmaster
Carpe diem, c'est la vie.
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: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
It took me awhile to remember that waitforkey does not work while the kindle is in screensaver mode. Tapping the power button while this program is running fixes that though.

In the past, waitforkey returned a keycode for rotation events even when in screensaver mode. After upgrading to 5.1.0, I see that it no longer returns rotation events. Perhaps the accelerometer defaults to OFF instead of ON as it did in 5.0.x firmware.

Last edited by geekmaster; 05-09-2016 at 06:06 PM.
geekmaster is offline   Reply With Quote
Old 05-10-2012, 09:51 PM   #73
geekmaster
Carpe diem, c'est la vie.
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: 6,433
Karma: 10773668
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
Quote:
Originally Posted by PoP View Post
... see the included original Byte Magazine extract! (with a bonus p486 rescan to fix original download URL )
Yeah, I loved the early Byte Magazine issues. I remember implementing the "serendipitous circles" algorithm published in Byte, which inspired my interest in computer graphics that is still strong with me. I still have the first 7 years of Byte Magazine on microfiche (but my fiche reader is broken), and I have the first several years in leather-bound books purchased from the publisher. Those (and a few others, like my early "Dr. Dobb's Journal of Computer Calisthenics and Orthodontia" magazine, with later issues simply called "DDJ") were the primary source of precious computer information tidbits we waited hungrily for every month, for the same stuff you can find in vast troves in seconds with the magic of the internet. And there was no downloading back then, you actually had to type EVERY LINE of code from every program published in those magazines (on a freaking slow teletype in many cases)!

I am thinking somebody needs to port this 3D program to C now that we have the tcc package.


Last edited by geekmaster; 05-10-2012 at 09:57 PM.
geekmaster is offline   Reply With Quote
Old 06-13-2012, 04:11 PM   #74
PoP
 curly᷂͓̫̙᷊̥̮̾ͯͤͭͬͦͨ ʎʌɹnɔ
PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.PoP ought to be getting tired of karma fortunes by now.
 
PoP's Avatar
 
Posts: 3,002
Karma: 50506927
Join Date: Dec 2010
Location: ♁ ᴺ₄₅°₃₀' ᵂ₇₃°₃₇' ±₆₀"
Device: K3₃.₄.₃ PW3&4₅.₁₃.₃
done : 3D.c version 5.0
PoP is online now   Reply With Quote
Old 07-01-2012, 09:40 PM   #75
twobob
( ͡° ͜ʖ ͡°){ʇlnɐɟ ƃǝs}Týr
twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.twobob ought to be getting tired of karma fortunes by now.
 
twobob's Avatar
 
Posts: 6,586
Karma: 6299991
Join Date: Jun 2012
Location: uti gratia usura (Yao ying da ying; Mo ying da yieng)
Device: PW-WIFI|K5-3G+WIFI| K4|K3-3G|DXG|K2| Rooted Nook Touch
Superb Seggie

Quote:
Originally Posted by PoP View Post
Seggies on my 3 in certain views. overflow-no doubt

But superb. Love this. Excellent
twobob 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
Scripting with epub-meta averyml Calibre 20 11-17-2016 10:13 AM
Bunny + Scripting + Calibre = here tBunnyMan Introduce Yourself 4 02-06-2012 12:16 AM
Possible scripting engine for Sigil Valloric Sigil 48 10-17-2009 09:58 AM
Any NetNewsWire Scripting Pros out there? adinb Sony Reader 0 02-25-2007 01:44 AM


All times are GMT -4. The time now is 08:01 AM.


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