I’m wrestling with some code and I think I’ve finally gotten some state pattern code working with my events.
I’ve got a simplified system that will work like this:
This is my first go with State Pattern code and the examples I found were too simple or just not related to my type of device.
Q:
Is the context class supposed to do all of the controlling of peripherals of the device or should the active state directly control them? For instance, Drive the display, read the sensors, etc. To me, either way would work, but don’t have enough experience here to choose what’s correct.
I mention DI because I’m having to pass all of my peripherals into the context then into each state to provide control. OR do I just pass them into context and make the calls to the context from the active state(mentioned above)? The way I see it, the active state and the context are constantly communicating with each other via DI to control the device.
I hope this is clear.
Context Class Code:
//context
//the context will capture the interrupt events and delegate the response
class ModeContext
{
private int rotationCount;
private string direction;
private State state;
public RotaryEncoder rotaryEncoderInstance;
public Tlc5940 Display;
//public event NativeEventHandler rotationEventHandler;// = null;
public ModeContext(RotaryEncoder re, Tlc5940 display)
{
this.State = new Mode1(this);
rotaryEncoderInstance = re;
rotaryEncoderInstance.RotationEventHandler += OnRotationEvent;//attach to Context's Handler
rotaryEncoderInstance.MomentaryButtonEventHandler += OnButtonEvent;
Display = display;
rotationCount = 0;
}
public void RunStateTask()
{
state.MainTask(this);
}
public State State
{
get { return state; }
set { state = value; }//debug state change
}
//Event Handler
public void OnButtonEvent(uint clickCount, uint zero, DateTime time)
{
switch (clickCount)
{
case 0:
state.OnButtonHold(this);
break;
case 1:
state.OnButtonSingleClick(this);
break;
//default:
// throw new Exception("Something went wrong!");
// break;
}
}
public void OnRotationEvent(uint data1, uint data2, DateTime time)
{
rotationCount++;
if (data1 == 1)
{
direction = "Clockwise";
state.OnCWRotationEvent(this);
}
else
{
direction = "Counter-Clockwise";
state.OnCCWRotationEvent(this);
}
//Debug.Print(rotationCount.ToString() + ": " + direction + " Context Mode Rotation Event Fired!");
}
}
Example State Class:
class Mode1 : State//, IDisposable
{
public Mode1(ModeContext mode): base(mode)
{
Display = mode.Display;
}
public override void MainTask(ModeContext mode)
{
Debug.Print("Mode1 Main Task Running...");
//read Sensor
//display the value to Display
//let the context determine how to display it??
//or call a method in the context that encapsulated all of it's functions?
//Thread.Sleep(500);
}
#region event handlers
public override void OnCCWRotationEvent(ModeContext mode)
{
//change state
mode.State = new Mode2(mode);
Debug.Print("Changing State");
}
public override void OnCWRotationEvent(ModeContext mode)
{
//change state
Debug.Print("Changing State");
mode.State = new Mode0(mode);
}
public override void OnButtonSingleClick(ModeContext mode)
{
Debug.Print("Mode1: Single Click!");
}
public override void OnButtonHold(ModeContext mode)
{
Debug.Print("Mode1: Press and Hold Detected!");
}
#endregion
}