// Espruino programmer example. // // Note: this application only works on devices that support // OTG and allow access to serial devices. // // Known to work: Nexus7, GalaxyS3, ExperiaZUltra // Don't work: Nexus4, GalaxyS1, GalaxyS4 //Global variables. var usb = null; //Called when application is started. function OnStart() { //Create a layout with objects vertically centered. lay = app.CreateLayout( "linear", "VCenter,FillXY" ); //Create title text. txt = app.CreateText("Espruino"); txt.SetTextSize( 22 ); txt.SetMargins( 0,0,0,0.01 ); lay.AddChild( txt ); //Create an edit box containing an example espruino program. var s = "var state=0;\n\nfunction flash()\n{\n"; s += " digitalWrite( LED1, state );\n"; s += " state = !state;\n"; s += "}\n"; s += "setInterval( flash, 200 );\n"; edt = app.CreateTextEdit( s, 0.96, 0.8, "NoSpell" ); lay.AddChild( edt ); //Create a horizontal layout for buttons. layBut = app.CreateLayout("Linear", "Horizontal"); lay.AddChild( layBut ); //Create an connect button. btnConnect = app.CreateButton( "Connect", 0.23, 0.1 ); btnConnect.SetOnTouch( btnConnect_OnTouch ); layBut.AddChild( btnConnect ); //Create an send button. btnSend = app.CreateButton( "Send", 0.23, 0.1 ); btnSend.SetOnTouch( btnSend_OnTouch ); layBut.AddChild( btnSend ); //Create a reset button. btnReset = app.CreateButton( "Reset", 0.23, 0.1 ); btnReset.SetOnTouch( btnReset_OnTouch ); layBut.AddChild( btnReset ); //Create an save button. btnSave = app.CreateButton( "Save", 0.23, 0.1 ); btnSave.SetOnTouch( btnSave_OnTouch ); layBut.AddChild( btnSave ); //Add layout to app. app.AddLayout( lay ); } //Called when user touches connect button. function btnConnect_OnTouch() { //Create USB serial object. usb = app.CreateUSBSerial(); } //Called when user touches send button. function btnSend_OnTouch() { //Get rid of blank lines, spaces etc that cause //a problem for Espruino. var s = edt.GetText(); s = s.replace( RegExp("\n\n+","gim"), "\n" ); s = s.replace( RegExp("\n +","gim"), "\n" ); s = s.replace( RegExp("\\)\\s*\\{","gim"), "\)\{" ); s = s.replace( RegExp(", +","gim"), "," ); s = s.replace( RegExp("\\( +","gim"), "\(" ); s = s.replace( RegExp(" +\\)","gim"), "\)" ); //alert( s ); //Send program to Espruino. Send( "echo(0);" + s ); } //Called when user touches reset button. function btnReset_OnTouch() { //Send reset and turn led's off. var s = "echo(0);\n" s += "reset();\n"; s += "digitalWrite(LED1,0);\n"; s += "digitalWrite(LED2,0);\n"; Send( s ); } //Called when user touches save button. function btnSave_OnTouch() { var s = "echo(0);\n" s += "save();\n"; Send( s ); } //Check connection and send data. function Send( s ) { if( usb ) usb.Write( s ); else app.ShowPopup( "Please connect" ); }