Skip to content

Commit

Permalink
Bugfixes,
Browse files Browse the repository at this point in the history
Fixes bugs in motor and sensor library concerning the daisy-chaining
feature
  • Loading branch information
c0pperdragon committed Feb 19, 2015
1 parent 4e34d0e commit 75a6199
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 19 deletions.
12 changes: 6 additions & 6 deletions EV3BasicCompiler/Resources/Motor.txt
Expand Up @@ -30,9 +30,9 @@ no_uppercase_port:
OR8 nos c nos
JR loop_end
no_lowercase_port:
JR_LT8 c 48 loop_end
JR_GT8 c 51 loop_end
SUB8 c 48 layer
JR_LT8 c 49 loop_end
JR_GT8 c 52 loop_end
SUB8 c 49 layer
loop_end:
ADD8 1 i i
JR loop
Expand Down Expand Up @@ -69,9 +69,9 @@ no_uppercase_port:
MOVE8_8 c no
JR loop_end
no_lowercase_port:
JR_LT8 c 48 loop_end
JR_GT8 c 51 loop_end
SUB8 c 48 layer
JR_LT8 c 49 loop_end
JR_GT8 c 52 loop_end
SUB8 c 49 layer
loop_end:
ADD8 1 i i
JR loop
Expand Down
29 changes: 20 additions & 9 deletions EV3BasicCompiler/Resources/Sensor.txt
Expand Up @@ -10,7 +10,8 @@ subcall SENSOR.GETNAME // FS

MOVEF_8 port no
SUB8 no 1 no
MOVE8_8 0 layer
DIV8 no 4 layer
MATH MOD8 no 4 no

INPUT_DEVICE GET_NAME layer no 32 result
STRINGS STRIP result result
Expand All @@ -28,7 +29,8 @@ subcall SENSOR.GETTYPE // FF

MOVEF_8 port no
SUB8 no 1 no
MOVE8_8 0 layer
DIV8 no 4 layer
MATH MOD8 no 4 no

INPUT_DEVICE GET_TYPEMODE layer no type mode
MOVE8_F type result
Expand All @@ -46,7 +48,8 @@ subcall SENSOR.GETMODE // FF

MOVEF_8 port no
SUB8 no 1 no
MOVE8_8 0 layer
DIV8 no 4 layer
MATH MOD8 no 4 no

INPUT_DEVICE GET_TYPEMODE layer no type mode
MOVE8_F mode result
Expand All @@ -68,7 +71,8 @@ subcall SENSOR.GETDATAFORMAT // FS

MOVEF_8 port no
SUB8 no 1 no
MOVE8_8 0 layer
DIV8 no 4 layer
MATH MOD8 no 4 no

INPUT_DEVICE GET_FORMAT layer no datasets format modes view

Expand Down Expand Up @@ -97,7 +101,9 @@ subcall SENSOR.SETMODE // FFV

MOVEF_8 port no
SUB8 no 1 no
MOVE8_8 0 layer
DIV8 no 4 layer
MATH MOD8 no 4 no

MOVEF_8 mode mode8

INPUT_DEVICE READY_RAW layer no 0 mode8 0
Expand All @@ -114,7 +120,8 @@ subcall SENSOR.ISBUSY // FS

MOVEF_8 port no
SUB8 no 1 no
MOVE8_8 0 layer
DIV8 no 4 layer
MATH MOD8 no 4 no

INPUT_TEST layer no busy

Expand All @@ -134,7 +141,8 @@ subcall SENSOR.WAIT // FV

MOVEF_8 port no
SUB8 no 1 no
MOVE8_8 0 layer
DIV8 no 4 layer
MATH MOD8 no 4 no

DATA8 busy
dotest:
Expand All @@ -158,7 +166,8 @@ subcall SENSOR.READPERCENT // FF

MOVEF_8 port no
SUB8 no 1 no
MOVE8_8 0 layer
DIV8 no 4 layer
MATH MOD8 no 4 no

INPUT_READ layer no 0 -1 percentage
JR_LT8 percentage 0 novalue
Expand All @@ -181,7 +190,9 @@ subcall SENSOR.READRAW // FFA

