-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
390 lines (375 loc) · 7.46 KB
/
App.jsx
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import {
useEffect,
useState
} from "react";
import {
StyleSheet,
Platform,
PermissionsAndroid,
Alert,
BackHandler,
Text,
View,
Pressable,
ActivityIndicator
} from "react-native";
import BluetoothSerial from "react-native-bluetooth-serial";
const deviceMacAddress = "00:22:09:02:68:3E";
const naturalNotes = [
{
"code": "a",
"letter": "C"
},
{
"code": "c",
"letter": "D"
},
{
"code": "e",
"letter": "E"
},
{
"code": "f",
"letter": "F"
},
{
"code": "h",
"letter": "G"
},
{
"code": "j",
"letter": "A"
},
{
"code": "l",
"letter": "B"
}
];
const accidentalNotes = [
{
"code": "b",
"letter": "C#"
},
{
"code": "d",
"letter": "D#"
},
{
"code": "g",
"letter": "F#"
},
{
"code": "i",
"letter": "G#"
},
{
"code": "k",
"letter": "A#"
},
];
const writeCode = async (code) => {
try {
await BluetoothSerial.write(code);
} catch (error) {
console.warn(error);
}
}
export default function App() {
const [isLoading, setLoading] = useState(false);
const [isPermissionsGranted, setPermissionsGranted] = useState(false);
const [isBluetoothEnabled, setBluetoothEnabled] = useState(false);
const [connectedDevice, setConnectedDevice] = useState(null);
const checkPermissions = async () => {
try {
if (Platform.OS == "android" && Platform.Version <= 30) {
const locationGranted = await PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
);
if (!locationGranted) {
const locationResult = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
);
if (locationResult === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Uygulama icin gerekli izinler verildi.");
setPermissionsGranted(true);
} else {
Alert.alert(
"Gerekli izinler verilmedi!",
"Uygulamanin dogru calismasi icin lutfen gerekli izinleri verin.",
[
{
text: "Tamam",
onPress: BackHandler.exitApp
},
]
);
}
}
}
} catch (error) {
console.warn(error);
}
}
const checkBluetoothEnabled = async () => {
try {
setLoading(true);
const initialBluetoothState = await BluetoothSerial.isEnabled();
if (!initialBluetoothState) {
await BluetoothSerial.requestEnable();
setBluetoothEnabled(true);
setLoading(false);
} else {
setBluetoothEnabled(true);
setLoading(false);
}
} catch (error) {
setBluetoothEnabled(false);
setLoading(false);
Alert.alert(
"Bluetooth kapali!",
"Uygulamanin dogru calismasi icin Bluetooth acik olmalidir.",
[
{
text: "Tamam",
onPress: BackHandler.exitApp
}
]
);
console.warn(error);
}
}
const connectToDevice = async (device) => {
try {
setLoading(true);
await BluetoothSerial.connect(device.id);
setConnectedDevice(device);
setLoading(false);
Alert.alert(
"HC-06 modul ile baglanti kuruldu!",
"",
[
{
text: "Tamam",
}
]
);
} catch (error) {
setConnectedDevice(null);
setLoading(false);
Alert.alert(
"HC-06 modul ile baglanti kurulamadi!",
"Eslesmis modul ile baglanti kurulamadi. Lutfen modulun calistigindan emin olun.",
[
{
text: "Tamam",
onPress: BackHandler.exitApp
}
]
);
console.warn(error);
}
}
const searchForDevice = async () => {
try {
if (connectedDevice && connectedDevice.id == deviceMacAddress) {
console.log("Modul baglantisi mevcut.");
return;
}
if (isBluetoothEnabled) {
setLoading(true);
const pairedDevices = await BluetoothSerial.list();
for (const device of pairedDevices) {
if (device.id === deviceMacAddress) {
setLoading(false);
Alert.alert(
"Eslesmis HC-06 modul bulundu!",
"Telefon ile eslesmis HC-06 modul bulundu. Baglanti kurulsun mu?",
[
{
text: "Hayir",
onPress: BackHandler.exitApp
},
{
text: "Evet",
onPress: () => connectToDevice(device)
}
]
);
} else {
setLoading(false);
Alert.alert(
"Eslesmis HC-06 modul bulunamadi!",
"Lutfen modulun telefon ile eslendiginden emin olun ve tekrar deneyin.",
[
{
text: "Tamam",
onPress: BackHandler.exitApp
}
]
);
}
}
}
} catch (error) {
console.warn(error);
}
}
useEffect(() => {
checkPermissions();
checkBluetoothEnabled();
searchForDevice();
}, [isPermissionsGranted, isBluetoothEnabled]);
return (
<View style={styles.container}>
{isLoading ? <Spinner /> : null}
<TopBar />
<PianoKeyboard />
</View>
);
}
function Spinner() {
return (
<View style={styles.spinnerContainer}>
<ActivityIndicator size="large" style={styles.spinner} />
</View>
);
}
function TopBar() {
return (
<View style={styles.topBar}>
<Text style={styles.topBarText}>Arduino Mini Piano</Text>
</View>
);
}
function PianoKeyboard() {
const accidentalKeys = accidentalNotes.map((note) =>
<PianoKeyAccidental
letter={note.letter}
handler={() => writeCode(note.code)}
/>
);
const naturalKeys = [];
let accidentalIndex = 0;
for (const note of naturalNotes) {
if (note.letter != "E" && note.letter != "B") {
naturalKeys.push(
<PianoKeyNatural
letter={note.letter}
handler={() => writeCode(note.code)}
children={accidentalKeys[accidentalIndex]}
/>
);
accidentalIndex++;
} else {
naturalKeys.push(
<PianoKeyNatural
letter={note.letter}
handler={() => writeCode(note.code)}
/>
);
}
}
return (
<View style={styles.keyboard}>
{naturalKeys}
</View>
);
}
function PianoKeyNatural({ letter, handler, children }) {
return (
<View style={styles.keyNatural}>
<Pressable onPress={handler}>
<Text style={styles.keyNaturalText}>{letter}</Text>
</Pressable>
{children}
</View>
);
}
function PianoKeyAccidental({ letter, handler }) {
return (
<Pressable style={styles.keyAccidental} onPress={handler}>
<Text style={styles.keyAccidentalText}>{letter}</Text>
</Pressable>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: "row-reverse",
zIndex: 0,
},
spinnerContainer: {
alignItems: "center",
justifyContent: "center",
position: "absolute",
zIndex: 1,
top: 0,
right: 0,
bottom: 0,
left: 0,
},
spinner: {
height: "10%",
width: "20%",
backgroundColor: "white",
elevation: 5,
},
topBar: {
flex: 1,
flexGrow: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "black",
borderStyle: "solid",
borderColor: "gray",
borderLeftWidth: 2,
},
topBarText: {
width: 180,
color: "white",
fontSize: 18,
transform: [{rotate: "90deg"}],
},
keyboard: {
flex: 1,
flexGrow: 9,
flexDirection: "column",
justifyContent: "flex-start",
zIndex: 0,
},
keyNatural: {
flex: 1,
justifyContent: "center",
alignItems: "flex-start",
position: "relative",
zIndex: 0,
backgroundColor: "white",
borderStyle: "solid",
borderWidth: 1,
borderColor: "#000000",
},
keyNaturalText: {
marginLeft: 30,
fontSize: 30,
color: "black",
transform: [{rotate: "90deg"}],
},
keyAccidental: {
height: "50%",
width: "65%",
justifyContent: "center",
alignItems: "flex-start",
position: "absolute",
marginTop: 100,
bottom: 0,
right: 0,
zIndex: 1,
backgroundColor: "black",
},
keyAccidentalText: {
marginLeft: 10,
fontSize: 20,
color: "white",
transform: [{rotate: "90deg"}],
}
});