uni

Thing1's amazing uni repo
Log | Files | Refs

commit b4c95c3ee1144a298a308ae9996aa767022c68af
parent d524e233454e6048afa46064c4207ad8bf0b1743
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Tue, 28 Oct 2025 17:36:22 +0000

did a lab

Diffstat:
ACS12020/labs/lab5/Makefile | 10++++++++++
ACS12020/labs/lab5/lab5.ino | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ACS12020/labs/lab5/util.h | 21+++++++++++++++++++++
AMP10610/28.10.25.md | 2++
4 files changed, 114 insertions(+), 0 deletions(-)

diff --git a/CS12020/labs/lab5/Makefile b/CS12020/labs/lab5/Makefile @@ -0,0 +1,10 @@ +all: compile upload + +upload: compile + arduino-cli upload --fqbn arduino:avr:uno . -p /dev/ttyACM0 + +compile: lab5.ino + arduino-cli compile --fqbn arduino:avr:uno . + +monitor: + arduino-cli monitor -p /dev/ttyACM0 diff --git a/CS12020/labs/lab5/lab5.ino b/CS12020/labs/lab5/lab5.ino @@ -0,0 +1,81 @@ +#include "util.h" + +#define NUMBER_OF_RUNNERS 16 + +/* DO NOT TOUCH, cursed, fills in printf, it works don't question */ +#define fill_printf_from_secs(t) (int)(t % 60), \ + (int)((int)(t / 60) % 60), \ + (int)((int)((int)(t / 60) / 60) % 60) + +const char *order[] = {"Secs", "Mins", "Hours"}; +const int time_orders = SIZE(order); + +const char *sports[] = {"swim", "cycle", "run"}; +const int number_of_sports = SIZE(sports); + +int +input_time(const char *order, const char *sport) { + SEprintf("How many %s did %s take: ", order, sport); + return readint(); +} + +int +input_sport(const char *sport, int times[time_orders]) { + for (int i = 0; i < time_orders; i++) + if ((times[i] = input_time(order[i], sport)) == -1) + return 1; + return 0; +} + +void +normalize(int times[time_orders]) { + for (int i = 0; i < time_orders - 1; i++) { + times[i + 1] += times[i] / 60; + times[i] %= 60; + } +} + +long +in_secs(int time[number_of_sports]) { + return (long)((time[0]) + (time[1] * 60) + (time[2] * 60 * 60)); +} + +void +setup() { + Serial.begin(9600); + + int sport_times[number_of_sports + 1][time_orders] = {0}; + long times[NUMBER_OF_RUNNERS] = {0}; + int runner; + + for (runner = 0; runner < NUMBER_OF_RUNNERS; runner++) { + memset(sport_times, 0, sizeof(sport_times)); + for (int i = 0; i < number_of_sports; i++) { + if (input_sport(sports[i], sport_times[i])) + goto end; + } + + for (int i = 0; i < number_of_sports; i++) + for (int j = 0; j < time_orders; j++) + sport_times[time_orders][j] += sport_times[i][j]; + + normalize(sport_times[3]); + times[runner] = in_secs(sport_times[time_orders]); + + SEprintf("Total time: %dsec %dmin %dhr.\n", fill_printf_from_secs((long)times[runner])); + } +end: + int best_index = -1; + long best = -1; + for (int i = 0; i < runner; i++) { + if (best < times[i]) { + best = times[i]; + best_index = i; + } + + SEprintf("Runner %d: %dsec %dmin %dhr.\n", i + 1, fill_printf_from_secs(times[i])); + } + SEprintf("The best runner was runner %d, with a time of %dsec %dmin %dhr.\n", best_index + 1, fill_printf_from_secs(best)); +} + +void loop() {} diff --git a/CS12020/labs/lab5/util.h b/CS12020/labs/lab5/util.h @@ -0,0 +1,21 @@ +#define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) + +void +SEprintf(const char *fmt, ...) { + static char sbuf[64] = { 0 }; + va_list ap; + + va_start(ap, fmt); + vsnprintf(sbuf, 64, fmt, ap); + va_end(ap); + + Serial.print(sbuf); +} + +int +readint() { + while (!Serial.available()) ; + int i = Serial.parseInt(); + SEprintf("\n"); + return i; +} diff --git a/MP10610/28.10.25.md b/MP10610/28.10.25.md @@ -0,0 +1,2 @@ +28/10/25 +========