Check-in [f4dd8495bc]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Sensor stuff
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: f4dd8495bcd0ccc60910e02c58eb8bd852ca0ce2
User & Date: bernd 2014-01-16 16:23:29
Context
2014-01-17
00:13
Rewrite for Gforth\NativeActivity check-in: af01051ada user: bernd tags: trunk
2014-01-16
16:23
Sensor stuff check-in: f4dd8495bc user: bernd tags: trunk
2014-01-13
15:34
Delete references check-in: 3874dedc13 user: bernd tags: trunk
Changes

Changes to gles2/android.fs.

   307    307   	APP_CMD_START of [: ." app start" cr ;] $err endof
   308    308   	APP_CMD_STOP of [: ." app stop" cr ;] $err endof
   309    309   	dup [: ." app cmd " . cr ;] $err
   310    310       endcase ; is acmd
   311    311   
   312    312   also jni
   313    313   
          314  +: android-characters ( string -- )  jstring>sstring unkeys jfree ;
          315  +: android-unicode    ( uchar -- )   >xstring unkeys ;
          316  +: android-keycode    ( keycode -- ) keycode>keys unkeys ;
          317  +
   314    318   : android-key ( event -- )
   315         -    >o getAction dup 2 = IF  drop ."
          319  +    >o getAction dup 2 = IF  drop
   316    320   	getKeyCode dup 0= IF
   317         -	    drop getCharacters jstring>sstring unkeys jfree
          321  +	    drop getCharacters android-characters
   318    322   	ELSE
   319         -	    keycode>keys unkeys
          323  +	    android-keycode
   320    324   	THEN
   321    325       ELSE
   322    326   	0= IF  getUnicodeChar dup 0>
   323         -	    IF    >xstring unkeys
   324         -	    ELSE  drop  getKeyCode  keycode>keys  unkeys
          327  +	    IF    android-unicode
          328  +	    ELSE  drop  getKeyCode  android-keycode
   325    329   	    THEN
   326    330   	THEN
   327         -    THEN o> ;
          331  +    THEN o ]gref o> ;
   328    332   
   329         -Defer android-touch ( event -- )
   330         -' drop IS android-touch
          333  +: android-touch ( event -- ) >o
          334  +    me-getAction *input action !
          335  +    getFlags *input flags !
          336  +    getMetaState *input metastate !
          337  +    getEdgeFlags *input edgeflags !
          338  +    getPressure f>s *input pressure !
          339  +    getSize f>s *input size !
          340  +    getPointerCount dup *input tcount !
          341  +    *input x0
          342  +    0 ?DO
          343  +	I getY f>s
          344  +	I getX f>s
          345  +	rot dup >r 2! r> 2 cells +
          346  +    LOOP  drop
          347  +    o ]gref o> ;
   331    348   
   332         -0 Value location
   333         -0 Value sensor
          349  +JValue location
          350  +JValue sensor
   334    351   
   335    352   Defer android-location ( location -- )
   336         -:noname ." new location" cr to location ; IS android-location
          353  +:noname to location ; IS android-location
   337    354   Defer android-sensor ( sensor -- )
   338         -:noname ." new sensor" cr to sensor ; IS android-sensor
          355  +:noname to sensor ; IS android-sensor
   339    356   
   340    357   :noname ( event type -- )
   341    358       CASE
   342         -	0 OF  android-key       ENDOF
   343         -	1 OF  android-touch     ENDOF
   344         -	2 OF  android-location  ENDOF
   345         -	3 OF  android-sensor    ENDOF
          359  +	0 OF  android-key         ENDOF
          360  +	1 OF  android-touch       ENDOF
          361  +	2 OF  android-location    ENDOF
          362  +	3 OF  android-sensor      ENDOF
          363  +	4 OF  android-characters  ENDOF
          364  +	5 OF  android-unicode     ENDOF
          365  +	6 OF  android-keycode     ENDOF
          366  +	7 Of  android-touch       ENDOF
   346    367       nip ENDCASE ; is akey
   347    368   
   348    369   previous previous set-current

