Compare commits

..

4 Commits

Author SHA1 Message Date
sirlilpanda
0ed9111af3 changed from setXState to a more discripive setDeviceStateToX 2025-08-29 00:12:44 +12:00
sirlilpanda
aead22a646 added more test cases 2025-08-29 00:11:55 +12:00
sirlilpanda
4734b778d8 added more comments 2025-08-29 00:11:41 +12:00
sirlilpanda
034d87394e re-enabling warnings 2025-08-29 00:11:08 +12:00
8 changed files with 50 additions and 23 deletions

6
nob.c
View File

@@ -35,9 +35,9 @@
SRC_FOLDER"states/debug_state.c", \
SRC_FOLDER"states/off_state.c", \
#define C_ARGS
// "-Wall", \
// "-Wextra"
#define C_ARGS \
"-Wall", \
"-Wextra"
int main(int argc, char **argv)
{

View File

@@ -24,9 +24,10 @@
// │ │
// └────────┘
// forward decl
typedef struct Device_s Device_t;
// the interface that you would use to interface with the state
typedef struct DeviceInterface_s{
// all the actions that change the state
// normally there wouldnt be any args
@@ -38,12 +39,14 @@ typedef struct DeviceInterface_s{
void (*pressLock)(Device_t*);
}DeviceInterface_t;
// the states themselves
typedef struct DeviceState_s{
const char* state_name;
Device_t* device;
DeviceInterface_t methods;
}DeviceState_t;
// the device
struct Device_s{
// in theroy this is also the flyweight pattern
DeviceState_t state;
@@ -53,5 +56,5 @@ struct Device_s{
DeviceInterface_t methods;
};
// creates the device
void initDevice(Device_t* device);

View File

@@ -1,21 +1,36 @@
#include "states/state.h"
#include "device.h"
#include <stdio.h>
#include <string.h>
int main() {
Device_t device;
initDevice(&device);
// 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
device.methods.pressPwr(&device);
if (strcmp(device.state.state_name, "lock_state.c") != 0) return 0;
// send the current string
device.methods.pressStrInput(&device);
if (strcmp(device.state.state_name, "lock_state.c") != 0) return 0;
// send in the right string
device.entered_string = "pwd";
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;
}

View File

@@ -7,19 +7,20 @@ const char debug_state_name[] = __FILE_NAME__;
static void pressPwrMethod(Device_t *device) {
printf("turning off device\n");
setOffState(device);
setDeviceStateToOff(device);
}
static void pressStrInputMethod(Device_t *device) {
(void) device;
printf("nothing happens\n");
}
static void pressLockMethod(Device_t *device) {
printf("locking device\n");
setOffState(device);
setDeviceStateToOff(device);
}
void setDebugState(Device_t *device) {
void setDeviceStateToDebug(Device_t *device) {
device->state = (DeviceState_t){
.state_name = debug_state_name,
.device = device,

View File

@@ -8,18 +8,18 @@ const char lock_state_name[] = __FILE_NAME__;
static void pressPwrMethod(Device_t *device) {
printf("turning off device\n");
setOffState(device);
setDeviceStateToOff(device);
}
static void pressStrInputMethod(Device_t *device) {
if (strcmp(device->entered_string, "dbg") == 0) {
printf("entering debug state\n");
setDebugState(device);
setDeviceStateToDebug(device);
return;
}
if (strcmp(device->entered_string, "pwd") == 0) {
printf("entering unlock state\n");
setUnlockState(device);
setDeviceStateToUnlock(device);
return;
}
@@ -28,10 +28,11 @@ static void pressStrInputMethod(Device_t *device) {
}
static void pressLockMethod(Device_t *device) {
(void) device;
printf("nothing happens\n");
}
void setLockState(Device_t *device) {
void setDeviceStateToLock(Device_t *device) {
device->state = (DeviceState_t){
.state_name = lock_state_name,
.device = device,

View File

@@ -6,18 +6,20 @@ const char off_state_name[] = __FILE_NAME__;
static void pressPwrMethod(Device_t *device) {
printf("turning on device\n");
setLockState(device);
setDeviceStateToLock(device);
}
static void pressStrInputMethod(Device_t *device) {
(void) device;
printf("nothing happens\n");
}
static void pressLockMethod(Device_t *device) {
(void) device;
printf("nothing happens\n");
}
void setOffState(Device_t *device) {
void setDeviceStateToOff(Device_t *device) {
device->state = (DeviceState_t){
.state_name = off_state_name,
.device = device,

View File

@@ -1,7 +1,9 @@
#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);
// why nothing working?
// each of these are each state
void setDeviceStateToLock(Device_t *device);
void setDeviceStateToUnlock(Device_t *device);
void setDeviceStateToDebug(Device_t *device);
void setDeviceStateToOff(Device_t *device);

View File

@@ -3,27 +3,30 @@
#include <stdio.h>
#include <string.h>
// this is me being lazy
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) {
printf("turning off device\n");
setOffState(device);
setDeviceStateToOff(device);
}
static void pressStrInputMethod(Device_t *device) {
if (strcmp(device->entered_string, "dbg") == 0) {
printf("entering debug state\n");
setDebugState(device);
setDeviceStateToDebug(device);
return;
}
printf("unknown string %s\n", device->entered_string);
}
static void pressLockMethod(Device_t *device) {
printf("locking device\n");
setLockState(device);
setDeviceStateToLock(device);
}
void setUnlockState(Device_t *device) {
void setDeviceStateToUnlock(Device_t *device) {
device->state = (DeviceState_t){
.state_name = name,
.device = device,