This commit is contained in:
Ren Juan 2021-08-23 06:34:16 +00:00
parent fc00582125
commit 31a55ff5f5
6 changed files with 116 additions and 52 deletions

View File

@ -1,16 +1,18 @@
import 'react-native-gesture-handler'; import 'react-native-gesture-handler';
import { StatusBar } from 'expo-status-bar'; import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { Text } from 'react-native'; import { Alert,Text } from 'react-native';
import * as Location from 'expo-location'; import * as Location from 'expo-location';
import { SafeAreaProvider } from 'react-native-safe-area-context'; import { SafeAreaProvider } from 'react-native-safe-area-context';
import useCachedResources from './hooks/useCachedResources'; import useCachedResources from './hooks/useCachedResources';
import useColorScheme from './hooks/useColorScheme'; import useColorScheme from './hooks/useColorScheme';
import Navigation from './navigation'; import Navigation from './navigation';
import { Trips } from './GT2';
import * as fs from 'fs';
export var debug:boolean = false; export var debug:boolean = false;
export var lastLoc:any;
var expoGeoState:any; var expoGeoState:any;
export default function App() { export default function App() {
@ -31,6 +33,8 @@ export default function App() {
let location = await Location.getCurrentPositionAsync({}); let location = await Location.getCurrentPositionAsync({});
setLocation(location); setLocation(location);
if (!errorMsg && location)
Trips.deltaLoc(JSON.stringify(location));
})(); })();
}, []); }, []);
@ -38,7 +42,6 @@ export default function App() {
if (errorMsg) { if (errorMsg) {
expoGeoState = errorMsg; expoGeoState = errorMsg;
} else if (location) { } else if (location) {
lastLoc = location;
expoGeoState = JSON.stringify(location); expoGeoState = JSON.stringify(location);
} }

View File

@ -1,8 +1,8 @@
/** Class for handling coordinates original by Linus Helgesson */ /** Class for handling coordinates original by Linus Helgesson */
export class Coordinate { export class Coordinate {
mLatitude:number = 0.0; public mLatitude:number = 0.0;
mLongitude:number = 0.0; public mLongitude:number = 0.0;
mResults:any = [0, 0]; mResults:any = [0, 0];
PI_OVER_180:number = 0.017453292519943295769236907684886; PI_OVER_180:number = 0.017453292519943295769236907684886;
EARTH_RADIUS:number = 6371009; EARTH_RADIUS:number = 6371009;

View File

@ -1,14 +1,14 @@
import * as React from 'react'; import * as React from 'react';
import { Coordinate } from "./Coordinate"; import { Coordinate } from "./Coordinate";
import { Text, View } from './components/Themed'; import { Text, View } from './components/Themed';
import { StyleSheet } from 'react-native'; import { Alert, StyleSheet } from 'react-native';
import { useState, useEffect } from 'react'; import { useState, useEffect } from 'react';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
tripText: { tripText: {
marginHorizontal: -10, marginHorizontal: -10,
marginVertical: 60, marginVertical: 60,
textAlign: 'center', textAlign: 'left',
width: "80%", width: "80%",
fontSize: 12, fontSize: 12,
fontWeight: 'bold', fontWeight: 'bold',
@ -16,10 +16,11 @@ const styles = StyleSheet.create({
var lastLoc:any = null; var lastLoc:any = null;
const heartbeat:number = 500; const heartbeat:number = 500;
const displayBeat:number = 3;
class Chronometer { class Chronometer {
public display:string = ""; public display:string = "00.00.00";
clock:number = 0; clock:number = 0;
intervalID:any = null; intervalID:any = null;
@ -38,23 +39,34 @@ class Chronometer {
} }
export class GT2 { export class GT2 {
loc:Coordinate = new Coordinate(0,0); loc:Coordinate = new Coordinate(0,0);
clock:Chronometer = new Chronometer(); clock:Chronometer = new Chronometer();
startPoint:Coordinate = new Coordinate(0,0); startPoint:Coordinate = new Coordinate(0,0);
displayInterval:any = null; displayInterval:number = displayBeat * heartbeat;
public distance:number = 0.0; public distance:number = 0.0;
co2Rate:number = 0.0; co2Rate:number = 0.0;
currTime:number = 0; currTime:number = 0;
lastTime:number = 0; lastTime:number = 0;
public v:number = 0; z:number = 0.0;
public v:number = 0.0;
public inProgress:boolean = false; public inProgress:boolean = false;
public paused:boolean = false; public paused:boolean = false;
public elapsed:number = 0.0; public elapsed:number = 0.0;
segments:number = 1; segments:number = 0;
elevation:number = 0.0;
nTrips:number = 0;
parseGeoExpo(key:string, value:any) {}
public deltaLoc(lastFix:any) {
}
public reset() { public reset() {
@ -75,39 +87,76 @@ export class GT2 {
this.clock.stop(); this.clock.stop();
this.inProgress = false; this.inProgress = false;
this.nTrips++;
} }
public pause() { public pause() {
if (!this.paused) {
this.clock.stop(); this.clock.stop();
this.paused = true; this.paused = true;
} else {
}
public resume() {
this.clock.start(); this.clock.start();
this.paused = false; this.paused = false;
} }
}
public start() { public start() {
this.reset();
this.clock.start(); this.clock.start();
this.inProgress = true; this.inProgress = true;
} }
public getTripPanel() : string {
public tripDisplay() { if (this.inProgress)
return (
'Elapsed - ' + this.clock.display + '\n' +
'Geo: ' + 'lat: ' + this.loc.mLatitude + ' long: ' + this.loc.mLatitude + '\n' +
'Vector: ' + 'distance: ' + this.distance + ' velocity: ' + this.v + '\n' +
'Altitude: ' + this.elevation + '\n');
else return("No trip in progress. " + this.nTrips + " trip(s) completed");
}
}
export class TripDisplay extends React.Component {
constructor(props:any) {
super(props);
this.state = { seconds: 0 };
}
interval:any;
tick() {
this.setState(state => ({
seconds: 1
}));
}
componentDidMount() {
this.interval = setInterval(() => this.tick(), Trips.displayInterval);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return( return(
<View> <View>
<Text style={styles.tripText}> <Text style={styles.tripText}>
{' No trip started yet.\n'} {Trips.getTripPanel() }
</Text> </Text>
</View> ); </View> );
} }
} }
export var ThisTrip:GT2 = new GT2(); export var Trips:GT2 = new GT2();

View File

@ -13,7 +13,7 @@ export default function ScreenInfo() {
style={styles.settingsText} style={styles.settingsText}
lightColor="rgba(0,0,0,0.8)" lightColor="rgba(0,0,0,0.8)"
darkColor="rgba(255,255,255,0.8)"> darkColor="rgba(255,255,255,0.8)">
Switch dark km/ light mi, select fuel used, or specify CO2 g per distance {'Switch dark: km, light: mi\nset fuel, or CO2 g per km'}
</Text> </Text>
</View> </View>
); );
@ -25,7 +25,7 @@ export function ScreenInfo2() {
style={styles.settingsText} style={styles.settingsText}
lightColor="rgba(0,0,0,0.8)" lightColor="rgba(0,0,0,0.8)"
darkColor="rgba(255,255,255,0.8)"> darkColor="rgba(255,255,255,0.8)">
Use Pause if fuel burning portion of trip interrupted Pause trip if fuel burning interrupted.
</Text> </Text>
</View> </View>
); );

View File

@ -1,8 +1,9 @@
import * as React from 'react'; import * as React from 'react';
import { Button, StyleSheet, Alert } from 'react-native'; import { useState } from 'react';
import { Alert, Button, StyleSheet } from 'react-native';
import { Text, View } from '../components/Themed'; import { Text, View } from '../components/Themed';
import { ScreenInfo2 } from '../components/ScreenInfo'; import { ScreenInfo2 } from '../components/ScreenInfo';
import { ThisTrip } from '../GT2'; import { TripDisplay, Trips } from '../GT2';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
container: { container: {
@ -29,27 +30,36 @@ const styles = StyleSheet.create({
function startTrip() { function startTrip() {
ThisTrip.start(); Trips.start();
Alert.alert('Trip Started'); Alert.alert('Trip Started');
} }
function pauseTrip() { function pauseTrip() {
ThisTrip.pause(); if (!Trips.paused) {
Alert.alert('Trip Paused'); Alert.alert('Trip Paused');
Trips.pause();
}
else {
Alert.alert('Trip Resumed');
Trips.pause();
}
} }
function endTrip() { function endTrip() {
ThisTrip.end(); Trips.end();
Alert.alert('Trip Ended'); Alert.alert('Trip Ended');
} }
export default function TripScreen() { export default function TripScreen() {
const [sButtonText, setSButtonText] = useState("Start");
const [pButtonText, setPButtonText] = useState("Pause");
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Text style={styles.title}>Trip Control</Text> <Text style={styles.title}>Trip Control</Text>
@ -57,19 +67,21 @@ export default function TripScreen() {
<ScreenInfo2 /> <ScreenInfo2 />
<View style={styles.controls} > <View style={styles.controls} >
<Button <Button
title="Start" title={sButtonText}
onPress={() => startTrip() } onPress={() => {
if (!Trips.inProgress) {setSButtonText("End");startTrip();}
else {setSButtonText('Start');setPButtonText('Pause');endTrip();}}
}
/> />
<Button <Button
title="Pause" title={pButtonText}
onPress={() => pauseTrip()} onPress={() => { if (!Trips.inProgress) Alert.alert("No trip in progress!"); else {
/> if (!Trips.paused) {setPButtonText("Resume");pauseTrip();}
<Button else {setPButtonText('Pause');pauseTrip();}}}
title="Stop" }
onPress={() => endTrip() }
/> />
</View> </View>
< ThisTrip.tripDisplay /> <TripDisplay></TripDisplay>
</View> </View>
); );