-
Notifications
You must be signed in to change notification settings - Fork 0
/
a3p1.c
113 lines (102 loc) · 3.24 KB
/
a3p1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Part of the code below has been developed by Johan Nordlander and Fredrik Bengtsson at LTU.
* Part of the code has been also developed, modified and extended to ARMv8 by Wagner de Morais and Hazem Ali.
* Updated by Wagner Morais and Hazem Ali on 20/09/21.
* Updated by Wagner Morais on Sept 22.
*/
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include "tinythreads.h"
#include "rpi3.h"
#include "piface.h"
#include "led.h"
#include "expstruct.h"
#include "rpi-armtimer.h"
#include "rpi-systimer.h"
#include "rpi-interrupts.h"
/** @brief Checks whether the input parameter is divisible by itself and 1, i.e, if the input parameter is prime.
* @param int i Is the input parameter to be checked whether it is prime or not
* @return int Returns 1 if the input parameter is prime, else it returns 0.
*
* Pre-condition: Input parameter i mus tbe positive
*
* test-cases:
* is_prime(7) must return 1, i.e., true
* is_prime(9) must return 0, i.e., false
* is_prime(-1) must return 0, i.e., false
*/
int is_prime(int i) {
// To be implemented!!!
if (i <= 1) return 0;
if (i % 2 == 0 && i > 2) return 0; //Check even numbers
for(int j = 3; j < i; j+= 2) //Check uneven numbers
if (i % j == 0)
return 0;
return 1;
}
/** @brief For all positive integers, displays prime numbers in a given segment
* @param int seg Is the segment, i.e., 0: top left, 1:top right, 2: bottom left and 3: bottom right.
*/
void computePrimes(int seg) {
for(int n = 0; ; n++) {
if (is_prime(n)) {
print_at_seg(seg, n);
//PUTTOLDC("T%i: Prime %i\n", seg, n);
RPI_WaitMicroSeconds(500000); //delay of 0.5s added for visualization purposes!!!
yield();
}
}
}
/** @brief For all positive integers, displays the power of a given number
* @param int seg Is the segment, i.e., 0: top left, 1:top right, 2: bottom left and 3: bottom right.
*/
void computePower(int seg) {
for(int n = 0; ; n++) {
print_at_seg(seg, n*n);
//PUTTOLDC("T%i: %i^2=%i\n", seg, n, n*n);
RPI_WaitMicroSeconds(500000); //delay of 0.5s added for visualization purposes!!!
yield();
}
}
/** @brief Loops over the positive integers less than 21,
* calculates the exponential function and displays the integer part in a given segment.
* @param int seg Is the segment, i.e., 0: top left, 1:top right, 2: bottom left and 3: bottom right.
*/
void computeExponential(int seg) {
char str[32];
ExpStruct* value;
int n, i;
while (1) {
for(i = 0; i < 21; i++){
value = iexp(i);
if(seg % 2 == 0)
print_at_seg(seg, value->expInt);
else
print_at_seg(seg, value->expFraction);
free(value);
}
}
}
/** @brief Toggle the state of the LED.
* @param int seg Is the segment, i.e., 0: top left, 1:top right, 2: bottom left and 3: bottom right.
*/
void toggle_led(int seg) {
while (1) {
led_toggle();
RPI_WaitMicroSeconds(50000);
}
}
int main() {
led_init();
piface_init();
piface_clear();
piface_puts("DT8025 - A3P1");
RPI_WaitMicroSeconds(2000000);
piface_clear();
spawn(computePower, 0);
computePrimes(1);
}