Skip to content

Commit

Permalink
Doku, Bugfixes
Browse files Browse the repository at this point in the history
Fixed sensor raw reading to allow negative numbers.
Added Small Basic documentation for file operations
  • Loading branch information
c0pperdragon committed Mar 16, 2015
1 parent d6321ea commit 6bb5d54
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
23 changes: 12 additions & 11 deletions EV3BasicCompiler/Resources/Sensor.txt
Expand Up @@ -192,17 +192,18 @@ subcall SENSOR.READRAW // FFA
JR_LTEQ32 values32 0 arrayempty
ARRAY RESIZE resultarray values32

DATA8 rawvalue0
DATA8 rawvalue1
DATA8 rawvalue2
DATA8 rawvalue3
DATA8 rawvalue4
DATA8 rawvalue5
DATA8 rawvalue6
DATA8 rawvalue7
DATA32 rawvalue0
DATA32 rawvalue1
DATA32 rawvalue2
DATA32 rawvalue3
DATA32 rawvalue4
DATA32 rawvalue5
DATA32 rawvalue6
DATA32 rawvalue7
INPUT_READEXT layer no 0 -1 18 8 rawvalue0 rawvalue1 rawvalue2 rawvalue3 rawvalue4 rawvalue5 rawvalue6 rawvalue7

DATA32 i
DATA32 rawtmp
MOVE32_32 0 i
loop:
DATA8 tmp8
Expand All @@ -212,9 +213,9 @@ loop:
JR_GTEQ32 i 8 nomoredata

MOVE32_8 i tmp8
READ8 rawvalue0 tmp8 tmp8
JR_LT8 tmp8 0 nomoredata
MOVE8_F tmp8 tmpf
READ32 rawvalue0 tmp8 rawtmp
JR_LT32 rawtmp -1000000000 nomoredata
MOVE32_F rawtmp tmpf

nomoredata:
ARRAY_WRITE resultarray i tmpf
Expand Down
9 changes: 7 additions & 2 deletions SmallBasicEV3Extension/EV3.cs
Expand Up @@ -68,7 +68,7 @@ public static void SetLEDColor(Primitive color, Primitive effect)
}

/// <summary>
/// The current loading level of the battery (range 0 to 100)
/// The current loading level of the battery (range 0 to 100).
/// </summary>
public static Primitive BatteryLevel
{
Expand All @@ -91,6 +91,11 @@ public static Primitive BatteryLevel
}
}

/// <summary>
/// Execute one system command by the command shell of the EV3 linux system. All threads of the virtual machine are halted until the system command is finished.
/// </summary>
/// <param name="commandline">The system command.</param>
/// <returns>Exit status of the command.</returns>
public static Primitive SystemCall (Primitive commandline)
{
String cmd = (commandline == null ? "" : commandline.ToString());
Expand All @@ -113,7 +118,7 @@ public static Primitive SystemCall (Primitive commandline)

static bool[] hasDownloaded = new bool[1];

public static Primitive NativeCode(Primitive command)
internal static Primitive NativeCode(Primitive command)
{
String cmd = (command == null) ? "" : command.ToString().Trim();
if (cmd.Length==0)
Expand Down
56 changes: 54 additions & 2 deletions SmallBasicEV3Extension/EV3File.cs
Expand Up @@ -12,6 +12,7 @@ namespace SmallBasicEV3Extension

/// <summary>
/// Access the file system on the EV3 brick to read or write data.
/// File names can be given either absolute (with a leading '/') to reach any file in the system, or relative to the 'prjs' folder.
/// </summary>
[SmallBasicType]
public static class EV3File
Expand Down Expand Up @@ -63,17 +64,31 @@ private static Primitive OpenWriteImpl(Primitive filename, bool append)
}
}

/// <summary>
/// Open a file for writing. When the file already exists, it will be overwritten.
/// </summary>
/// <param name="filename">Name of the file to create/overwrite.</param>
/// <returns>A number that identifies this open file (a.k.a. file handle)</returns>
public static Primitive OpenWrite(Primitive filename)
{
return OpenWriteImpl(filename, false);
}

/// <summary>
/// Open a file for adding data. When the file does not exist, it will be created.
/// </summary>
/// <param name="filename">Name of the file to create/extend.</param>
/// <returns>A number that identifies this open file (a.k.a. file handle)</returns>
public static Primitive OpenAppend(Primitive filename)
{
return OpenWriteImpl(filename, true);
}


