uni

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

commit 3143d9955c248ea40b5a676e09ac453136e1cbde
parent ecf51563722f52a5c010c9890c86f88d26a55126
Author: Christos Margiolis <christos@margiolis.net>
Date:   Tue, 10 Jan 2023 03:03:01 +0200

new stuff

Diffstat:
Ac_microcomputers/ex7/doc.pdf | 0
Ac_microcomputers/ex7/doc.tex | 44++++++++++++++++++++++++++++++++++++++++++++
Ac_microcomputers/ex7/door.ino | 115+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ac_microcomputers/ex7/door.png | 0
Ac_parallel_systems/ex2/Makefile | 6++++++
Ac_parallel_systems/ex2/ex2a.c | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 248 insertions(+), 0 deletions(-)

diff --git a/c_microcomputers/ex7/doc.pdf b/c_microcomputers/ex7/doc.pdf Binary files differ. diff --git a/c_microcomputers/ex7/doc.tex b/c_microcomputers/ex7/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{Μικροϋπολογιστές: Εργαστηριακή άσκηση 7} +\author{Χρήστος Μαργιώλης -- 19390133} +\date{Ιανουάριος 2023} + +\begin{document} + +\begin{titlepage} + \maketitle +\end{titlepage} + +\section{Κύκλωμα} + +\includegraphics[width=\linewidth]{door.png} +\pagebreak + +\section{Κώδικας} + +\lstinputlisting[language=C]{door.ino} + +\end{document} diff --git a/c_microcomputers/ex7/door.ino b/c_microcomputers/ex7/door.ino @@ -0,0 +1,115 @@ +#include <Encoder.h> + +#define PIN_ENC_CHAN1 2 +#define PIN_ENC_CHAN2 3 +#define PIN_L293D_IN 4 +#define PIN_L293D_OUT 5 +#define PIN_ULTRASONIC_ECHO 6 +#define PIN_ULTRASONIC_TRIGGER 7 +#define PIN_DCMOTOR 9 +#define PIN_PIR 10 +#define PIN_LED 13 +#define PIN_TEMP A0 + +static Encoder enc(PIN_ENC_CHAN1, PIN_ENC_CHAN2); + +void +setup() +{ + pinMode(PIN_ENC_CHAN1, INPUT); + pinMode(PIN_ENC_CHAN2, INPUT); + pinMode(PIN_L293D_IN, OUTPUT); + pinMode(PIN_L293D_OUT, OUTPUT); + pinMode(PIN_DCMOTOR, OUTPUT); + pinMode(PIN_PIR, INPUT); + pinMode(PIN_LED, OUTPUT); + pinMode(PIN_ULTRASONIC_ECHO, INPUT); + pinMode(PIN_ULTRASONIC_TRIGGER, OUTPUT); + Serial.begin(9600); +} + +void +loop() +{ + if (measure_distance() <= 40) + open_door(); + if (measure_temp() > 20) + fan(); + if (digitalRead(PIN_PIR) == HIGH) + digitalWrite(PIN_LED, HIGH); + else + digitalWrite(PIN_LED, LOW); + + delay(10); +} + +int +measure_distance() +{ + long duration; + int distance; + + digitalWrite(PIN_ULTRASONIC_TRIGGER, LOW); + delayMicroseconds(2); + digitalWrite(PIN_ULTRASONIC_TRIGGER, HIGH); + delayMicroseconds(10); + digitalWrite(PIN_ULTRASONIC_TRIGGER, LOW); + + duration = pulseIn(PIN_ULTRASONIC_ECHO, HIGH); + distance = (float)duration * 0.344 / 20; + + Serial.print("Distance: "); + Serial.print(distance); + Serial.println(" cm"); + + delay(100); + + return (distance); +} + +void +open_door() +{ + long pos; + int rot; + + analogWrite(PIN_L293D_IN, 30); + analogWrite(PIN_L293D_OUT, 0); + pos = enc.read() / 10; + rot = abs(pos) / 10; + + Serial.print("Encoder position: "); + Serial.println(pos); + Serial.print("Encoder rotation: "); + Serial.println(rot); +} + +float +measure_temp() +{ + float temp; + int adc; + + adc = analogRead(PIN_TEMP); + temp = (float)adc * (5000 / 1024.0); + temp = (temp - 500) / 10; + + Serial.print("Temperatue: "); + Serial.print(temp); + Serial.println(" C"); + + delay(100); + + return (temp); +} + +void +fan() +{ + int v; + + for (v = 0; v <= 255; v += 5) + analogWrite(PIN_DCMOTOR, v); + for (v = 255; v >= 0; v -= 5) + analogWrite(PIN_DCMOTOR, v); +} diff --git a/c_microcomputers/ex7/door.png b/c_microcomputers/ex7/door.png Binary files differ. diff --git a/c_parallel_systems/ex2/Makefile b/c_parallel_systems/ex2/Makefile @@ -0,0 +1,6 @@ +all: + cc ex2a.c -fopenmp -lomp -o ex2a + cc ex2b.c -fopenmp -lomp -o ex2b + +clean: + rm -f ex2a ex2b *.core diff --git a/c_parallel_systems/ex2/ex2a.c b/c_parallel_systems/ex2/ex2a.c @@ -0,0 +1,83 @@ +#include <err.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> + +#include <omp.h> + +static int safe_input(const char *, ...); +static void pretty_print(int *, int, const char *); + +static int +safe_input(const char *fmt, ...) +{ + va_list args; + char buf[48]; + int n, rc; + + /* Collect the arguments into a buffer. */ + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + + /* + * The following loop keeps asking for input as long as the current + * input wasn't correct. In this case "incorrect" input means anything + * other than digits. + */ + do { + printf("\r%s", buf); + rc = scanf("%d", &n); + (void)getchar(); + } while (rc != 1); + + return (n); +} + +/* + * Print the contents of a 2D array like: + * + * array = [x, y, z] + */ +static void +pretty_print(int *arr, int n, const char *name) +{ + int i; + + printf("\n%s = [", name); + for (i = 0; i < n; i++) + printf("%d%s", arr[i], (i == n - 1) ? "" : ", "); + printf("]\n"); +} + +int +main(int argc, char *argv[]) +{ + int *a, i, n, ntd; + double start, end; + + + ntd = safe_input("threads: ", 0); + omp_set_num_threads(ntd); + + n = safe_input("n: ", 0); + if ((a = malloc(n * sizeof(int))) == NULL) + err(1, "malloc"); + for (i = 0; i < n; i++) + a[i] = safe_input("a[%d]: ", i); + + start = omp_get_wtime(); + + /* TODO */ + <++> + + end = omp_get_wtime(); + printf("Total time: %f seconds\n", end - start); + + pretty_print(a, n, "A_unsorted"); + pretty_print(a, n, "A_multisort"); + + free(a); + + return (0); +}