Changes to gles2/jni-helper.fs.

    45     45   jni-method: getUnicodeChar(I) getUnicodeChar (I)I
    46     46   jni-method: getUnicodeChar getUnicodeChar ()I
    47     47   jni-method: getKeyCode getKeyCode ()I
    48     48   jni-method: getCharacters getCharacters ()Ljava/lang/String;
    49     49   jni-method: getAction getAction ()I
    50     50   jni-method: isLongPress isLongPress ()Z
    51     51   
           52  +jni-class: android/view/MotionEvent
           53  +jni-method: getPointerCount getPointerCount ()I
           54  +jni-method: getX getX (I)F
           55  +jni-method: getY getY (I)F
           56  +jni-method: me-getAction getAction ()I
           57  +jni-method: getFlags getFlags ()I
           58  +jni-method: getEdgeFlags getEdgeFlags ()I
           59  +jni-method: getEventTime getEventTime ()J
           60  +jni-method: getMetaState getMetaState ()I
           61  +jni-method: getSize getSize (I)F
           62  +jni-method: getPressure getPressure (I)F
           63  +
    52     64   jni-class: java/util/List
    53     65   
    54     66   jni-method: l-get get (I)Ljava/lang/Object;
    55     67   jni-method: l-size size ()I
    56     68   
           69  +: l[] ( n list -- object )  >o l-get o> ;
           70  +: l# ( list -- n )  >o l-size o> ;
           71  +
           72  +: l-map ( xt list -- )  >o { xt } ( -- )
           73  +    l-size 0 ?DO  I l-get >o xt execute ref>  LOOP o> ;
           74  +
    57     75   : getUnicodeKey { event -- key }
    58     76       event AKeyEvent_getAction event AKeyEvent_getKeyCode newKeyEvent >o
    59     77       event AKeyEvent_getMetaState getUnicodeChar(I) o> ;
    60     78   
    61     79   Variable kbflag
    62     80   
    63     81   : togglekb ( -- )

