-
Notifications
You must be signed in to change notification settings - Fork 3
/
RNJoystick.js
153 lines (135 loc) · 4.28 KB
/
RNJoystick.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
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
import React, { Component } from 'react';
import {
AppState,
Alert,
Image,
AppRegistry,
StyleSheet,
Text,
View,
PanResponder,
Animated,
Dimensions
} from 'react-native';
import PacketSender from './PacketSender';
const CIRCLE_RADIUS = 36;
const DEG_TO_RAD = 57.29578;
// This funky looking function transforms movements gestures from screen coordinate
// system to the STEMI Hexapod coordinate system.
function calculateAngle(x, y) {
if (y === 0) return 0;
if (y < 0) return Math.atan(x / y) * DEG_TO_RAD * -1;
if (x === 0) return 180;
if (x > 0) return Math.atan(y / x) * DEG_TO_RAD + 90;
if (x < 0) return Math.atan(y / x) * DEG_TO_RAD - 90;
}
const Window = Dimensions.get('window');
const styles = StyleSheet.create({
mainContainer: {
flex: 1
},
dropZone: {
height: 100,
backgroundColor: '#2c3e50'
},
text: {
marginTop: 25,
marginLeft: 5,
marginRight: 5,
textAlign: 'center',
color: '#fff'
},
draggableContainer: {
position: 'absolute',
top: Window.height / 2 - CIRCLE_RADIUS,
left: Window.width / 2 - CIRCLE_RADIUS,
},
circle: {
width: CIRCLE_RADIUS * 2,
height: CIRCLE_RADIUS * 2,
borderRadius: CIRCLE_RADIUS
}
});
export default class RNJoystick extends Component {
constructor(props) {
super(props);
this.handlePanResponderMove = this.handlePanResponderMove.bind(this);
this.handlePanResponderRelease = this.handlePanResponderRelease.bind(this);
this.handleAppStateChange = this.handleAppStateChange.bind(this);
this.onCommunicationError = this.onCommunicationError.bind(this);
this.packetSender = new PacketSender('80', '192.168.4.1', this.onCommunicationError);
this.state = {
showDraggable: true,
dropZoneValues: null,
pan: new Animated.ValueXY(),
appState: AppState.currentState,
};
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderMove: this.handlePanResponderMove,
onPanResponderRelease: this.handlePanResponderRelease,
});
}
getPower(x, y) {
absDistFromCentre = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
relDistFromCentre = Math.trunc(absDistFromCentre / (Window.width / 2) * 100);
return relDistFromCentre > 100 ? 100 : relDistFromCentre;
}
getAngle(x, y) {
return Math.trunc(calculateAngle(x, y));
}
handlePanResponderMove(evt, gestureState) {
const dx = gestureState.dx;
const dy = gestureState.dy;
this.packetSender.updatePacket({ power: this.getPower(dx, dy), angle: this.getAngle(dx, dy) });
console.debug('x: ' + dx + ' y: ' + dy + ' power: ' + this.getPower(dx, dy) + ' angle: ' + this.getAngle(dx, dy));
Animated.event([null, {
dx: this.state.pan.x,
dy: this.state.pan.y
}])(evt, gestureState);
}
handlePanResponderRelease(evt, gestureState) {
this.packetSender.updatePacket({});
Animated.spring(
this.state.pan,
{ toValue: { x: 0, y: 0 } }
).start();
}
onCommunicationError() {
Alert.alert(
'Connection lost.',
('Mobile phone lost connection with STEMI Hexapod. Please go to the WiFi settings' +
' on your phone and connect to the WiFi network named STEMI-XXXXXXX, where X is a' +
' number between 0-9. Password for the network is "12345678".'),
[
{ text: 'Got it', onPress: () => console.log('OK Pressed') },
],
{ cancelable: true }
);
}
handleAppStateChange(nextAppState) {
if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
this.packetSender.connect();
} else if (nextAppState.match(/inactive|background/) && this.state.appState === 'active') {
this.packetSender.disconnect();
}
this.setState({ appState: nextAppState });
}
componentWillMount() {
AppState.addEventListener('change', this.handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this.handleAppStateChange);
}
render() {
return (
<View style={styles.draggableContainer}>
<Animated.Image
{...this.panResponder.panHandlers}
style={[{ transform: this.state.pan.getTranslateTransform() }, styles.circle]}
source={require('./assets/joystick_button.png')}>
</Animated.Image>
</View>
);
}
}