init
This commit is contained in:
28
src/device.c
Normal file
28
src/device.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "device.h"
|
||||
#include <string.h>
|
||||
|
||||
static void pressPwrMethod(Device_t *device) {
|
||||
device->state.methods.pressPwr(device);
|
||||
}
|
||||
|
||||
static void pressStrInputMethod(Device_t *device) {
|
||||
device->state.methods.pressStrInput(device);
|
||||
}
|
||||
|
||||
static void pressLockMethod(Device_t *device) {
|
||||
device->state.methods.pressLock(device);
|
||||
}
|
||||
|
||||
void initDevice(Device_t* device) {
|
||||
*device = (Device_t){
|
||||
.state = NULL,
|
||||
.entered_string = "frogs",
|
||||
.entered_string_len = 6,
|
||||
.methods = (DeviceInterface_t){
|
||||
.pressPwr = &pressPwrMethod,
|
||||
.pressStrInput = &pressStrInputMethod,
|
||||
.pressLock = &pressLockMethod,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
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);
|
||||
22
src/main.c
Normal file
22
src/main.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include "states/state.h"
|
||||
#include "device.h"
|
||||
|
||||
int main() {
|
||||
Device_t device;
|
||||
initDevice(&device);
|
||||
// sets the device to off on start up
|
||||
setOffState(&device);
|
||||
|
||||
// turn on the device
|
||||
device.methods.pressPwr(&device);
|
||||
|
||||
// send the current string
|
||||
device.methods.pressStrInput(&device);
|
||||
|
||||
// send in the right string
|
||||
device.entered_string = "pwd";
|
||||
device.methods.pressStrInput(&device);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
33
src/states/debug_state.c
Normal file
33
src/states/debug_state.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "state.h"
|
||||
#include "../device.h"
|
||||
|
||||
const char debug_state_name[] = __FILE_NAME__;
|
||||
|
||||
static void pressPwrMethod(Device_t *device) {
|
||||
printf("turning off device\n");
|
||||
setOffState(device);
|
||||
}
|
||||
|
||||
static void pressStrInputMethod(Device_t *device) {
|
||||
printf("nothing happens\n");
|
||||
}
|
||||
|
||||
static void pressLockMethod(Device_t *device) {
|
||||
printf("locking device\n");
|
||||
setOffState(device);
|
||||
}
|
||||
|
||||
void setDebugState(Device_t *device) {
|
||||
device->state = (DeviceState_t){
|
||||
.state_name = debug_state_name,
|
||||
.device = device,
|
||||
.methods = (DeviceInterface_t){
|
||||
.pressPwr = &pressPwrMethod,
|
||||
.pressStrInput = &pressStrInputMethod,
|
||||
.pressLock = &pressLockMethod,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
45
src/states/lock_state.c
Normal file
45
src/states/lock_state.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "state.h"
|
||||
#include "../device.h"
|
||||
|
||||
const char lock_state_name[] = __FILE_NAME__;
|
||||
|
||||
static void pressPwrMethod(Device_t *device) {
|
||||
printf("turning off device\n");
|
||||
setOffState(device);
|
||||
}
|
||||
|
||||
static void pressStrInputMethod(Device_t *device) {
|
||||
if (strcmp(device->entered_string, "dbg") == 0) {
|
||||
printf("entering debug state\n");
|
||||
setDebugState(device);
|
||||
return;
|
||||
}
|
||||
if (strcmp(device->entered_string, "pwd") == 0) {
|
||||
printf("entering unlock state\n");
|
||||
setUnlockState(device);
|
||||
return;
|
||||
}
|
||||
|
||||
printf("unknown string %s\n", device->entered_string);
|
||||
|
||||
}
|
||||
|
||||
static void pressLockMethod(Device_t *device) {
|
||||
printf("nothing happens\n");
|
||||
}
|
||||
|
||||
void setLockState(Device_t *device) {
|
||||
device->state = (DeviceState_t){
|
||||
.state_name = lock_state_name,
|
||||
.device = device,
|
||||
.methods = (DeviceInterface_t){
|
||||
.pressPwr = &pressPwrMethod,
|
||||
.pressStrInput = &pressStrInputMethod,
|
||||
.pressLock = &pressLockMethod,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
31
src/states/off_state.c
Normal file
31
src/states/off_state.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "state.h"
|
||||
#include "../device.h"
|
||||
#include <stdio.h>
|
||||
|
||||
const char off_state_name[] = __FILE_NAME__;
|
||||
|
||||
static void pressPwrMethod(Device_t *device) {
|
||||
printf("turning on device\n");
|
||||
setLockState(device);
|
||||
}
|
||||
|
||||
static void pressStrInputMethod(Device_t *device) {
|
||||
printf("nothing happens\n");
|
||||
}
|
||||
|
||||
static void pressLockMethod(Device_t *device) {
|
||||
printf("nothing happens\n");
|
||||
}
|
||||
|
||||
void setOffState(Device_t *device) {
|
||||
device->state = (DeviceState_t){
|
||||
.state_name = off_state_name,
|
||||
.device = device,
|
||||
.methods = (DeviceInterface_t){
|
||||
.pressPwr = &pressPwrMethod,
|
||||
.pressStrInput = &pressStrInputMethod,
|
||||
.pressLock = &pressLockMethod,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
7
src/states/state.h
Normal file
7
src/states/state.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
#include "../device.h"
|
||||
|
||||
void setLockState(Device_t *device);
|
||||
void setUnlockState(Device_t *device);
|
||||
void setDebugState(Device_t *device);
|
||||
void setOffState(Device_t *device);
|
||||
37
src/states/unlock_state.c
Normal file
37
src/states/unlock_state.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include "state.h"
|
||||
#include "../device.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
const char name[] = __FILE_NAME__;
|
||||
|
||||
static void pressPwrMethod(Device_t *device) {
|
||||
printf("turning off device\n");
|
||||
setOffState(device);
|
||||
}
|
||||
|
||||
static void pressStrInputMethod(Device_t *device) {
|
||||
if (strcmp(device->entered_string, "dbg") == 0) {
|
||||
printf("entering debug state\n");
|
||||
setDebugState(device);
|
||||
}
|
||||
printf("unknown string %s\n", device->entered_string);
|
||||
}
|
||||
|
||||
static void pressLockMethod(Device_t *device) {
|
||||
printf("locking device\n");
|
||||
setLockState(device);
|
||||
}
|
||||
|
||||
void setUnlockState(Device_t *device) {
|
||||
device->state = (DeviceState_t){
|
||||
.state_name = name,
|
||||
.device = device,
|
||||
.methods = (DeviceInterface_t){
|
||||
.pressPwr = &pressPwrMethod,
|
||||
.pressStrInput = &pressStrInputMethod,
|
||||
.pressLock = &pressLockMethod,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user