Skip to content

lumostor/arduino-esp32-LinenoiseBitlash

Repository files navigation

LinenoiseBitlash

a console for esp32 that has the scripting of bitlash and linenoise as line editing + history.

Bitlash is a tiny interpreter (C like) to which it's possible, and easy, to add functions to control your project, or retreive info from it. Have a look at http:https://bitlash.net/bitlash-users-guide.pdf for a better description.

By itself, bitlash, doesn't have line editing neither history, beside ^U to recall the last line. LinenoiseBitlash by pluging bitlash to linenoise (a tiny readline replacement), provide line editing/history. The resulting line from linenoise, after pressing "enter", is executed with bitlash's doCommand(), which give, in the end, a tiny bitlash interpreter with readline equivalent line edition/history.

This library offer bitlash plugged with linenoise and a kind of encapsulation. But be advized that bitlash is C code that use many global variables, part of it's code is not reentrant. And this may be the case for linenoise too.

Due to linenoise a new function have been added to bitlash, termset or ts, to reset the dumb terminal mode, used by linenoise. In the example below there is a test ( using termProbe() ) to probe if the terminal support escape sequences, and set the terminal to dumb mode if not. In dumb mode there is no line editing/history possible but no escape sequence displayed, which, when there is, is quite annoying. So termset permit to retest the terminal and change the dumb mode accordingly, a message is displayed if dumb mode is set, no message otherwise; termset is useful if you change your terminal and don't want to reset your device.

The flash partition used by EEPROMClass will be used to store bitlash script function (created using function). It you need to use EEPROMClass for other use, it is possible, by changing some settings in the bitlash code, it is not provided directly by this library, have a look at page 32 of http:https://bitlash.net/bitlash-users-guide.pdf, the bitlash.h in question is the one in src directory. Also when calling "ls" or "help" if you get a lot of lines that looks like function {};\n alone on the line, it is because your EEPROM values are set to zero, which seems to be the default. You'll have to format it with rm *, this will initialize the flash partition used by EEPROM to 0xff. From there ls and help will behave normally.

When using bitlash functions like pinmode(pin,mode) pin is numeric, don't use d13 in there it won't work, use 13. And for the mode, be aware that INPUT mode is not numeric 0, like in bitlash manual and arduino uno (and probably like a lot of arduino compatible boards), INPUT is numeric 1 and OUTPUT is numeric 2. To ease that aspect mode names have been added as built-in in the esp32 bitlash port, they are not case sensitive, and are as follow:

	BUILT_IN("low",		"return 0")
	BUILT_IN("high",	"return 1")
	BUILT_IN("input",	"return 1")
	BUILT_IN("output",	"return 2")
	BUILT_IN("pullup",      "return 4")
	BUILT_IN("input_pullup","return 5")
	BUILT_IN("pulldown",    "return 8")
	BUILT_IN("input_pulldown","return 9")
	BUILT_IN("open_drain",  "return 16")
	BUILT_IN("output_open", "return 18")
	BUILT_IN("analog",      "return 12")

INSTALL

To use this lib you will have to install:

These libraries are to be installed like normal arduino library. In my case I use platformio so I just create a link in the lib directory of my projects.

The LinenoiseBitlash class

Other methods provided by Console class

class LinenoiseBitlash: public Console {

 public:

  // baud: baud rate
  // p:  prompt string may contain ANSI escape sequence
  // hlen: the history length
  void begin(int baud, String p, int hlen);

  // this console task will pass the line to Bitlash doCommand() for parsing.
  virtual void consoleTask();
  
  // do the same thing as addBitlashFunction()
  void addFunction(const char * func_name, bitlash_function func) {

};

Example of use

#include <LinenoiseBitlash.h>

LinenoiseBitlash CON;

// could be used as:
// > pin(13,0)  # set pin 13 to 0
// > print pin(13)
// 0
numvar pin_func(void) {

  // two args this is a set
  if( getarg(0) == 2 )    ///  getarg(0) is the number of args
    digitalWrite(getarg(1),getarg(2)); // getarg(1): 1st arg, getarg(2) 2nd arg

  // always return the value read
  return digitalRead(getarg(1));
}

setup() {

	CON.begin(115200, "HAL> ", 10);

	if (CON.termProbe()) { /* zero indicates success */
          printf("\n"
	    "Your terminal application does not support escape sequences.\n"
	    "Line editing and history features are disabled.\n"
	    "On linux , try screen.\n"
	    "On Windows, try using Putty instead.\n");
          CON.termDumb(true);
  	}

	// add a new function "pin" to bitlash
	CON.addFunction("pin",(bitlash_function)pin_func); 
	
	CON.consoleTaskStart( );  // will start a task waiting for input and execute
			          // it with Bitlash doCommand()
}

loop() {

       // put your loop code here

}

Interaction example

bitlash here! v2.0 (c) 2013 Bill Roy -type HELP- 118096 bytes free
HAL> ls
function blink13 {d13=!d13; snooze(100);};
function tscl {d0=!d0;};
function tsda {d4=!d4;};
HAL> help

bitlash here! v2.0 (c) 2013 Bill Roy -type HELP- 103532 bytes free
http:https://bitlash.net
See LICENSE for license
Pins: d0-39,a0-39  Variables: a-z, 32 bit long integers
Operators: + - * / ( ) < <= > >= == != << >> ! ^ & | ++ -- :=
Commands: arg boot else function help if ls peep print ps return rm run stop switch while
Functions:abs ar aw baud bc beep br bs bw constrain delay dr dw er ew free getkey getnum inb isstr max millis min outb pinmode printf pulsein random sign snooze
termset ts pin

function blink13 {d13=!d13; snooze(100);};
function tscl {d0=!d0;};
function tsda {d4=!d4;};
HAL> print free
103504
HAL>
HAL> i=0; while i<10 { print i; blink13; i++; }
0
1
2
3
4
5
6
7
8
9
HAL> pinmode(13,OUTPUT)
HAL> print pin(13,0)
0
HAL> print pin(13)
0
HAL> print pin(13,HIGH)
1
HAL> dw(13,LOW)
HAL> print dr(13)
0
HAL>