From 1bbf652d172c93ba1eba29945b1cf742ddb6e2ab Mon Sep 17 00:00:00 2001 From: xeovalyte Date: Thu, 27 Mar 2025 08:43:41 +0100 Subject: [PATCH] Added line follower boilerplate --- devenv.lock | 49 +++++++++++++++-------------- devenv.nix | 9 +++++- line-follower/leelib.c | 71 ++++++++++++++++++++++++++++++++++++++++++ line-follower/leelib.h | 19 +++++++++++ 4 files changed, 124 insertions(+), 24 deletions(-) create mode 100644 line-follower/leelib.c create mode 100644 line-follower/leelib.h diff --git a/devenv.lock b/devenv.lock index 1336316..437abc8 100644 --- a/devenv.lock +++ b/devenv.lock @@ -31,10 +31,31 @@ "type": "github" } }, + "git-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1742649964, + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "dcf5072734cb576d2b0c59b2ac44f5050b5eac82", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, "gitignore": { "inputs": { "nixpkgs": [ - "pre-commit-hooks", + "git-hooks", "nixpkgs" ] }, @@ -66,32 +87,14 @@ "type": "github" } }, - "pre-commit-hooks": { - "inputs": { - "flake-compat": "flake-compat", - "gitignore": "gitignore", - "nixpkgs": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1737465171, - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "rev": "9364dc02281ce2d37a1f55b6e51f7c0f65a75f17", - "type": "github" - }, - "original": { - "owner": "cachix", - "repo": "pre-commit-hooks.nix", - "type": "github" - } - }, "root": { "inputs": { "devenv": "devenv", + "git-hooks": "git-hooks", "nixpkgs": "nixpkgs", - "pre-commit-hooks": "pre-commit-hooks" + "pre-commit-hooks": [ + "git-hooks" + ] } } }, diff --git a/devenv.nix b/devenv.nix index 9b45afd..e5d498f 100644 --- a/devenv.nix +++ b/devenv.nix @@ -4,7 +4,14 @@ env.GREET = "devenv"; # https://devenv.sh/packages/ - packages = [ pkgs.verilator pkgs.gtkwave ]; + packages = [ + pkgs.verilator + pkgs.gtkwave + pkgs.clang-tools + pkgs.clang + ]; + + scripts.hello.exec = '' echo hello from $GREET diff --git a/line-follower/leelib.c b/line-follower/leelib.c new file mode 100644 index 0000000..232018d --- /dev/null +++ b/line-follower/leelib.c @@ -0,0 +1,71 @@ +#include +#include + +void readEdge (int *i, int *j) +/* Scans input to read a blocked edge. + Returns the indices i and j in m[13][13] of an edge. + Call this function with pointers to the variables + that should receive the values, e.g. + readEdge (&x, &y); +*/ +{ + int cx, cy; + char d; + scanf ("%d %d %c", &cy, &cx, &d); + if (d == 's') { + *i = 2 + cy * 2 + 1; + *j = 2 + cx * 2; + } + else if (d == 'e') { + *i = 2 + cy * 2; + *j = 2 + cx * 2 + 1; + } + else { + fprintf (stderr, "error on direction: %c\n", d); + exit (1); + } +} + +void readStation (int *i, int *j) +/* Scans input to read a station. + Returns the indices i and j in m[13][13] of a station. + */ +{ + int s; + scanf ("%d", &s); + switch (s) { + case 1: *i = 12, *j = 4; break; + case 2: *i = 12, *j = 6; break; + case 3: *i = 12, *j = 8; break; + case 4: *i = 8, *j = 12; break; + case 5: *i = 6, *j = 12; break; + case 6: *i = 4, *j = 12; break; + case 7: *i = 0, *j = 8; break; + case 8: *i = 0, *j = 6; break; + case 9: *i = 0, *j = 4; break; + case 10: *i = 4, *j = 0; break; + case 11: *i = 6, *j = 0; break; + case 12: *i = 8, *j = 0; break; + default: fprintf (stderr, "Illegal station\n"); exit (-1); + } +} + +void printCrossingName (int i, int j) +/* Print the name of the crossing with indices i and j in m[13][13]/ */ +{ + if ((i-2)%2 == 0 && (j-2)%2 == 0) + printf ("c%d%d ", (i-2)/2, (j-2)/2); +} + +void printMatrix (int m[][13]) +/* Print the elements of the matrix m[13][13]. */ +{ + int i, j; + + for (i = 0; i < 13; i++) { + for (j = 0; j < 13; j++) { + printf ("%2d ", m[i][j]); + } + printf ("\n"); + } +} diff --git a/line-follower/leelib.h b/line-follower/leelib.h new file mode 100644 index 0000000..58f95fb --- /dev/null +++ b/line-follower/leelib.h @@ -0,0 +1,19 @@ +void readEdge (int *i, int *j); +/* Scans input to read a blocked edge. + Returns the indices i and j in m[13][13] of an edge. + Call this function with pointers to the variables + that should receive the values, e.g. + readEdge (&x, &y); +*/ + +void readStation (int *i, int *j); +/* Scans input to read a station. + Returns the indices i and j in m[13][13] of a station. + */ + +void printCrossingName (int i, int j); +/* Print the name of the crossing with indices i and j in m[13][13]/ */ + +void printMatrix (int m[][13]); +/* Print the elements of the matrix m[13][13]. */ +