Skip to content

Commit

Permalink
fix: Change all print to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
Max851010 committed Feb 4, 2025
1 parent f800d2c commit 011a216
Showing 1 changed file with 55 additions and 57 deletions.
112 changes: 55 additions & 57 deletions examples/C/src/SkiLift/Lift.lf
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
* @author Christian Menard
* @author Edward A. Lee
*/

target C {
// cmake-include: ["c_lib/drawSkiLift.cmake"],
// files:["c_lib/drawSkiLift.c"]
}
target C

import EntryGate from "./EntryGate.lf"
import EntrySensor from "lib/EntrySensor.lf"
import LiftSensor from "lib/LiftSensor.lf"
import ObjectDetector from "lib/ObjectDetector.lf"
//import ScreenPrinter from "lib/ScreenPrinter.lf"

// import ScreenPrinter from "lib/ScreenPrinter.lf"
preamble {=
#define WIDTH 80 // Width of the terminal screen
#define HEIGHT 6 // Number of rows for the ski lift
Expand All @@ -31,28 +27,28 @@ preamble {=
static volatile int offset = 0;
static void moveCursor(int row, int col) {
// Move cursor to (row, col)
printf("\033[%d;%dH", row, col);
fprintf(stdout, "\033[%d;%dH", row, col);
fflush(stdout);
}
static void clearScreen() {
printf("\033[H\033[J"); // ANSI escape code to clear the screen
fprintf(stdout, "\033[H\033[J"); // ANSI escape code to clear the screen
fflush(stdout);
}
static void clearLine() {
printf("\033[2K"); // Clear the entire line
fprintf(stdout, "\033[2K"); // Clear the entire line
fflush(stdout);
}
}



static void drawSkiLift(int offset) {
char screen[HEIGHT][WIDTH];

// Clear the screen array
for (int i = 0; i < HEIGHT; i++) {
memset(screen[i], ' ', WIDTH - 1);
screen[i][WIDTH - 1] = '\0';
}

// Draw the top row of chairs
for (int i = 0; i < NUM_CHAIRS; i++) {
int pos = (i * 8 + offset) % WIDTH;
Expand All @@ -65,7 +61,7 @@ preamble {=
screen[1][pos + 2] = '\\';
}
}

// Draw the bottom row of chairs
for (int i = 0; i < NUM_CHAIRS; i++) {
int pos = (i * 8 - offset + WIDTH) % WIDTH;
Expand All @@ -78,42 +74,42 @@ preamble {=
screen[HEIGHT - 1][pos + 2] = '\\';
}
}

// Draw the left-most and right-most vertical lines
for (int i = 0; i < NUM_CHAIRS; i++) {
int pos_top =
(i * 8 + offset) % WIDTH;
int pos_bottom =
(i * 8 - offset + WIDTH) % WIDTH;

// Left vertical connection (top to bottom)
if (pos_top + 1 > 5 && pos_top + 1 < WIDTH - 10) {
screen[2][1] = '|';
screen[3][1] = '|';
}

// Right vertical connection (bottom to top)
if (pos_bottom + 2 > 8 && pos_bottom + 2 < WIDTH - 5) {
screen[2][WIDTH - 6] = '|';
screen[3][WIDTH - 6] = '|';
}
}

// Print the screen to the terminal
for (int i = 0; i < HEIGHT; i++) {
printf("%s\n", screen[i]);
fprintf(stdout, "%s\n", screen[i]);
}
}

static void skiLiftInit() {
clearScreen();
printf("Ski Lift Animation\n");
printf(
fprintf(stdout, "Ski Lift Animation\n");
fprintf(stdout,
"---------------------------------------------------------------------"
"-------\n\n\n");
drawSkiLift(offset);
printf("\n\n");
printf(
fprintf(stdout, "\n\n");
fprintf(stdout,
"---------------------------------------------------------------------"
"-------\n");
offset = (offset + 1) % WIDTH; // Update the offset for the next frame
Expand All @@ -124,67 +120,71 @@ preamble {=
clearLine();
}
drawSkiLift(offset);
printf("\n\n");
printf(
fprintf(stdout, "\n\n");
fprintf(stdout,
"---------------------------------------------------------------------"
"-------\n");
offset = (offset + 1) % WIDTH;
}
static void printMotionStatus(const char* str_motion) {
moveCursor(ANIMATION_HEIGHT + 1, 1);
clearLine();
printf("Lift Status: %s\n", str_motion);
fprintf(stdout, "Lift Status: %s\n", str_motion);
fflush(stdout);
}
static void printGateStatus(const char* str_gate) {
moveCursor(ANIMATION_HEIGHT + 2, 1);
clearLine();
printf("Gate Status: %s\n", str_gate);
fprintf(stdout, "Gate Status: %s\n", str_gate);
fflush(stdout);
moveCursor(ANIMATION_HEIGHT + 3, 1);
printf("\033[J");
fprintf(stdout, "\033[J");
fflush(stdout);
}
=}

main reactor Lift {
preamble {=
static int32_t passenger_cnt = 0;
=}
state move: bool = true;
state open: bool = false;
state stay_cur_mode: bool = true;
state count: int32_t = 0;
state start_number: int32_t = 0;
state end_number: int32_t = 0;
state move: bool = true
state open: bool = false
state stay_cur_mode: bool = true
state count: int32_t = 0
state start_number: int32_t = 0
state end_number: int32_t = 0
liftMotion = new LiftMotion()
entryGate = new EntryGate()
entrySensor = new EntrySensor()
liftSensor = new LiftSensor()
objectDetector = new ObjectDetector()

timer t(0, 500 msec)

state gate_t_counter: int32_t = 0
timer t_gate(0, 1 sec)

// Start the program (When staff press the start button of the lift)
reaction(startup) -> liftMotion.move, entryGate.open {=
skiLiftInit();
lf_set(liftMotion.move, self->move);
=}

// Behaviors on the start of the lift
// reaction(liftSensor.start_ready) -> entryGate.open {=
// if (liftSensor.start_ready->value) {
// self->open = true;
// reaction(liftSensor.start_ready) -> entryGate.open {=
// if (liftSensor.start_ready->value) {
// self->open = true;
// } else {
// self->open = false;
// self->open = false;
// }
// lf_set(entryGate.open, self->open);
// lf_set(entryGate.open, self->open);
// =}

reaction(objectDetector.start_number) {=
if (objectDetector.start_number != 0) {
self->start_number = objectDetector.start_number->value;
}
=}


timer t(0, 500 msec);
reaction(t) -> liftMotion.move {=
// start_number from objectDetector
// passenger_cnt from entrySensor
Expand All @@ -200,8 +200,6 @@ main reactor Lift {
lf_set(liftMotion.move, self->move);
=}

state gate_t_counter: int32_t = 0;
timer t_gate(0, 1 sec);
reaction(t_gate) -> entryGate.open {=
if (self->gate_t_counter % 4 == 0) {
self->open = true;
Expand All @@ -220,35 +218,35 @@ main reactor Lift {
passenger_cnt ++;
}
=}

reaction(entrySensor.gate_2) {=
if (entrySensor.gate_2->value) {
passenger_cnt ++;
}
=}

reaction(entrySensor.gate_3) {=
if (entrySensor.gate_3->value) {
passenger_cnt ++;
}
=}

// Behavior on the end of the lift
// reaction(liftSensor.end_ready) -> liftMotion.move {=
// if (liftSensor.end_ready->value) {
// if (self->end_number != 0) {
// // self->move = false;
// } else {
// // self->move = true;
// }
// //lf_set(liftMotion.move, self->move);
// }
// =}

// reaction(liftSensor.end_ready) -> liftMotion.move {=
// if (liftSensor.end_ready->value) {
// if (self->end_number != 0) {
// // self->move = false;
// } else {
// // self->move = true;
// }
// //lf_set(liftMotion.move, self->move);
// }
// =}
reaction(objectDetector.end_number) {=
if (objectDetector.end_number != 0) {
self->end_number = objectDetector.end_number->value;
}
=}

}

reactor LiftMotion {
Expand Down

0 comments on commit 011a216

Please sign in to comment.