Compare commits
4 Commits
d4e72630e9
...
0ed9111af3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ed9111af3 | ||
|
|
aead22a646 | ||
|
|
4734b778d8 | ||
|
|
034d87394e |
6
nob.c
6
nob.c
@@ -35,9 +35,9 @@
|
|||||||
SRC_FOLDER"states/debug_state.c", \
|
SRC_FOLDER"states/debug_state.c", \
|
||||||
SRC_FOLDER"states/off_state.c", \
|
SRC_FOLDER"states/off_state.c", \
|
||||||
|
|
||||||
#define C_ARGS
|
#define C_ARGS \
|
||||||
// "-Wall", \
|
"-Wall", \
|
||||||
// "-Wextra"
|
"-Wextra"
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -24,9 +24,10 @@
|
|||||||
// │ │
|
// │ │
|
||||||
// └────────┘
|
// └────────┘
|
||||||
|
|
||||||
|
// forward decl
|
||||||
typedef struct Device_s Device_t;
|
typedef struct Device_s Device_t;
|
||||||
|
|
||||||
|
// the interface that you would use to interface with the state
|
||||||
typedef struct DeviceInterface_s{
|
typedef struct DeviceInterface_s{
|
||||||
// all the actions that change the state
|
// all the actions that change the state
|
||||||
// normally there wouldnt be any args
|
// normally there wouldnt be any args
|
||||||
@@ -38,12 +39,14 @@ typedef struct DeviceInterface_s{
|
|||||||
void (*pressLock)(Device_t*);
|
void (*pressLock)(Device_t*);
|
||||||
}DeviceInterface_t;
|
}DeviceInterface_t;
|
||||||
|
|
||||||
|
// the states themselves
|
||||||
typedef struct DeviceState_s{
|
typedef struct DeviceState_s{
|
||||||
const char* state_name;
|
const char* state_name;
|
||||||
Device_t* device;
|
Device_t* device;
|
||||||
DeviceInterface_t methods;
|
DeviceInterface_t methods;
|
||||||
}DeviceState_t;
|
}DeviceState_t;
|
||||||
|
|
||||||
|
// the device
|
||||||
struct Device_s{
|
struct Device_s{
|
||||||
// in theroy this is also the flyweight pattern
|
// in theroy this is also the flyweight pattern
|
||||||
DeviceState_t state;
|
DeviceState_t state;
|
||||||
@@ -53,5 +56,5 @@ struct Device_s{
|
|||||||
DeviceInterface_t methods;
|
DeviceInterface_t methods;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// creates the device
|
||||||
void initDevice(Device_t* device);
|
void initDevice(Device_t* device);
|
||||||
|
|||||||
17
src/main.c
17
src/main.c
@@ -1,21 +1,36 @@
|
|||||||
#include "states/state.h"
|
#include "states/state.h"
|
||||||
#include "device.h"
|
#include "device.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Device_t device;
|
Device_t device;
|
||||||
initDevice(&device);
|
initDevice(&device);
|
||||||
// sets the device to off on start up
|
// sets the device to off on start up
|
||||||
setOffState(&device);
|
setDeviceStateToOff(&device);
|
||||||
|
if (strcmp(device.state.state_name, "off_state.c") != 0) return 0;
|
||||||
|
|
||||||
// turn on the device
|
// turn on the device
|
||||||
device.methods.pressPwr(&device);
|
device.methods.pressPwr(&device);
|
||||||
|
if (strcmp(device.state.state_name, "lock_state.c") != 0) return 0;
|
||||||
|
|
||||||
// send the current string
|
// send the current string
|
||||||
device.methods.pressStrInput(&device);
|
device.methods.pressStrInput(&device);
|
||||||
|
if (strcmp(device.state.state_name, "lock_state.c") != 0) return 0;
|
||||||
|
|
||||||
// send in the right string
|
// send in the right string
|
||||||
device.entered_string = "pwd";
|
device.entered_string = "pwd";
|
||||||
device.methods.pressStrInput(&device);
|
device.methods.pressStrInput(&device);
|
||||||
|
if (strcmp(device.state.state_name, "unlock_state.c") != 0) return 0;
|
||||||
|
|
||||||
|
// try to enter debug mode
|
||||||
|
device.entered_string = "dbg";
|
||||||
|
device.methods.pressStrInput(&device);
|
||||||
|
if (strcmp(device.state.state_name, "debug_state.c") != 0) return 0;
|
||||||
|
|
||||||
|
// try to power down
|
||||||
|
device.methods.pressPwr(&device);
|
||||||
|
if (strcmp(device.state.state_name, "off_state.c") != 0) return 0;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,19 +7,20 @@ const char debug_state_name[] = __FILE_NAME__;
|
|||||||
|
|
||||||
static void pressPwrMethod(Device_t *device) {
|
static void pressPwrMethod(Device_t *device) {
|
||||||
printf("turning off device\n");
|
printf("turning off device\n");
|
||||||
setOffState(device);
|
setDeviceStateToOff(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pressStrInputMethod(Device_t *device) {
|
static void pressStrInputMethod(Device_t *device) {
|
||||||
|
(void) device;
|
||||||
printf("nothing happens\n");
|
printf("nothing happens\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pressLockMethod(Device_t *device) {
|
static void pressLockMethod(Device_t *device) {
|
||||||
printf("locking device\n");
|
printf("locking device\n");
|
||||||
setOffState(device);
|
setDeviceStateToOff(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDebugState(Device_t *device) {
|
void setDeviceStateToDebug(Device_t *device) {
|
||||||
device->state = (DeviceState_t){
|
device->state = (DeviceState_t){
|
||||||
.state_name = debug_state_name,
|
.state_name = debug_state_name,
|
||||||
.device = device,
|
.device = device,
|
||||||
|
|||||||
@@ -8,18 +8,18 @@ const char lock_state_name[] = __FILE_NAME__;
|
|||||||
|
|
||||||
static void pressPwrMethod(Device_t *device) {
|
static void pressPwrMethod(Device_t *device) {
|
||||||
printf("turning off device\n");
|
printf("turning off device\n");
|
||||||
setOffState(device);
|
setDeviceStateToOff(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pressStrInputMethod(Device_t *device) {
|
static void pressStrInputMethod(Device_t *device) {
|
||||||
if (strcmp(device->entered_string, "dbg") == 0) {
|
if (strcmp(device->entered_string, "dbg") == 0) {
|
||||||
printf("entering debug state\n");
|
printf("entering debug state\n");
|
||||||
setDebugState(device);
|
setDeviceStateToDebug(device);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (strcmp(device->entered_string, "pwd") == 0) {
|
if (strcmp(device->entered_string, "pwd") == 0) {
|
||||||
printf("entering unlock state\n");
|
printf("entering unlock state\n");
|
||||||
setUnlockState(device);
|
setDeviceStateToUnlock(device);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,10 +28,11 @@ static void pressStrInputMethod(Device_t *device) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void pressLockMethod(Device_t *device) {
|
static void pressLockMethod(Device_t *device) {
|
||||||
|
(void) device;
|
||||||
printf("nothing happens\n");
|
printf("nothing happens\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLockState(Device_t *device) {
|
void setDeviceStateToLock(Device_t *device) {
|
||||||
device->state = (DeviceState_t){
|
device->state = (DeviceState_t){
|
||||||
.state_name = lock_state_name,
|
.state_name = lock_state_name,
|
||||||
.device = device,
|
.device = device,
|
||||||
|
|||||||
@@ -6,18 +6,20 @@ const char off_state_name[] = __FILE_NAME__;
|
|||||||
|
|
||||||
static void pressPwrMethod(Device_t *device) {
|
static void pressPwrMethod(Device_t *device) {
|
||||||
printf("turning on device\n");
|
printf("turning on device\n");
|
||||||
setLockState(device);
|
setDeviceStateToLock(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pressStrInputMethod(Device_t *device) {
|
static void pressStrInputMethod(Device_t *device) {
|
||||||
|
(void) device;
|
||||||
printf("nothing happens\n");
|
printf("nothing happens\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pressLockMethod(Device_t *device) {
|
static void pressLockMethod(Device_t *device) {
|
||||||
|
(void) device;
|
||||||
printf("nothing happens\n");
|
printf("nothing happens\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOffState(Device_t *device) {
|
void setDeviceStateToOff(Device_t *device) {
|
||||||
device->state = (DeviceState_t){
|
device->state = (DeviceState_t){
|
||||||
.state_name = off_state_name,
|
.state_name = off_state_name,
|
||||||
.device = device,
|
.device = device,
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../device.h"
|
#include "../device.h"
|
||||||
|
|
||||||
void setLockState(Device_t *device);
|
// why nothing working?
|
||||||
void setUnlockState(Device_t *device);
|
// each of these are each state
|
||||||
void setDebugState(Device_t *device);
|
void setDeviceStateToLock(Device_t *device);
|
||||||
void setOffState(Device_t *device);
|
void setDeviceStateToUnlock(Device_t *device);
|
||||||
|
void setDeviceStateToDebug(Device_t *device);
|
||||||
|
void setDeviceStateToOff(Device_t *device);
|
||||||
@@ -3,27 +3,30 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
// this is me being lazy
|
||||||
const char name[] = __FILE_NAME__;
|
const char name[] = __FILE_NAME__;
|
||||||
|
|
||||||
|
// so when a button is pressed it updates the state to the new one
|
||||||
static void pressPwrMethod(Device_t *device) {
|
static void pressPwrMethod(Device_t *device) {
|
||||||
printf("turning off device\n");
|
printf("turning off device\n");
|
||||||
setOffState(device);
|
setDeviceStateToOff(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pressStrInputMethod(Device_t *device) {
|
static void pressStrInputMethod(Device_t *device) {
|
||||||
if (strcmp(device->entered_string, "dbg") == 0) {
|
if (strcmp(device->entered_string, "dbg") == 0) {
|
||||||
printf("entering debug state\n");
|
printf("entering debug state\n");
|
||||||
setDebugState(device);
|
setDeviceStateToDebug(device);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
printf("unknown string %s\n", device->entered_string);
|
printf("unknown string %s\n", device->entered_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void pressLockMethod(Device_t *device) {
|
static void pressLockMethod(Device_t *device) {
|
||||||
printf("locking device\n");
|
printf("locking device\n");
|
||||||
setLockState(device);
|
setDeviceStateToLock(device);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUnlockState(Device_t *device) {
|
void setDeviceStateToUnlock(Device_t *device) {
|
||||||
device->state = (DeviceState_t){
|
device->state = (DeviceState_t){
|
||||||
.state_name = name,
|
.state_name = name,
|
||||||
.device = device,
|
.device = device,
|
||||||
|
|||||||
Reference in New Issue
Block a user