-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenTask.java
More file actions
169 lines (148 loc) · 4.99 KB
/
ScreenTask.java
File metadata and controls
169 lines (148 loc) · 4.99 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package HealthMonitor;
//Firmata imports
import org.firmata4j.Pin;
import org.firmata4j.ssd1306.SSD1306;
//Exception import
import java.io.IOException;
//LocalTime import
import java.time.LocalTime;
//DateTimerFormat import
import java.time.format.DateTimeFormatter;
//ArrayList import
import java.util.ArrayList;
//TimerTask import
import java.util.TimerTask;
public class ScreenTask extends TimerTask //This class extends the TimerTask class
{
//Private variables to be used in the constructor and methods
private final SSD1306 display;
private final Pin Light;
private final Pin Sound;
private final Pin LED;
private final Pin Potentio;
private LocalTime CurrentTime;
private ArrayList<Integer> LightVal;
private ArrayList<Integer> SoundVal;
public ScreenTask(SSD1306 display, Pin Light, Pin Sound, Pin LED, Pin Potentio, ArrayList<Integer> LightVal,ArrayList<Integer> SoundVal) //Constructor
{
//this statements
this.display = display;
this.Light = Light;
this.Sound = Sound;
this.LED = LED;
this.Potentio = Potentio;
this.LightVal = LightVal;
this.SoundVal = SoundVal;
}
//ArrayList of light values return method
public ArrayList<Integer> getLightValues()
{
return LightVal;
}
//ArrayList of sound values return method
public ArrayList<Integer> getSoundValues()
{
return SoundVal;
}
@Override
public void run() //run method
{
//Current time code
CurrentTime = LocalTime.now();
//Proper formatting of the time to mimic a digital clock
String time = CurrentTime.format(DateTimeFormatter.ofPattern("hh:mm:ss\na"));
//Light and Sound sensor value check
long Lval = Light.getValue();
long Sval = Sound.getValue();
//Adding both sensor values into their respective ArrayList
LightVal.add(Integer.valueOf((int)Lval));
SoundVal.add(Integer.valueOf((int)Sval));
//Conversion of long to string to be displayed
String LightValue = String.valueOf(Lval);
String SoundValue = String.valueOf(Sval);
//Potentiometer code to determine if the screen will be turned on or off
if (Potentio.getValue() == 0)
{
display.getCanvas().clear();
display.display();
}
display.getCanvas().clear();
//Time is displayed with a larger font at 2
display.getCanvas().setTextsize(2);
display.getCanvas().drawString(0,0,time);
//Light and Sound intensity values are displayed with a smaller font of 1
display.getCanvas().setTextsize(1);
display.getCanvas().drawString(0,35,"Light Intensity: " + LightValue);
display.getCanvas().drawString(0,45,"Sound Intensity: " + SoundValue);
//If and else if statements for the danger levels
//checks if the volume is too loud
if (Sval > 740)
{
display.getCanvas().clear();
display.getCanvas().drawString(0,0,"Turn down your volume\nit's too loud!");
display.display();
try
{
//LED blinks when warnings are initiated
LED.setValue(1);
Thread.sleep(1000);
LED.setValue(0);
}
//exceptions
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
//Light and Sound values are connected by if and else if statements so that they do not display on top of each other
//checks if the brightness is too bright
else if (Lval > 700)
{
display.getCanvas().clear();
display.getCanvas().drawString(0,0,"Turn down your \nbrightness it's too\nbright!");
display.display();
try
{
LED.setValue(1);
Thread.sleep(1000);
LED.setValue(0);
}
//exceptions
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
//To check if lights are turned off
else if (Lval < 10)
{
display.getCanvas().clear();
display.getCanvas().drawString(0,0,"Turn on your lights\nits too dark!");
display.display();
try
{
LED.setValue(1);
Thread.sleep(1000);
LED.setValue(0);
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
//display is updated
display.display();
}
}