init
This commit is contained in:
57
src/device.h
Normal file
57
src/device.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
// lets assume we have some app with 4 states, 3 differnet input that change the state
|
||||
// states : [locked, unlocked, off, debug]
|
||||
// change actions : lock, str_input, power button
|
||||
//
|
||||
// pwr ┌──────────┐ pwr
|
||||
// ┌─────┴┬────►│off state │◄────┴┐
|
||||
// │ │ └────┬─────┘ │
|
||||
// │ │ │ │
|
||||
// │ │ ┌─────┘ │
|
||||
// │ │ ├ pwr │
|
||||
// │ │ ▼ │
|
||||
// │ ┌──┴─────┐ str=pwd ┌────┴───┐
|
||||
// │ │lock ├─────┴─────►│unlock │
|
||||
// │ │state │ │state │
|
||||
// │ └──────┬─┘◄───┬───────┴──┬─────┘
|
||||
// │ ^ │ lck │
|
||||
// │ lck ┤ ├ str=dbg ├ str=dbg
|
||||
// │ │ │ │
|
||||
// │ │ │ │
|
||||
// │ │ ▼ │
|
||||
// │ ┌─┴──────┐ │
|
||||
// └───┤debug │◄──────────────┘
|
||||
// │ │
|
||||
// └────────┘
|
||||
|
||||
typedef struct Device_s Device_t;
|
||||
|
||||
|
||||
typedef struct DeviceInterface_s{
|
||||
// all the actions that change the state
|
||||
// normally there wouldnt be any args
|
||||
// but as we dont have a protected keyword
|
||||
// we have to pass in the device to change
|
||||
// the state
|
||||
void (*pressPwr)(Device_t*);
|
||||
void (*pressStrInput)(Device_t*);
|
||||
void (*pressLock)(Device_t*);
|
||||
}DeviceInterface_t;
|
||||
|
||||
typedef struct DeviceState_s{
|
||||
const char* state_name;
|
||||
Device_t* device;
|
||||
DeviceInterface_t methods;
|
||||
}DeviceState_t;
|
||||
|
||||
struct Device_s{
|
||||
// in theroy this is also the flyweight pattern
|
||||
DeviceState_t state;
|
||||
char* entered_string;
|
||||
int entered_string_len;
|
||||
|
||||
DeviceInterface_t methods;
|
||||
};
|
||||
|
||||
|
||||
void initDevice(Device_t* device);
|
||||
Reference in New Issue
Block a user