uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

commit c7ba5d17c2709fd46eb48bc454baf4e39ff1e556
parent 589b111f08cbc5d928bdc414ed9edff9ff27ea6a
Author: Christos Margiolis <christos@margiolis.net>
Date:   Fri,  9 Dec 2022 19:09:44 +0200

new stuff

Diffstat:
Ac_microcomputers/ex1/doc.pdf | 0
Ac_microcomputers/ex1/doc.tex | 40++++++++++++++++++++++++++++++++++++++++
Ac_microcomputers/ex1/servo.ino | 46++++++++++++++++++++++++++++++++++++++++++++++
Ac_microcomputers/ex1/servo.png | 0
Ac_microcomputers/ex2/doc.pdf | 0
Ac_microcomputers/ex2/doc.tex | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ac_microcomputers/ex2/traffic.ino | 106+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ac_microcomputers/ex2/traffic.png | 0
Ac_microcomputers/ex4/doc.pdf | 0
Ac_microcomputers/ex4/doc.tex | 44++++++++++++++++++++++++++++++++++++++++++++
Ac_microcomputers/ex4/freertos.ino | 134+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ac_microcomputers/ex4/freertos.png | 0
Ac_parallel_systems/ex1/Makefile | 10++++++++++
Ac_parallel_systems/ex1/ex1.c | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14 files changed, 593 insertions(+), 0 deletions(-)