MOVEF_8 port no
SUB8 no 1 no
MOVE8_8 0 layer
DIV8 no 4 layer
MATH MOD8 no 4 no

MOVEF_32 values values32

JR_LTEQ32 values32 0 arrayempty
Expand Down
33 changes: 33 additions & 0 deletions Examples/DaisyChain.sb
@@ -0,0 +1,33 @@
Motor.Power("1A",50)
Motor.Power("1B",50)
Motor.Power("1C",50)
Motor.Power("1D",50)

While "True"
LCD.StopUpdate()
LCD.Clear()
LCD.Text(1,0,2,1, "Plug touch sensors to")
LCD.Text(1,0,14,1, "the daisy chain bricks")
LCD.Text(1,0,26,1, "to control the motors")

For l=1 To 4
For s=1 To 4
m = l + Text.GetCharacter(64+s)
P = Sensor.ReadPercent(s+(l-1)*4)
x = s*40 - 30
y = l*20 + 25
If P>50 Then
LCD.FillRect(1,x,y, 30,18)
LCD.Text(1,x+2,y+5, 1, P)
Motor.Start(m)
else
LCD.Rect(1,x,y,30,18)
LCD.Text(1,x+10,y+5, 1, P)
Motor.Stop(m,"true")
Endif
endfor
endfor
LCD.Update()

Program.Delay(100)
EndWhile
14 changes: 12 additions & 2 deletions Examples/MotorSynchronize.sb
@@ -1,2 +1,12 @@
Motor.ScheduleSyncForCount("AD",100,200,1000,"false")
Program.Delay(5000)
Motor.ScheduleSyncForCount("AD",100,200,500,"false")
Program.Delay(1000)
Motor.ScheduleSyncForTime("AD",100,-200,1000,"true")
Program.Delay(1000)
Motor.SchedulePowerForCount("A", 100, 0,500,0, "true")
Program.Delay(1000)
Motor.SchedulePowerForTime("AD", -100, 0,500,0, "true")
Program.Delay(1000)
Motor.ScheduleSpeedForCount("D", -100, 0,500,0, "false")
Program.Delay(1000)
Motor.ScheduleSpeedForTime("A", 100, 0,500,0, "true")
Program.Delay(1000)
4 changes: 2 additions & 2 deletions SmallBasicEV3Extension/EV3Communicator.cs
Expand Up @@ -97,7 +97,7 @@ internal static byte[] DirectCommand(ByteCodeBuffer bytecodes, int globalbytes,

// set up local ping thread to periodically send a command to the watchdog
// program to keep it alive (and check if the brick is still operating)
(new Thread(runpings)).Start();
(new System.Threading.Thread(runpings)).Start();
}

// finally execute the command
Expand Down Expand Up @@ -176,7 +176,7 @@ private static void runpings()
System.Environment.Exit(1);
}
}
Thread.Sleep(500);
System.Threading.Thread.Sleep(500);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions SmallBasicEV3Extension/LCD.cs
Expand Up @@ -19,6 +19,7 @@ public static class LCD
private static ByteCodeBuffer commandBuffer = new ByteCodeBuffer();
private static bool autoflush = true;

