State Pattern Code for embedded device – Dependency Injection of Device Objects in Context and State Classes

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:
State Diagram

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


}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

State Pattern Code for embedded device – Dependency Injection of Device Objects in Context and State Classes

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:
State Diagram

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


}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật