Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.

Commit d3fb796

Browse files
committed
Keep the state for temperature sensor
1 parent 85fed6b commit d3fb796

File tree

8 files changed

+278
-133
lines changed

8 files changed

+278
-133
lines changed

src/view/components/cpx/Cpx.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { CPX_TOOLBAR_ICON_ID } from "../../components/toolbar/SensorModalUtils";
66
import ToolBar from "../../components/toolbar/ToolBar";
77
import * as TOOLBAR_SVG from "../../svgs/toolbar_svg";
88
import Simulator from "./CpxSimulator";
9+
import { SENSOR_LIST } from "../../constants";
910

1011
// Component grouping the functionality for circuit playground express
1112

@@ -14,10 +15,17 @@ export class Cpx extends React.Component {
1415
return (
1516
<React.Fragment>
1617
<Simulator />
17-
<ToolBar buttonList={CPX_TOOLBAR_BUTTONS} />
18+
<ToolBar
19+
buttonList={CPX_TOOLBAR_BUTTONS}
20+
onUpdateSensor={this.updateSensor}
21+
sensorValues={{}}
22+
/>
1823
</React.Fragment>
1924
);
2025
}
26+
updateSensor = (sensor: SENSOR_LIST, value: number) => {
27+
this.setState({ [sensor]: value });
28+
};
2129
}
2230

2331
const CPX_TOOLBAR_BUTTONS: Array<{ label: any; image: any }> = [

src/view/components/microbit/Microbit.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,38 @@ import "../../styles/Simulator.css";
77
import * as TOOLBAR_SVG from "../../svgs/toolbar_svg";
88
import ToolBar from "../toolbar/ToolBar";
99
import { MicrobitSimulator } from "./MicrobitSimulator";
10+
import { SENSOR_LIST } from "../../constants";
1011

1112
// Component grouping the functionality for micro:bit functionalities
13+
interface IState {
14+
sensors: { [key: string]: number };
15+
}
1216

13-
export class Microbit extends React.Component {
17+
export class Microbit extends React.Component<{}, IState> {
18+
state = {
19+
sensors: {
20+
[SENSOR_LIST.TEMPERATURE]: 0,
21+
[SENSOR_LIST.LIGHT]: 0,
22+
[SENSOR_LIST.ACCELEROMETER]: 0,
23+
},
24+
};
1425
render() {
1526
return (
1627
<React.Fragment>
1728
<MicrobitSimulator />
18-
<ToolBar buttonList={MICROBIT_TOOLBAR_BUTTONS} />
29+
<ToolBar
30+
buttonList={MICROBIT_TOOLBAR_BUTTONS}
31+
onUpdateSensor={this.updateSensor}
32+
sensorValues={this.state.sensors}
33+
/>
1934
</React.Fragment>
2035
);
2136
}
37+
updateSensor = (sensor: SENSOR_LIST, value: number) => {
38+
console.log(value);
39+
this.setState({ sensors: { ...this.state.sensors, [sensor]: value } });
40+
console.log(JSON.stringify(this.state.sensors));
41+
};
2242
}
2343

2444
const MICROBIT_TOOLBAR_BUTTONS: Array<{ label: string; image: JSX.Element }> = [

src/view/components/toolbar/InputSlider.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class InputSlider extends React.Component<ISliderProps, any, any> {
1212
constructor(props: ISliderProps) {
1313
super(props);
1414
this.state = {
15-
value: 0,
15+
value: this.props.value,
1616
};
1717

1818
this.handleOnChange = this.handleOnChange.bind(this);
@@ -44,7 +44,7 @@ class InputSlider extends React.Component<ISliderProps, any, any> {
4444
<input
4545
type="text"
4646
className="sliderValue"
47-
value={this.state.value}
47+
value={this.props.value}
4848
onInput={this.handleOnChange}
4949
defaultValue={this.props.minValue.toLocaleString()}
5050
pattern="^-?[0-9]{0,4}$"
@@ -67,7 +67,7 @@ class InputSlider extends React.Component<ISliderProps, any, any> {
6767
onKeyUp={this.sendTelemetry}
6868
onMouseUp={this.sendTelemetry}
6969
aria-valuenow={this.state.value}
70-
value={this.state.value}
70+
value={this.props.value}
7171
aria-label={`${this.props.type} sensor slider`}
7272
defaultValue={this.props.minValue.toLocaleString()}
7373
disabled={isInputDisabled}
@@ -104,7 +104,11 @@ class InputSlider extends React.Component<ISliderProps, any, any> {
104104
const newValue = event.target.validity.valid
105105
? event.target.value
106106
: this.state.value;
107-
this.setState({ value: newValue });
107+
// this.setState({ value: newValue });
108+
if (this.props.onUpdateValue) {
109+
console.log(this.props.type, newValue);
110+
this.props.onUpdateValue(this.props.type as SENSOR_LIST, newValue);
111+
}
108112
return newValue;
109113
};
110114

0 commit comments

Comments
 (0)