Changes to gles2/jni-location.fs.

    86     86   jni-field: argsensor argsensor Landroid/hardware/Sensor;
    87     87   jni-field: locationManager locationManager Landroid/location/LocationManager;
    88     88   jni-field: sensorManager sensorManager Landroid/hardware/SensorManager;
    89     89   
    90     90   also android
    91     91   
    92     92   : start-gps ( -- )
    93         -    clazz >o startgps gforth-handler >o post o> drop o> ;
           93  +    clazz >o startgps gforth-handler >o post ref> drop o> ;
    94     94   
    95     95   : stop-gps ( -- )
    96         -    clazz >o stopgps  gforth-handler >o post o> drop o> ;
           96  +    clazz >o stopgps  gforth-handler >o post ref> drop o> ;
    97     97   
    98     98   : start-sensor ( type delayus -- )
    99     99       clazz >o
   100    100       argj0 2>r
   101    101       0 to argj0
   102         -    sensorManager >o getDefaultSensor o> to argsensor
   103         -    startsensor gforth-handler >o post o> drop
          102  +    sensorManager >o getDefaultSensor dup ref> to argsensor ]ref
          103  +    startsensor gforth-handler >o post ref> drop
   104    104       2r> to argj0
   105    105       o> ;
   106    106   
   107         -: stop-sensor ( -- )
   108         -    clazz >o stopsensor gforth-handler >o post o> drop o> ;
          107  +: stop-sensor ( type -- )
          108  +    clazz >o
          109  +    sensorManager >o getDefaultSensor dup ref> to argsensor ]ref
          110  +    stopsensor gforth-handler >o post ref> drop o> ;
   109    111   
   110    112   : .deg ( degree -- )
   111    113       fdup f0< IF ." -" fnegate THEN
   112    114       fdup floor fdup f>s 0 .r '°' xemit f-  60e f*
   113    115       fdup floor fdup f>s 0 .r ''' xemit f-  60e f*
   114    116       f. ;
   115    117   
   116    118   : .location ( -- )  location >o
   117    119       o 0= IF  ." unknown" cr
   118    120       ELSE
          121  +	getProvider dup .jstring ]ref
   119    122   	." Lat: " getLatitude .deg cr
   120    123   	." Lon: " getLongitude .deg cr
   121    124   	." Alt: " getAltitude f. cr
   122    125   	." Spd: " getSpeed f. cr
   123    126   	." Dir: " getBearing f. cr
   124    127   	." Tme: " getTime d. cr
   125    128   	." Acc: " getAccuracy f. cr
   126    129       THEN
   127    130       o> ;
   128    131   
   129    132   : .sensor ( -- )  sensor >o
   130    133       o 0= IF  ." unknown"
   131    134       ELSE
          135  +	se-sensor >o getName .jstring space ref>
   132    136   	se-values [f@ bounds DO  I sf@ f. 1 sfloats +LOOP
   133    137       THEN cr o> ;
          138  +
          139  +: .sensors ( -- )  clazz >o sensorManager >o TYPE_ALL getSensorList >o
          140  +    [: cr getType . getName .jstring ;] o l-map ref> ref> o> ;
   134    141   
   135    142   previous previous

Changes to gles2/jni-tools.fs.

   140    140   : f@' ( jclass jfid -- r )  fieldenv JNIEnv-GetStaticFloatField() ;
   141    141   : d@' ( jclass jfid -- r )  fieldenv JNIEnv-GetStaticDoubleField() ;
   142    142   : l@' ( jclass jfid -- object )  fieldenv JNIEnv-GetStaticObjectField() ;
   143    143   
   144    144   Create 'sfield@ '[' 1+ 'A'
   145    145   [DO] "x@'" over [i] swap c! current @ search-wordlist 0= [IF] ' 2drop [THEN] , [LOOP]
   146    146   
          147  +\ global ref handling - you should ]gref every global ref after usage
          148  +
          149  +: ]ref ( object -- )  env tuck JNIEnv-DeleteLocalRef() ;
          150  +: ]gref ( object -- )  env tuck JNIEnv-DeleteGlobalRef() ;
          151  +: ref> ( object -- ) o ]ref r> o> >r ;
          152  +comp: drop ]] o ]ref o> [[ ;
          153  +
          154  +: gref! ( gref addr -- )  dup @ ?dup-IF  ]gref  THEN ! ;
          155  +: jvalue! ( gref xt -- )  >body gref! ;
          156  +comp: drop >body postpone ALiteral postpone gref! ;
          157  +
          158  +: JValue ( "name" -- ) 0 Value ['] jvalue! set-to ;
          159  +
   147    160   Variable cstring
   148    161   Variable cstring1
          162  +
          163  +\ round robin store for four active jstrings
          164  +
          165  +JValue jstring0
          166  +JValue jstring1
          167  +JValue jstring2
          168  +JValue jstring3
          169  +Variable jstring#
          170  +: to-jstring ( value -- )
          171  +    1 jstring# +!  jstring# 3 and case
          172  +	0 of  to jstring0  endof
          173  +	1 of  to jstring1  endof
          174  +	2 of  to jstring2  endof
          175  +	3 of  to jstring3  endof
          176  +    endcase ;
   149    177   
   150    178   : $0! ( addr u string -- addr' ) >r
   151    179       r@ $! 0 r@ $@ + c! r> $@ drop ;
   152    180   : cstr" ( -- addr )  parse-name cstring  $0! ;
   153    181   : cstr1" ( -- addr ) parse-name cstring1 $0! ;
   154         -: make-jstring ( c-addr -- jstring-addr ) env tuck JNIEnv-NewStringUTF() ;
          182  +: make-jstring ( c-addr -- jstring-addr )
          183  +    env tuck JNIEnv-NewStringUTF() dup to-jstring ;
   155    184   : js" ( -- addr )  '"' parse cstring $0! make-jstring ;
   156    185   comp: drop '"' parse cstring $0!
   157    186       cstring>sstring 1+ ]] SLiteral drop make-jstring [[ ;
   158    187   
   159    188   Variable iscopy
   160    189   2Variable to-release
   161         -: jstring>sstring ( string -- addr u )
          190  +: jfree ( -- )
          191  +    to-release 2@ 2dup d0= IF  2drop  EXIT  THEN  0. to-release 2!
          192  +    fieldenv JNIEnv-ReleaseStringUTFChars() ;
          193  +: jstring>sstring ( string -- addr u )  jfree
   162    194       dup >r iscopy fieldenv JNIEnv-GetStringUTFChars()
   163    195       r> over to-release 2! cstring>sstring ;
   164         -: jfree ( -- ) to-release 2@ fieldenv JNIEnv-ReleaseStringUTFChars() ;
          196  +: .jstring ( string -- ) jstring>sstring type jfree ;
   165    197   
   166    198   0 Value jniclass
   167    199   
   168    200   "Java identifier not found" exception Constant !!javanf!!
   169    201   
   170    202   : ?javanf ( id -- id )  dup 0= !!javanf!! and throw ;
   171    203   
................................................................................
   230    262       cstring@1 'sfield@ + @ compile, postpone ; ;
   231    263   
   232    264   \ array access: you can access one array at a time
   233    265   
   234    266   Variable jnibuffer
   235    267   
   236    268   : [len ( array -- n )  env tuck JNIEnv-GetArrayLength() ;
   237         -: ]ref ( array -- )  env tuck JNIEnv-DeleteLocalRef() ;
   238    269   
   239    270   : >buffer ( size -- buffer )  jnibuffer $!len jnibuffer $@ drop ;
   240    271   : buffer@ ( -- addr u )  jnibuffer $@ ;
   241    272   
   242    273   : [z@ ( array -- addr n )  >r env r@ 0 r@ [len dup >buffer
   243    274       env JNIEnv-GetBooleanArrayRegion() buffer@ r> ]ref ;
   244    275   : [b@ ( array -- addr n )  >r env r@ 0 r@ [len dup >buffer

Changes to jni-location.fs.

    86     86   jni-field: argsensor argsensor Landroid/hardware/Sensor;
    87     87   jni-field: locationManager locationManager Landroid/location/LocationManager;
    88     88   jni-field: sensorManager sensorManager Landroid/hardware/SensorManager;
    89     89   
    90     90   also android
    91     91   
    92     92   : start-gps ( -- )
    93         -    clazz >o startgps gforth-handler >o post o> drop o> ;
           93  +    clazz >o startgps gforth-handler >o post ref> drop o> ;
    94     94   
    95     95   : stop-gps ( -- )
    96         -    clazz >o stopgps  gforth-handler >o post o> drop o> ;
           96  +    clazz >o stopgps  gforth-handler >o post ref> drop o> ;
    97     97   
    98     98   : start-sensor ( type delayus -- )
    99     99       clazz >o
   100    100       argj0 2>r
   101    101       0 to argj0
   102         -    sensorManager >o getDefaultSensor o> to argsensor
   103         -    startsensor gforth-handler >o post o> drop
          102  +    sensorManager >o getDefaultSensor dup ref> to argsensor ]ref
          103  +    startsensor gforth-handler >o post ref> drop
   104    104       2r> to argj0
   105    105       o> ;
   106    106   
   107         -: stop-sensor ( -- )
   108         -    clazz >o stopsensor gforth-handler >o post o> drop o> ;
          107  +: stop-sensor ( type -- )
          108  +    clazz >o
          109  +    sensorManager >o getDefaultSensor dup ref> to argsensor ]ref
          110  +    stopsensor gforth-handler >o post ref> drop o> ;
   109    111   
   110    112   : .deg ( degree -- )
   111    113       fdup f0< IF ." -" fnegate THEN
   112    114       fdup floor fdup f>s 0 .r '°' xemit f-  60e f*
   113    115       fdup floor fdup f>s 0 .r ''' xemit f-  60e f*
   114    116       f. ;
   115    117   
   116    118   : .location ( -- )  location >o
   117    119       o 0= IF  ." unknown" cr
   118    120       ELSE
          121  +	getProvider dup .jstring ]ref
   119    122   	." Lat: " getLatitude .deg cr
   120    123   	." Lon: " getLongitude .deg cr
   121    124   	." Alt: " getAltitude f. cr
   122    125   	." Spd: " getSpeed f. cr
   123    126   	." Dir: " getBearing f. cr
   124    127   	." Tme: " getTime d. cr
   125    128   	." Acc: " getAccuracy f. cr
   126    129       THEN
   127    130       o> ;
   128    131   
   129    132   : .sensor ( -- )  sensor >o
   130    133       o 0= IF  ." unknown"
   131    134       ELSE
          135  +	se-sensor >o getName .jstring space ref>
   132    136   	se-values [f@ bounds DO  I sf@ f. 1 sfloats +LOOP
   133    137       THEN cr o> ;
          138  +
          139  +: .sensors ( -- )  clazz >o sensorManager >o TYPE_ALL getSensorList >o
          140  +    [: cr getType . getName .jstring ;] o l-map ref> ref> o> ;
   134    141   
   135    142   previous previous