diff --git a/c_microcomputers/ex1/doc.pdf b/c_microcomputers/ex1/doc.pdf Binary files differ. diff --git a/c_microcomputers/ex1/doc.tex b/c_microcomputers/ex1/doc.tex @@ -0,0 +1,40 @@ +\documentclass{article} +\usepackage[utf8]{inputenc} +\usepackage[greek,english]{babel} +\usepackage{alphabeta} +\usepackage{fancyhdr} +\usepackage{listings} +\usepackage{mathtools} +\usepackage{xcolor} +\usepackage[backend=bibtex]{biblatex} +\usepackage{hyperref} +\usepackage[left=1cm,right=1cm]{geometry} +\hypersetup{ + colorlinks=true, + linktoc=all, + linkcolor=black, +} +\lstset { + basicstyle=\ttfamily, + columns=fullflexible, + breaklines=true, + keepspaces=true, + showstringspaces=false +} + +\title{Μικροϋπολογιστές: Εργαστηριακή άσκηση 1} +\author{Χρήστος Μαργιώλης -- 19390133} +\date{Νοέμβριος 2022} + +\begin{document} + +\begin{titlepage} + \maketitle +\end{titlepage} + +\includegraphics[width=\linewidth]{servo.png} +\pagebreak + +\lstinputlisting[language=C]{servo.ino} + +\end{document} diff --git a/c_microcomputers/ex1/servo.ino b/c_microcomputers/ex1/servo.ino @@ -0,0 +1,46 @@ +#include <Servo.h> + +/* photo resistor values tested and found on tinkercad */ +#define LIGHT_NONE 640 +#define LIGHT_FULL 33 +#define SEVENTY_PERC (180 * (70 / (float)100)) + +Servo servo; + +void +setup() +{ + servo.attach(9); + Serial.begin(9600); +} + +void +loop() +{ + int pr_out, pr_in, pos = 0; + + pr_out = analogRead(A0); /* exterior photoresistor */ + pr_in = analogRead(A1); /* interior photoresistor */ + + if (pr_out == LIGHT_NONE) { + pos = 0; + servo_write_and_delay(pos); + Serial.println("night"); + } else if (pr_out < pr_in) { + pos = map(pr_out, LIGHT_NONE, LIGHT_FULL, 0, 180); + servo_write_and_delay(pos); + Serial.println("day"); + } else { + pos = SEVENTY_PERC; + servo_write_and_delay(pos); + Serial.println("peak"); + } + Serial.println(pos); +} + +void +servo_write_and_delay(int pos) +{ + servo.write(pos); + delay(15); +} diff --git a/c_microcomputers/ex1/servo.png b/c_microcomputers/ex1/servo.png Binary files differ. diff --git a/c_microcomputers/ex2/doc.pdf b/c_microcomputers/ex2/doc.pdf Binary files differ. diff --git a/c_microcomputers/ex2/doc.tex b/c_microcomputers/ex2/doc.tex @@ -0,0 +1,70 @@ +\documentclass{article} +\usepackage[utf8]{inputenc} +\usepackage[greek,english]{babel} +\usepackage{alphabeta} +\usepackage{fancyhdr} +\usepackage{listings} +\usepackage{mathtools} +\usepackage{xcolor} +\usepackage[backend=bibtex]{biblatex} +\usepackage{hyperref} +\usepackage[left=1cm,right=1cm]{geometry} +\hypersetup{ + colorlinks=true, + linktoc=all, + linkcolor=black, +} +\lstset { + basicstyle=\ttfamily, + columns=fullflexible, + breaklines=true, + keepspaces=true, + showstringspaces=false +} + +\title{Μικροϋπολογιστές: Εργαστηριακή άσκηση 2} +\author{Χρήστος Μαργιώλης -- 19390133} +\date{Νοέμβριος 2022} + +\begin{document} + +\begin{titlepage} + \maketitle +\end{titlepage} + +\section{Κύκλωμα} + +\includegraphics[width=\linewidth]{traffic.png} +\pagebreak + +\section{Κώδικας} + +\lstinputlisting[language=C]{traffic.ino} + +\section{Πίνακας καταστάσεων} + +\begin{itemize} + \item $P_1$: Κόκκινο πεζών + \item $P_2$: Πράσινο πεζών + \item $T_1$: Κόκκινο οχημάτων + \item $T_2$: Πορτοκαλί οχημάτων + \item $T_3$: Πράσινο οχημάτων +\end{itemize} + +\begin{center} +\begin{tabular}{|l|l|l|l|l|} + \hline + $P_1$ & $P_2$ & $T_1$ & $T_2$ & $T_3$ \\ + \hline + 1 & 0 & 0 & 0 & 1 \\ + \hline + 1 & 0 & 0 & 1 & 0 \\ + \hline + 0 & 1 & 1 & 0 & 0 \\ + \hline + 1 & 0 & 1 & 0 & 0 \\ + \hline +\end{tabular} +\end{center} + +\end{document} diff --git a/c_microcomputers/ex2/traffic.ino b/c_microcomputers/ex2/traffic.ino @@ -0,0 +1,106 @@ +const int PL_BUTTON = 2; +const int PL_LED_RED = 6; +const int PL_LED_GREEN = 7; +const int TL_LED_RED = 3; +const int TL_LED_ORANGE = 4; +const int TL_LED_GREEN = 5; + +const int NSTATES = 4; + +volatile int curstate = 0; + +ISR(TIMER1_COMPA_vect) +{ + int btn_state; + + btn_state = digitalRead(PL_BUTTON); + if (btn_state == HIGH) { + delay(100); /* lazy debounce */ + curstate = 1; + } +} + +void +timer_init() +{ + noInterrupts(); + TCCR1A = 0; + TCCR1B = 0; + TCNT1 = 0; + OCR1A = 6249; + TCCR1B |= (1 << WGM12) | (1 << CS12); + TIMSK1 |= (1 << OCIE1A); + interrupts(); +} + +void +state0() +{ + digitalWrite(PL_LED_RED, HIGH); + digitalWrite(PL_LED_GREEN, LOW); + digitalWrite(TL_LED_RED, LOW); + digitalWrite(TL_LED_ORANGE, LOW); + digitalWrite(TL_LED_GREEN, HIGH); + delay(4000); +} + +void +state1() +{ + digitalWrite(PL_LED_RED, HIGH); + digitalWrite(PL_LED_GREEN, LOW); + digitalWrite(TL_LED_RED, LOW); + digitalWrite(TL_LED_ORANGE, HIGH); + digitalWrite(TL_LED_GREEN, LOW); + delay(2000); +} + +void +state2() +{ + digitalWrite(PL_LED_RED, LOW); + digitalWrite(PL_LED_GREEN, HIGH); + digitalWrite(TL_LED_RED, HIGH); + digitalWrite(TL_LED_ORANGE, LOW); + digitalWrite(TL_LED_GREEN, LOW); + delay(4000); +} + +void +state3() +{ + digitalWrite(PL_LED_RED, HIGH); + digitalWrite(PL_LED_GREEN, LOW); + digitalWrite(TL_LED_RED, HIGH); + digitalWrite(TL_LED_ORANGE, LOW); + digitalWrite(TL_LED_GREEN, LOW); + delay(3000); +} + +void +setup() +{ + pinMode(PL_BUTTON, INPUT); + pinMode(PL_LED_RED, OUTPUT); + pinMode(PL_LED_GREEN, OUTPUT); + pinMode(TL_LED_RED, OUTPUT); + pinMode(TL_LED_ORANGE, OUTPUT); + pinMode(TL_LED_GREEN, OUTPUT); + + timer_init(); + Serial.begin(9600); +} + +void (*states[NSTATES])() = { + state0, + state1, + state2, + state3, +}; + +void +loop() +{ + curstate %= NSTATES; + (*states[curstate++])(); +} diff --git a/c_microcomputers/ex2/traffic.png b/c_microcomputers/ex2/traffic.png Binary files differ. diff --git a/c_microcomputers/ex4/doc.pdf b/c_microcomputers/ex4/doc.pdf Binary files differ. diff --git a/c_microcomputers/ex4/doc.tex b/c_microcomputers/ex4/doc.tex @@ -0,0 +1,44 @@ +\documentclass{article} +\usepackage[utf8]{inputenc} +\usepackage[greek,english]{babel} +\usepackage{alphabeta} +\usepackage{fancyhdr} +\usepackage{listings} +\usepackage{mathtools} +\usepackage{xcolor} +\usepackage[backend=bibtex]{biblatex} +\usepackage{hyperref} +\usepackage[left=1cm,right=1cm]{geometry} +\hypersetup{ + colorlinks=true, + linktoc=all, + linkcolor=black, +} +\lstset { + basicstyle=\ttfamily, + columns=fullflexible, + breaklines=true, + keepspaces=true, + showstringspaces=false +} + +\title{Μικροϋπολογιστές: Εργαστηριακή άσκηση 4} +\author{Χρήστος Μαργιώλης -- 19390133} +\date{Δεκέμβριος 2022} + +\begin{document} + +\begin{titlepage} + \maketitle +\end{titlepage} + +\section{Κύκλωμα} + +\includegraphics[width=\linewidth]{freertos.png} +\pagebreak + +\section{Κώδικας} + +\lstinputlisting[language=C]{freertos.ino} + +\end{document} diff --git a/c_microcomputers/ex4/freertos.ino b/c_microcomputers/ex4/freertos.ino @@ -0,0 +1,134 @@ +#include <Arduino_FreeRTOS.h> +#include <LiquidCrystal.h> +#include <Servo.h> +#include <queue.h> + +#define PIN_BTN_LEFT 2 +#define PIN_BTN_RIGHT 3 +#define PIN_BTN_ALARM 13 +#define PIN_TURN_LEFT 4 +#define PIN_TURN_RIGHT 5 +#define PIN_SERVO 6 +#define PIN_POTENTIOMETER A0 + +#define TURN_SIGNAL_DELAY 500 + +QueueHandle_t queue; +LiquidCrystal lcd(12, 11, 10, 9, 8, 7); +Servo servo; + +void +setup() +{ + Serial.begin(9600); + pinMode(PIN_BTN_LEFT, INPUT_PULLUP); + pinMode(PIN_BTN_RIGHT, INPUT_PULLUP); + pinMode(PIN_BTN_ALARM, INPUT_PULLUP); + lcd.begin(16, 2); + servo.attach(PIN_SERVO); + queue = xQueueCreate(5, sizeof(int)); + if (queue == NULL) { + Serial.println("queue cannot be created"); + /* hang */ + for (;;); + } + xTaskCreate(turn_signal_left, "Left turn singal", 128, NULL, 1, NULL); + xTaskCreate(turn_signal_right, "Right turn singal", 128, NULL, 1, NULL); + xTaskCreate(alarm, "Alarm", 128, NULL, 1, NULL); + xTaskCreate(rpm_count, "RPM count", 128, NULL, 1, NULL); + xTaskCreate(tachometer, "Tachometer", 128, NULL, 1, NULL); + vTaskStartScheduler(); +} + +void +loop() +{ +} + +int +btn_pressed(int btn_pin) +{ + return (digitalRead(btn_pin) == LOW); +} + +void +turn_signal_left(void *pv_params) +{ + if (!btn_pressed(PIN_BTN_LEFT)) + return; + pinMode(PIN_TURN_LEFT, OUTPUT); + for (;;) { + Serial.println("Left turn"); + digitalWrite(PIN_TURN_LEFT, HIGH); + vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS); + digitalWrite(PIN_TURN_LEFT, LOW); + vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS); + } +} + +void +turn_signal_right(void *pv_params) +{ + if (!btn_pressed(PIN_BTN_RIGHT)) + return; + pinMode(PIN_TURN_RIGHT, OUTPUT); + for (;;) { + Serial.println("Right turn"); + digitalWrite(PIN_TURN_RIGHT, HIGH); + vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS); + digitalWrite(PIN_TURN_RIGHT, LOW); + vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS); + } +} + +void +alarm(void *pv_params) +{ + if (!btn_pressed(PIN_BTN_ALARM)) + return; + pinMode(PIN_TURN_LEFT, OUTPUT); + pinMode(PIN_TURN_RIGHT, OUTPUT); + for (;;) { + Serial.println("Alarm"); + digitalWrite(PIN_TURN_LEFT, HIGH); + digitalWrite(PIN_TURN_RIGHT, HIGH); + vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS); + digitalWrite(PIN_TURN_LEFT, LOW); + digitalWrite(PIN_TURN_RIGHT, LOW); + vTaskDelay(TURN_SIGNAL_DELAY / portTICK_PERIOD_MS); + } +} + +void +rpm_count(void *pv_params) +{ + int rpm, servo_angle; + + for (;;) { + servo_angle = map(analogRead(PIN_POTENTIOMETER), 0, 1023, 0, 180); + servo.write(servo_angle); + rpm = map(servo_angle, 0, 180, 0, 8000); + Serial.print("RPM: "); + Serial.println(rpm); + xQueueSend(queue, &rpm, portMAX_DELAY); + vTaskDelay(1000 / portTICK_PERIOD_MS); + } +} + +void +tachometer(void *pv_params) +{ + int rpm; + + for (;;) { + if (xQueueReceive(queue, &rpm, portMAX_DELAY) != pdPASS) + return; + Serial.print("Tachometer: "); + Serial.println(rpm); + lcd.clear(); + lcd.setCursor(0, 0); + lcd.print("RPM: "); + lcd.setCursor(6, 0); + lcd.print(rpm); + } +} diff --git a/c_microcomputers/ex4/freertos.png b/c_microcomputers/ex4/freertos.png Binary files differ. diff --git a/c_parallel_systems/ex1/Makefile b/c_parallel_systems/ex1/Makefile @@ -0,0 +1,10 @@ +TARG= ex1 + +all: + cc ${TARG}.c -fopenmp -lomp -o ${TARG} + +clean: + rm ${TARG} + +run: + ./${TARG} diff --git a/c_parallel_systems/ex1/ex1.c b/c_parallel_systems/ex1/ex1.c @@ -0,0 +1,143 @@ +#include <err.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include <omp.h> + +#define abs(x) ((x) < 0 ? -(x) : (x)) + +static int +safe_input(const char *fmt, ...) +{ + va_list args; + char buf[48]; + int n, rc; + + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + do { + printf("\r%s", buf); + rc = scanf("%d", &n); + (void)getchar(); + } while (rc != 1); + + return (n); +} + +static void * +emalloc(size_t nb) +{ + void *p; + + if ((p = malloc(nb)) == NULL) + err(1, "malloc"); + + return (p); +} + +static int +strictly_diagonal_dominant(int **a, int n) +{ + int i, j, sum, flag = 1; + +#pragma omp parallel for + for (i = 0; i < n; i++) { + for (j = 0; j < n; j++) { + if (i == j) + continue; + sum += abs(a[i][j]); + } + if (abs(a[i][i]) <= sum) + flag = 0; + } + + return (flag); +} + +static int +diagonal_max(int **a, int n) +{ + int i, max; + + max = a[0][0]; +#pragma omp parallel for reduction(max : max) + for (i = 0; i < n; i++) { + if (abs(a[i][i]) > max) + max = a[i][i]; + } + + return (max); +} + +static int ** +new_array(int **a, int m, int n) +{ + int **b, i, j; + + b = emalloc(n * sizeof(int *)); +#pragma omp parallel for + for (i = 0; i < n; i++) { + b[i] = emalloc(n * sizeof(int)); + for (j = 0; j < n; j++) + b[i][j] = m - a[i][j]; + } + + return (b); +} + +static int +min_reduction(int **b, int n) +{ + int i, min; + + min = b[0][0]; +#pragma omp parallel for reduction(min : min) + for (i = 0; i < n; i++) { + if (b[i][i] < min) + min = b[i][i]; + } + + return (min); +} + +/* TODO: min_no_reduction() */ + +int +main(int argc, char *argv[]) +{ + int **a, **b; + int i, j, m, min, n, ntd; + + ntd = safe_input("threads: ", 0); + omp_set_num_threads(ntd); + + n = safe_input("n: ", 0); + a = emalloc(n * sizeof(int *)); + for (i = 0; i < n; i++) { + a[i] = emalloc(n * sizeof(int)); + for (j = 0; j < n; j++) + a[i][j] = safe_input("a[%d][%d]: ", i, j); + } + + if (strictly_diagonal_dominant(a, n)) { + m = diagonal_max(a, n); + printf("diagonal_max: %d\n", m); + + b = new_array(a, m, n); + + min = min_reduction(b, n); + printf("min_reduction: %d\n", min); + + for (i = 0; i < n; i++) + for (j = 0; j < n; j++) + printf("b[%d][%d]: %d\n", i, j, b[i][j]); + free(b); + } else + printf("not strictly diagonal dominant\n"); + + free(a); + + return (0); +}