For the new eeprom modules (requires recent v72 and the new eeprom modules)
function getList() {
var count=0;
var map={};
for (var i=0;i<maxid;i++) {
var b=rom.read(ftst+i*4,4);
if (b[2]!=255) { //if b[2]==255, length ~= 64K, which is clearly not valid data.
count++;
var a=b[1]+(b[0]<<8);
var l=b[3]+(b[2]<<8);
map[i]=[a,l];
console.log(i+". 0x"+a.toString(16)+" "+l+" bytes.");
}
}
console.log("Scan complete "+ count+ " functions in index");
return map;
}
//getFunction(id) will return a string containing the code for that function, assuming it exists.
function getFunction(id) {
var x=rom.read(ftst+id*4,4);
if (x[2]!=255) {return E.toString(rom.read((x[1]+(x[0]<<8)),(x[3]+(x[2]<<8))));}
}
//isSafe(address, length, map) takes a 'map' object (as returned by getList()), and returns 1 if a function of specified length can be placed in the specified address without overwriting something.
//If it fails, print the ID of the function that it's got a problem with.
function isSafe(adr,len,map) {
var max=len+adr;
for (var i=0;i<maxid;i++) {
if (map[i]!=undefined) {
var a=map[i][0];
var l=map[i][1];
if (((a < adr)&&(a+l > adr))||((a > adr)&&(a < max))) {
console.log("Conflict on function "+i);
return 0;
}
}
}
return 1;
}
//addFunction(id, address, function) - This creates a new entry for a function of 'id', located at 'address' in the function index, and writes that and the function (supplied as a string) to the rom, provided that that can be done without overwriting another function.
function addFunction(id,adr,str) {
console.log("Adding function of length "+str.length+" at address "+adr+" with ID: "+id);
if (isSafe(adr,str.length,getList())) {
rom.write(adr,str);
rom.write(ftst+4*id,E.toString((adr>>8),(adr&0xFF),(str.length>>8),(str.length&0xFF)));
} else {
console.log("Selected location would overlap with other function!");
}
}
//deleteFunction(id) deletes the function with that ID from the function index.
function deleteFunction(id) {
console.log("deleting function: " + id);
rom.write(ftst+4*id,"ÿÿÿÿ");
}
ÿ
//cleanup - remove all the stuff related to this.
function cleanup() {
delete getFunction;
delete isSafe;
delete deleteFunction;
delete getList;
delete addFunction;
delete maxid;
delete getFunction;
delete ftst;
delete cleanup;
}
'maxid=255;ftst=1024;function addFunction(a,d,b){console.log("Adding function of length "+b.length+" at address "+d+" with ID: "+a);isSafe(d,b.length,getList())?(rom.write(d,b),rom.write(ftst+4*a,E.toString(d>>8,d&255,b.length>>8,b.length&255))):console.log("Selected location would overlap with other function!")}function deleteFunction(a){console.log("deleting function: "+a);rom.write(ftst+4*a,"ÿÿÿÿ")}function getList(){for(var a=0,d={},b=0;b<maxid;b++){var c=rom.read(ftst+4*b,4);if(255!=c[2]){a++;var e=c[1]+(c[0]<<8),c=c[3]+(c[2]<<8);d[b]=[e,c];console.log(b+". 0x"+e.toString(16)+" "+c+" bytes.")}}console.log("Scan complete "+a+" functions in index");return d}function getFunction(a){a=rom.read(ftst+4*a,4);if(255!=a[2])return E.toString(rom.read(a[1]+(a[0]<<8),a[3]+(a[2]<<8)))}function isSafe(a,d,b){d+=a;for(var c=0;c<maxid;c++)if(void 0!=b[c]){var e=b[c][0],f=b[c][1];if(e<a&&e+f>a||e>a&&e<d)return console.log("Conflict on function "+c),0}return 1}function cleanup(){delete getFunction;delete isSafe;delete deleteFunction;delete getList;delete addFunction;delete maxid;delete getFunction;delete ftst;delete cleanup};'
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
For the new eeprom modules (requires recent v72 and the new eeprom modules)