// must be called while holding a lock on commandBuffer
private static void CheckFlush()
{
if ( (commandBuffer.Length>0 && autoflush)
Expand Down
1 change: 1 addition & 0 deletions SmallBasicEV3Extension/SmallBasicEV3Extension.csproj
Expand Up @@ -51,6 +51,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sensor.cs" />
<Compile Include="Speaker.cs" />
<Compile Include="Thread.cs" />
<Compile Include="Vector.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
91 changes: 91 additions & 0 deletions SmallBasicEV3Extension/Thread.cs
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using Microsoft.SmallBasic.Library;

namespace SmallBasicEV3Extension
{

[SmallBasicType]
public class Thread
{
// The list of all threads that were triggered from the basic program.
// A thread is identified by its event handler target and can be
// triggered multiple times, in which case the handler will be called this many times,
// but only in sequence.
private static Dictionary<SmallBasicCallback, Thread> triggeredThreads = new Dictionary<SmallBasicCallback, Thread>();

// This does not install an event handler, but triggers a new thread instead.
// That was the only way to get a nice API for threading in Small Basic.
public static event SmallBasicCallback Run
{
// do not install an event handler, but start a thread instead
add
{
lock (triggeredThreads)
{
if (!triggeredThreads.ContainsKey(value))
{
triggeredThreads[value] = new Thread(value);
}
triggeredThreads[value].Trigger();
}
}
remove
{
// no action, because threads can not be removed
}
}

// instance members
private SmallBasicCallback callback;
private int triggerCount;

private Thread(SmallBasicCallback callback)
{
this.callback = callback;
this.triggerCount = 0;
(new System.Threading.Thread(run)).Start();
}

private void Trigger()
{
lock (this)
{
triggerCount++;
Monitor.PulseAll(this);
}
}

private void run()
{
for (; ; ) // this c#-thread runs forever for the case that basic wants to trigger the thread once more
{
// wait for a trigger
bool mustrun = false;
lock (this)
{
if (triggerCount>0)
{
triggerCount--;
mustrun = true;
}
else
{
Monitor.Wait(this);
}
}
// when a trigger is detected, run the basic thread once
if (mustrun)
{
callback.Invoke();
}
}
}

}

}
27 changes: 27 additions & 0 deletions testsuite/languagefeatures/ConcurrentThreads.sb
@@ -0,0 +1,27 @@
a=1
Thread.Run = changer

While "true"
x = a
x = x+a
If x<>2 And x<>4 then
TextWindow.WriteLine("Found miss-match")
endif
endwhile


Sub changer
While "True"
A = 1
A = 1
A = 1
A = 1
A = 1
A = 2
A = 2
A = 2
A = 2
A = 2
endwhile
endsub

27 changes: 27 additions & 0 deletions testsuite/languagefeatures/LargeMemory.sb
@@ -0,0 +1,27 @@
' Test program to chech how much memory is available for variables

SIZE = 1000000

LCD.Clear()
A = Vector.Init(SIZE, 4711)
LCD.Text(1, 10,0, 1, "A["+(SIZE-1)+"] = " + A[SIZE-1])
B = Vector.Init(SIZE, 815)
LCD.Text(1, 10,12, 1, "B["+(SIZE-1)+"] = " + B[SIZE-1])
C = Vector.Init(SIZE, 1111)
LCD.Text(1, 10,24, 1, "C["+(SIZE-1)+"] = " + C[SIZE-1])
D = Vector.Init(SIZE, 2222)
LCD.Text(1, 10,36, 1, "D["+(SIZE-1)+"] = " + D[SIZE-1])
E = Vector.Init(SIZE, 3333)
LCD.Text(1, 10,48, 1, "E["+(SIZE-1)+"] = " + E[SIZE-1])
F = Vector.Init(SIZE, 4444)
LCD.Text(1, 10,60, 1, "F["+(SIZE-1)+"] = " + F[SIZE-1])
G = Vector.Init(SIZE, 5555)
LCD.Text(1, 10,72, 1, "G["+(SIZE-1)+"] = " + G[SIZE-1])
H = Vector.Init(SIZE, 666)
LCD.Text(1, 10,84, 1, "H["+(SIZE-1)+"] = " + H[SIZE-1])
I = Vector.Init(SIZE, 1234)
LCD.Text(1, 10,96, 1, "I["+(SIZE-1)+"] = " + I[SIZE-1])
J = Vector.Init(SIZE, 5678)
LCD.Text(1, 10,108, 1, "J["+(SIZE-1)+"] = " + J[SIZE-1])

Buttons.Wait()
27 changes: 27 additions & 0 deletions testsuite/languagefeatures/MultiThreading.sb
@@ -0,0 +1,27 @@
a=1
Thread.Run = changer

While "true"
x = a
x = x+a
If x<>2 And x<>4 then
TextWindow.WriteLine("Found miss-match")
endif
endwhile


Sub changer
While "True"
A = 1
A = 1
A = 1
A = 1
A = 1
A = 2
A = 2
A = 2
A = 2
A = 2
endwhile
endsub

0 comments on commit 75a6199

Please sign in to comment.