-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathApp.js
128 lines (118 loc) · 3.82 KB
/
App.js
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
import React from 'react';
import {
View,
Text,
FlatList,
Button,
StyleSheet,
TouchableWithoutFeedback,
} from 'react-native';
import useStream from './useStream';
import adapterStateStream from './adapterStateStream';
import deviceStream from './deviceStream';
import selectedDeviceStream from './selectedDeviceStream';
import characteristicsStream from './characteristicsStream';
import xs from 'xstream';
import buttonStream from './buttonStream';
import commandStream from './commandStream';
import collisionStream from './collisionStream';
import {configureCollisions} from './spheroCommands';
import {encodeFromUint8Array} from './base64';
const wakeUpStream = characteristicsStream.filter(
({uuid}) => uuid === '22bb746f-2bbf-7554-2d6f-726568705327',
);
const antiDosStream = characteristicsStream.filter(
({uuid}) => uuid === '22bb746f-2bbd-7554-2d6f-726568705327',
);
const txPowerStream = characteristicsStream.filter(
({uuid}) => uuid === '22bb746f-2bb2-7554-2d6f-726568705327',
);
const sendStream = characteristicsStream.filter(
({uuid}) => uuid === '22bb746f-2ba1-7554-2d6f-726568705327',
);
xs.combine(wakeUpStream, antiDosStream, txPowerStream, sendStream).addListener({
next: async ([wakeUp, antiDos, txPower, send]) => {
await antiDos.writeWithResponse('MDExaTM=');
await txPower.writeWithResponse('AAc=');
await wakeUp.writeWithResponse('AQ==');
const command = configureCollisions({
meth: 0x01,
xt: 0x20,
xs: 0x20,
yt: 0x20,
ys: 0x20,
dead: 0x21,
});
await send
.writeWithResponse(encodeFromUint8Array(command))
.catch(console.error);
},
});
xs.combine(sendStream, commandStream).addListener({
next: ([send, command]) =>
send.writeWithResponse(command).catch(console.error),
});
collisionStream.addListener({next: console.log, error: console.error});
const App = () => {
const adapterState = useStream(adapterStateStream, '');
const devices = useStream(deviceStream, {});
const collisions = useStream(collisionStream, false);
return (
<View>
<Text>Adapter State: {adapterState}</Text>
<FlatList
data={Object.values(devices).slice(0, 3)}
renderItem={({item}) => (
<Button
title={item.name || item.id}
onPress={() => selectedDeviceStream.shamefullySendNext(item)}
/>
)}
keyExtractor={device => device.id}
/>
<TouchableWithoutFeedback
onPressIn={() => buttonStream.shamefullySendNext('UP')}
onPressOut={() => buttonStream.shamefullySendNext('STOP')}>
<Text style={styles.button}>↑</Text>
</TouchableWithoutFeedback>
<View style={styles.row}>
<TouchableWithoutFeedback
onPressIn={() => buttonStream.shamefullySendNext('LEFT')}
onPressOut={() => buttonStream.shamefullySendNext('STOP')}>
<Text style={styles.buttonHalf}>←</Text>
</TouchableWithoutFeedback>
<TouchableWithoutFeedback
onPressIn={() => buttonStream.shamefullySendNext('RIGHT')}
onPressOut={() => buttonStream.shamefullySendNext('STOP')}>
<Text style={styles.buttonHalf}>→</Text>
</TouchableWithoutFeedback>
</View>
<TouchableWithoutFeedback
onPressIn={() => buttonStream.shamefullySendNext('DOWN')}
onPressOut={() => buttonStream.shamefullySendNext('STOP')}>
<Text style={styles.button}>↓</Text>
</TouchableWithoutFeedback>
{collisions && <Text style={styles.collision}>💥</Text>}
</View>
);
};
const styles = StyleSheet.create({
collision: {
fontSize: 100,
textAlign: 'center',
},
button: {
fontSize: 70,
width: '100%',
textAlign: 'center',
},
buttonHalf: {
fontSize: 70,
width: '50%',
textAlign: 'center',
},
row: {
flexDirection: 'row',
},
});
export default App;