-
Notifications
You must be signed in to change notification settings - Fork 1
/
single_page.html
227 lines (183 loc) · 5.52 KB
/
single_page.html
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
<!DOCTYPE html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Simple Web Zephyr</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #0d1117;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: #c9d1d9;
}
h1 {
font-size: 2.5em;
margin-bottom: 0.5em;
color: #58a6ff;
}
h2 {
font-size: 1.5em;
margin-bottom: 1em;
color: #8b949e;
}
.align-horizontal {
display: flex;
align-items: center;
justify-content: center;
gap: 15px;
margin-top: 20px;
}
input[type="color"] {
width: 50px;
height: 50px;
border: none;
cursor: pointer;
background-color: #161b22;
border-radius: 5px;
}
button {
background-color: #238636;
color: white;
border: none;
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
border-radius: 5px;
margin-bottom: 0;
}
button:hover {
background-color: #2ea043;
}
div.container {
text-align: center;
background-color: #161b22;
padding: 2em;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
@media only screen and (max-width: 768px) {
body {
justify-content: flex-start;
padding: 2em;
}
div.container {
width: 80%;
margin: 0 auto;
}
h1 {
font-size: 2em;
}
h2 {
font-size: 1.2em;
}
button {
font-size: 0.8em;
padding: 8px 16px;
margin-bottom: 0.5em;
}
input[type="color"] {
width: 40px;
height: 40px;
}
}
@media only screen and (max-width: 480px) {
body {
padding: 1em;
}
div.container {
width: 90%;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1em;
}
button {
font-size: 0.7em;
padding: 6px 12px;
margin-bottom: 0.3em;
}
input[type="color"] {
width: 30px;
height: 30px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Simple Web Zephyr</h1>
<h2 id="status">Not Connected</h2>
<div class="align-horizontal">
<button id="connect">CONNECT</button>
<input id="colorpicker" type="color">
</div>
</div>
</body>
<script>
const SIMPLE_IO_SERVICE = '13e779a0-bb72-43a4-a748-9781b918258c';
const SIMPLE_IO_RGB_CHAR = '4332aca6-6d71-4173-9945-6653b6c684a0';
const SIMPLE_IO_ID_CHAR = 'b749d964-4efb-408a-82ad-7495e8af8d6d';
const SIMPLE_IO_BUTTON_CHAR = '030de9cf-ce4b-44d0-8aa2-1db9185dc069';
let rgbCharacteristic = null;
let statusElement = null;
const setRGB = (r, g, b) => {
if (rgbCharacteristic) {
rgbCharacteristic.writeValueWithoutResponse(new Uint8Array([r, g, b]));
}
}
const hexToRGB = hex => hex.match(/[A-Za-z0-9]{2}/g).map(v => parseInt(v, 16));
const handleColorPicker = (evt) => {
const hexcolor = evt.target.value;
setRGB(...hexToRGB(hexcolor));
}
const startButtonNotifications = async (service) => {
const characteristic = await service.getCharacteristic(SIMPLE_IO_BUTTON_CHAR);
characteristic.addEventListener('characteristicvaluechanged', (evt) => {
const value = evt.target.value.getUint8(0);
console.log(`Button = ${value}`);
statusElement.innerHTML = `Button ${value === 0 ? "released" : "pressed"}`;
});
return characteristic.startNotifications();
}
const fetchRGBCharacteristic = async (service) => {
rgbCharacteristic = await service.getCharacteristic(SIMPLE_IO_RGB_CHAR);
}
const openDevice = async (device) => {
const server = await device.gatt.connect();
try {
const service = await server.getPrimaryService(SIMPLE_IO_SERVICE);
await startButtonNotifications(service);
await fetchRGBCharacteristic(service);
console.log('Connected to device', device);
statusElement.innerHTML = `Connected to ${device.name}`
device.ongattserverdisconnected = _ => {
console.log(`Disconnected ${device.id}`);
statusElement.innerHTML = "Not Connected";
};
} catch (err) {
console.warn(err);
}
}
const scan = async () => {
try {
const device = await navigator.bluetooth.requestDevice({
filters: [{ services: [SIMPLE_IO_SERVICE] }]
});
await openDevice(device);
} catch (err) {
// ignore if we didn't get a device
}
}
const init = () => {
document.querySelector('#connect').addEventListener('click', scan);
document.querySelector('#colorpicker').addEventListener('input', handleColorPicker);
statusElement = document.querySelector('#status');
}
window.addEventListener('load', init);
</script>