Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
clnhlzmn committed Mar 23, 2021
1 parent 2a1207c commit ef2f424
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 34 deletions.
79 changes: 50 additions & 29 deletions example/oven.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,68 @@ static int oven_open(struct oven *, struct oven_event *);

static int oven_closed_idle(struct oven *self, struct oven_event *event) {
if (!self || !event) return -1;
if (event->id == oven_event_start && 1) {
self->state = NULL;
self->state = oven_closed_cooking;
enable_heater(self, event);
return 0;
}
if (event->id == oven_event_open && 1) {
self->state = NULL;
self->state = oven_open;
return 0;
switch (event->id) {
case oven_event_start:
if (1) {
self->state = NULL;
self->state = oven_closed_cooking;
enable_heater(self, event);
break;
}
break;
case oven_event_open:
if (1) {
self->state = NULL;
self->state = oven_open;
break;
}
break;
default: break;
}
return 0;
}

static int oven_closed_cooking(struct oven *self, struct oven_event *event) {
if (!self || !event) return -1;
if (event->id == oven_event_timeout && 1) {
disable_heater(self, event);
self->state = NULL;
self->state = oven_closed_idle;
return 0;
}
if (event->id == oven_event_open && 1) {
disable_heater(self, event);
self->state = NULL;
self->state = oven_open;
return 0;
switch (event->id) {
case oven_event_timeout:
if (1) {
disable_heater(self, event);
self->state = NULL;
self->state = oven_closed_idle;
break;
}
break;
case oven_event_open:
if (1) {
disable_heater(self, event);
self->state = NULL;
self->state = oven_open;
break;
}
break;
default: break;
}
return 0;
}

static int oven_open(struct oven *self, struct oven_event *event) {
if (!self || !event) return -1;
if (event->id == oven_event_close && 1) {
self->state = NULL;
self->state = oven_closed_idle;
return 0;
}
if (event->id == oven_event_start && 1) {
error(self, event);
return 0;
switch (event->id) {
case oven_event_close:
if (1) {
self->state = NULL;
self->state = oven_closed_idle;
break;
}
break;
case oven_event_start:
if (1) {
error(self, event);
break;
}
break;
default: break;
}
return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions example/oven.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ enum oven_event_id {
};

struct oven_event {
void * ctx;
enum oven_event_id id;
void *ctx;
};

struct oven {
int (*state)(struct oven *, struct oven_event *);
void *ctx;
void * ctx;
};

int oven_init(struct oven *);
Expand Down
6 changes: 3 additions & 3 deletions makina-compiler/src/xyz/colinholzman/makina/CodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class CodeGenerator(val machine: Machine,
println("/*This file was generated by Makina. Do not modify.*/")
println("#ifndef ${machine.id.toUpperCase()}_H")
println("#define ${machine.id.toUpperCase()}_H")
println()
for (include in includes) {
println()
println("#include \"$include\"")
}
println()
Expand Down Expand Up @@ -105,8 +105,8 @@ class CodeGenerator(val machine: Machine,
println("\tif (!self || !event) return -1;")
val config = activeLeafState.getStateConfiguration()
println("\tswitch (event->id) {")
val handlerStatePairs = config.getHandlers().groupByIdAndRemoveRedundantHandlers()
for (entry in handlerStatePairs) {
val handlerGroups = config.getHandlers().groupByIdAndRemoveRedundantHandlers()
for (entry in handlerGroups) {
val eventId = entry.key
println("\tcase ${machine.id}_event_$eventId:")
for (handlerStatePair in entry.value) {
Expand Down

0 comments on commit ef2f424

Please sign in to comment.