/// <summary>
/// Open a file for reading data. When the file does not exist the program will crash.
/// </summary>
/// <param name="filename">Name of the file to read from.</param>
/// <returns>A number that identifies this open file (a.k.a. file handle)</returns>
public static Primitive OpenRead(Primitive filename)
{
String f = (filename == null ? "" : filename.ToString());
Expand Down Expand Up @@ -104,6 +119,10 @@ public static Primitive OpenRead(Primitive filename)
}
}

/// <summary>
/// Close an open file.
/// </summary>
/// <param name="handle">The file handle (previously obtained from an Open... call)</param>
public static void Close(Primitive handle)
{
int hdl = 0;
Expand All @@ -117,6 +136,11 @@ public static void Close(Primitive handle)
}
}

/// <summary>
/// Write one line of text to the file. The line will be encoded with ISO-8859-1 encoding and will be terminated with a newline-character (code 10).
/// </summary>
/// <param name="handle">The file handle (previously obtained from an Open... call)</param>
/// <param name="text">The text to write to the file.</param>
public static void WriteLine(Primitive handle, Primitive text)
{
int hdl = 0;
Expand Down Expand Up @@ -148,6 +172,11 @@ public static void WriteLine(Primitive handle, Primitive text)
}
}

/// <summary>
/// Write a single byte of data on the file.
/// </summary>
/// <param name="handle">The file handle (previously obtained from an Open... call)</param>
/// <param name="data">One byte to write (value of 0 - 255).</param>
public static void WriteByte(Primitive handle, Primitive data)
{
int hdl = 0;
Expand Down Expand Up @@ -179,6 +208,11 @@ public static void WriteByte(Primitive handle, Primitive data)
}
}

/// <summary>
/// Read one line of text from the file. The line will be decoded using the ISO-8859-1 encoding and must be terminated with a newline-character (code 10).
/// </summary>
/// <param name="handle">The file handle (previously obtained from an Open... call)</param>
/// <returns>The text from the current line in the file.</returns>
public static Primitive ReadLine(Primitive handle)
{
int hdl = 0;
Expand All @@ -204,6 +238,11 @@ public static Primitive ReadLine(Primitive handle)
return new Primitive("");
}

/// <summary>
/// Read one byte of data from the file.
/// </summary>
/// <param name="handle">The file handle (previously obtained from an Open... call)</param>
/// <returns>The next byte from the file.</returns>
public static Primitive ReadByte(Primitive handle)
{
int hdl = 0;
Expand All @@ -224,14 +263,27 @@ public static Primitive ReadByte(Primitive handle)
return new Primitive(0);
}

/// <summary>
/// Utility function to convert a text to a number.
/// </summary>
/// <param name="text">A text holding a number in decimal representation (with optional fractional digits)</param>
/// <returns>The number</returns>
public static Primitive ConvertToNumber(Primitive text)
{
double d = 0;
double.TryParse(text == null ? "" : text.ToString(), out d);
return new Primitive(d);
}


/// <summary>
/// Utility function to read bytes from potentially huge data files that are too big to be transfered to memory as a whole.
/// Because the file could be so big that the numerical precision of the normal numbers is not enough, a row/column addressing is possible.
/// </summary>
/// <param name="filename">The name of the file.</param>
/// <param name="bytes_per_row">When the file has a row/column structure, this is the number of bytes in one row. Use 1 if not applicable.</param>
/// <param name="row">Which row to access (start with 0).</param>
/// <param name="column">Which column to access (start with 0).</param>
/// <returns>The byte on the denoted position</returns>
public static Primitive TableLookup(Primitive filename, Primitive bytes_per_row, Primitive row, Primitive column)
{
int bpr = 0;
Expand Down
6 changes: 1 addition & 5 deletions SmallBasicEV3Extension/Sensor.cs
Expand Up @@ -271,7 +271,7 @@ public static Primitive ReadRaw(Primitive port, Primitive values)
{
v = DecodeRaw(result, i * 4);
}
map[new Primitive((double)i)] = new Primitive(v<0 ? 0:v);
map[new Primitive((double)i)] = new Primitive(v<-1000000000 ? 0:v);
}
return Primitive.ConvertFromMap(map);
}
Expand Down Expand Up @@ -389,10 +389,6 @@ private static int DecodeRaw(byte[] result, int start)
return b0 | (b1<<8) | (b2<<16) | (b3<<24);
}

private static double DecodeSI(byte[] result, int start)
{
return BitConverter.ToSingle(result, start);
}

}
}

0 comments on commit 6bb5d54

Please sign in to comment.