Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes bugs in motor and sensor library concerning the daisy-chaining feature
- Loading branch information
1 parent
4e34d0e
commit 75a6199
Showing
11 changed files
with
247 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|