Author Topic: Script BASIC for Android  (Read 2670 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Script BASIC for Android
« on: May 28, 2014, 11:00:31 PM »
One of the disadvantages of using a compiler to generate applications is they must reside inside the teminal app directory structure. /data/data/org.connectbot for example. (internal memory - limited)  Using an interpreter like Script BASIC you can store and run all your application programs from the sdcard.



Here is the same Word Count Challenge bench running on my Laptop under Ubuntu 64 bit.

jrs@laptop:~/sb/sb22/test$ time scriba wc.sb Bible.txt

real   0m8.871s
user   0m8.121s
sys   0m1.512s
jrs@laptop:~/sb/sb22/test$ head wc.lst
- (1)
-- (6)
a (8436)
a' (1)
aaron (319)
aaronites (2)
aaron's (31)
ab (2)
abaddon (1)
abagtha (1)
jrs@laptop:~/sb/sb22/test$

Update - I was curious what was taking all the time under Android. I stopped the program after it did a couple dozen scans of the 4.3 MB Bible text file replacing punctuation and control characters to spaces and finally splitting all the words into a Script BASIC array. This took 16 seconds on my tablet which means that 95% of the time was simulated disk I/O (Linux sort command line utility) to sort the words and writing /reading  the lists.  :(  Good to find out now that disk emulation reading and writing is a potential bottleneck.
« Last Edit: May 29, 2014, 07:45:27 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Script BASIC for Android
« Reply #1 on: May 29, 2014, 08:32:04 PM »
This is an example I did some time ago using the SL4A Android VM interface. SL4A allows access to the GUI features of Android.  This example also features the ANSI screen wrapper I wrote to give Script BASIC a screen mnemonic syntax.

SL4A API Reference

The SL4A interface reminds me of the good old days of GTK-Server.



Code: [Select]
' SL4A and console screen mnemonics demo

IMPORT DVM
INCLUDE "ansi.inc"

CLS

PRINT GRAPHICS_ON
PRINT "l",STRING(38,"q"),"k"
FOR i = 2 TO 15
  PRINT AT(1,i),"x",AT(40,i),"x"
NEXT i
PRINTNL
PRINT "m",STRING(38,"q"),"j"
PRINT AT(1,3),"t",STRING(38,"q"),"u"
PRINT AT(30,3),"w"
FOR i = 4 TO 15
  PRINT AT(30,i),"x"
NEXT i
PRINT AT(30,i),"v"
PRINT GRAPHICS_OFF

PRINT AT(2,2),BOLD,COLOR(7,4,STRING(10," ") & "SAMSUNG Tab 2 10.1" & STRING(10," "))
PRINT AT(2,4),BOLD,COLOR(7,6," Phone Type" & STRING(17," "))
PRINT AT(2,5),BOLD,COLOR(7,6," Screen Brightness" & STRING(10," "))
PRINT AT(2,6),BOLD,COLOR(7,6," Airplane Mode" & STRING(14," "))
PRINT AT(2,7),BOLD,COLOR(7,6," Ringer Slient Mode" & STRING(9," "))
PRINT AT(2,8),BOLD,COLOR(7,6," Screen On" & STRING(18," "))
PRINT AT(2,9),BOLD,COLOR(7,6," Max Media Volume" & STRING(11," "))
PRINT AT(2,10),BOLD,COLOR(7,6," Max Ringer Volume" & STRING(10," "))
PRINT AT(2,11),BOLD,COLOR(7,6," Media Volume" & STRING(15," "))
PRINT AT(2,12),BOLD,COLOR(7,6," Ringer Volume" & STRING(14," "))
PRINT AT(2,13),BOLD,COLOR(7,6," Vibrate Mode" & STRING(15," "))
PRINT AT(2,14),BOLD,COLOR(7,6," Vibrate Mode Ringer" & STRING(8," "))
PRINT AT(2,15),BOLD,COLOR(7,6," Vibrate Mode Notify" & STRING(8," "))

PRINT AT(33,4),DVM::getPhoneType()
PRINT AT(33,5),DVM::getScreenBrightness()
IF DVM::checkAirplaneMode() = "false" THEN
  PRINT AT(33,6),COLOR(1,0,"false")
ELSE
  PRINT AT(33,6),COLOR(2,0,"true")
END IF
IF DVM::checkRingerSilentMode() = "false" THEN
  PRINT AT(33,7),COLOR(1,0,"false")
ELSE
  PRINT AT(33,7),COLOR(2,0,"true")
END IF
IF DVM::checkScreenOn() = "false" THEN
  PRINT AT(33,8),COLOR(1,0,"false")
ELSE
  PRINT AT(33,8),COLOR(2,0,"true")
END IF
PRINT AT(33,9),DVM::getMaxMediaVolume()
PRINT AT(33,10),DVM::getMaxRingerVolume()
PRINT AT(33,11),DVM::getMediaVolume()
PRINT AT(33,12),DVM::getRingerVolume()
IF DVM::getVibrateMode() = "false" THEN
  PRINT AT(33,13),COLOR(1,0,"false")
ELSE
  PRINT AT(33,13),COLOR(2,0,"true")
END IF
IF DVM::getVibrateMode(TRUE) = "false" THEN
  PRINT AT(33,14),COLOR(1,0,"false")
ELSE
  PRINT AT(33,14),COLOR(2,0,"true")
END IF
IF DVM::getVibrateMode(FALSE) = "false" THEN
  PRINT AT(33,15),COLOR(1,0,"false")
ELSE
  PRINT AT(33,15),COLOR(2,0,"true")
END IF

PRINT AT(1,18)
LINE INPUT z

  • CLS - Clear screen and home the cursor. (x1:y1)
  • COLOR(fg,bg,text) - Set the foreground, background and text to be displayed in color.
  • AT(x,y) - Position the cursor to the (x:y) position on the screen.
  • NORMAL - Return attributes to the default state.
  • BOLD - Bold the text follow the attribute.
  • UNDERLINE_ON - Start underline text mode.
  • UNDERLINE_OFF - End  underline text mode.
  • REVERSE_ON - Start reverse video mode.
  • REVERSE_OFF - End reverse video mode.
  • INS - Insert a blank character at the current cursor position moving the text to the right.
  • DEL - Delete a character at the current cursor position and shift text to the left.
  • INS_LINE - Insert a blank line on the row the cursor is at and shift lines of text down.
  • DEL_LINE - Delete a line on the row the cursor is at and shift lines of text up.
  • GRAPHICS_ON - Switch to the graphics/line draw character set.
  • GRAPHICS_OFF - Return back to the standard charater set.
Note: All of the above screen attributes are in the form of a string. (except CLS)

Here is the DVM (SL4A) extension module where I left off with it. (just getting started)

Code: [Select]
' Android SL4A Interface
'
' ScriptBasic Open Source Project
'
' Contributors:
'   John Spikowski (JRS)
'
MODULE DVM

OPEN "localhost:9999" FOR SOCKET AS #1

id = 1

FUNCTION JSON2SB(a)
  LOCAL r,t,x
  LINE INPUT #1, r
  r = CHOMP(r)
  r = MID(r,2)
  r = LEFT(r,LEN(r) - 1)
  r = REPLACE(r, "\"", "")
  SPLITA r BY "," TO t
  FOR x = 0 to UBOUND(t)
    a{LEFT(t[x],INSTR(t[x],":")-1)} = MID(t[x],INSTR(t[x],":")+1)
  NEXT
  IF a{"error"} <> "null" THEN
    JSON2SB = undef
  ELSE
    JSON2SB = a{"result"}
  END IF
  id += 1
END FUNCTION


' *************
' PhoneFacade *
' *************

FUNCTION getPhoneType
  PRINT #1,"""{"id":""" & id & ""","method":"getPhoneType","params":[]}\n"""
  getPhoneType = JSON2SB(response)
END FUNCTION


' ****************
' SettingsFacade *
' ****************

FUNCTION getScreenBrightness
  PRINT #1,"""{"id":""" & id & ""","method":"getScreenBrightness","params":[]}\n"""
  getScreenBrightness = JSON2SB(response)
END FUNCTION

FUNCTION checkAirplaneMode
  PRINT #1,"""{"id":""" & id & ""","method":"checkAirplaneMode","params":[]}\n"""
  checkAirplaneMode = JSON2SB(response)
END FUNCTION

FUNCTION checkRingerSilentMode
  PRINT #1,"""{"id":""" & id & ""","method":"checkRingerSilentMode","params":[]}\n"""
  checkRingerSilentMode = JSON2SB(response)
END FUNCTION

FUNCTION checkScreenOn
  PRINT #1,"""{"id":""" & id & ""","method":"checkScreenOn","params":[]}\n"""
  checkScreenOn = JSON2SB(response)
END FUNCTION

FUNCTION getMaxMediaVolume
  PRINT #1,"""{"id":""" & id & ""","method":"getMaxMediaVolume","params":[]}\n"""
  getMaxMediaVolume = JSON2SB(response)
END FUNCTION

FUNCTION getMaxRingerVolume
  PRINT #1,"""{"id":""" & id & ""","method":"getMaxRingerVolume","params":[]}\n"""
  getMaxRingerVolume = JSON2SB(response)
END FUNCTION

FUNCTION getMediaVolume
  PRINT #1,"""{"id":""" & id & ""","method":"getMediaVolume","params":[]}\n"""
  getMediaVolume = JSON2SB(response)
END FUNCTION

FUNCTION getRingerVolume
  PRINT #1,"""{"id":""" & id & ""","method":"getRingerVolume","params":[]}\n"""
  getRingerVolume = JSON2SB(response)
END FUNCTION

FUNCTION getScreenTimeout
  PRINT #1,"""{"id":""" & id & ""","method":"getScreenTimeout","params":[]}\n"""
  getScreenTimeout = JSON2SB(response)
END FUNCTION

FUNCTION getVibrateMode(ringer)
  LOCAL source
  IF ringer = undef THEN
    source = 0
  ELSE IF ringer = TRUE THEN
    source = 1
  ELSE
    source = 0
  END IF
  PRINT #1,"""{"id":""" & id & ""","method":"getVibrateMode","params":[""" & source & """]}\n"""
  getVibrateMode = JSON2SB(response)
END FUNCTION

SUB notify(title,msg)
  PRINT #1,"""{"id":1,"method":"notify","params":[\"""" & title & """\",\"""" & msg & """\"]}\n"""
END SUB
 
END MODULE

The goal was to get a standard JSON extension module working and replace my home brew version.
« Last Edit: May 29, 2014, 10:16:09 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 2747
    • ScriptBasic Open Source Project
Re: Script BASIC for Android
« Reply #2 on: May 30, 2014, 09:39:57 PM »
SL4A has a nice interface to the Android WebView control. It allows callbacks to your script when events you assign in HTML are fired.

Using Webviews (SL4A)

Note: Most SL4A examples are shown using Python as the interpreter in the documentation for it. I'm using Script BASIC instead.



This is all that is needed to do the above.

Code: [Select]
IMPORT DVM

DVM::webViewShow("http://www.scriptbasic.org/forum")
LINE INPUT WAIT
« Last Edit: May 31, 2014, 01:07:48 PM by John »