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

Commit

Permalink
Merge pull request #197 from microsoft/users/t-anmah/sensor-integration
Browse files Browse the repository at this point in the history
 Sensor Integration
  • Loading branch information
xnkevinnguyen authored Feb 12, 2020
2 parents 2e4e326 + f760dc9 commit ff0d18f
Show file tree
Hide file tree
Showing 20 changed files with 1,685 additions and 2,960 deletions.
3,585 changes: 943 additions & 2,642 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/microbit/__model/accelerometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ def __add_current_gesture_to_gesture_lists(self):
if self.__current_gesture in CONSTANTS.GESTURES:
self.__gestures.append(self.__current_gesture)
self.__prev_gestures.add(self.__current_gesture)

def __update(self, axis, accel):
if accel is not None:
previous_accel = self.__get_accel(axis)
if accel != previous_accel:
self.__set_accel(axis, accel)
10 changes: 10 additions & 0 deletions src/microbit/__model/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ def __press_down(self):

def __release(self):
self.__pressed = False

def __update(self, is_button_pressed):
if is_button_pressed is not None:
was_button_pressed = self.is_pressed()

if is_button_pressed != was_button_pressed:
if is_button_pressed:
self.__press_down()
else:
self.__release()
10 changes: 10 additions & 0 deletions src/microbit/__model/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,13 @@
"button_a",
"button_b",
]

EXPECTED_INPUT_ACCEL = {
"motion_x": "x",
"motion_y": "y",
"motion_z": "z",
}

EXPECTED_INPUT_LIGHT = "light"

EXPECTED_INPUT_TEMP = "temperature"
6 changes: 6 additions & 0 deletions src/microbit/__model/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ def __update_client(self):
sendable_json = {"leds": self.__get_array()}
utils.send_to_simulator(sendable_json, CONSTANTS.MICROBIT)

def __update_light_level(self, new_light_level):
if new_light_level is not None:
previous_light_level = self.read_light_level()
if new_light_level != previous_light_level:
self.__set_light_level(new_light_level)

@staticmethod
def sleep_ms(ms):
time.sleep(ms / 1000)
37 changes: 27 additions & 10 deletions src/microbit/__model/microbit_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def __init__(self):

self.__start_time = time.time()
self.__temperature = 0

self.microbit_button_dict = {
self.__microbit_button_dict = {
"button_a": self.button_a,
"button_b": self.button_b,
}
Expand All @@ -42,17 +41,35 @@ def __set_temperature(self, temperature):
self.__temperature = temperature

def update_state(self, new_state):
self.__update_buttons(new_state)
self.__update_motion(new_state)
self.__update_light(new_state)
self.__update_temp(new_state)

# helpers
def __update_buttons(self, new_state):
# get button pushes
for button_name in CONSTANTS.EXPECTED_INPUT_BUTTONS:
button = self.microbit_button_dict[button_name]
button = self.__microbit_button_dict[button_name]
button._Button__update(new_state.get(button_name))

def __update_motion(self, new_state):
# set motion_x, motion_y, motion_z
for name, direction in CONSTANTS.EXPECTED_INPUT_ACCEL.items():
self.accelerometer._Accelerometer__update(direction, new_state.get(name))

previous_pressed = button.is_pressed()
button_pressed = new_state.get(button_name, previous_pressed)
def __update_light(self, new_state):
# set light level
new_light_level = new_state.get(CONSTANTS.EXPECTED_INPUT_LIGHT)
self.display._Display__update_light_level(new_light_level)

if button_pressed != previous_pressed:
if button_pressed:
button._Button__press_down()
else:
button._Button__release()
def __update_temp(self, new_state):
# set temperature
new_temp = new_state.get(CONSTANTS.EXPECTED_INPUT_TEMP)
if new_temp is not None:
previous_temp = self.temperature()
if new_temp != previous_temp:
self._MicrobitModel__set_temperature(new_temp)


__mb = MicrobitModel()
1 change: 0 additions & 1 deletion src/process_user_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def run(self):
new_state_message = json.loads(read_val)
device = new_state_message.get(CONSTANTS.ACTIVE_DEVICE_FIELD)
new_state = new_state_message.get(CONSTANTS.STATE_FIELD, {})

if device in device_dict:
device_dict[device].update_state(new_state)
else:
Expand Down
24 changes: 12 additions & 12 deletions src/view/components/cpx/Cpx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import * as React from "react";
import { TOOLBAR_ICON_ID } from "../../components/toolbar/SensorModalUtils";
import { CPX_TOOLBAR_ICON_ID } from "../../components/toolbar/SensorModalUtils";
import ToolBar from "../../components/toolbar/ToolBar";
import * as TOOLBAR_SVG from "../../svgs/toolbar_svg";
import Simulator from "./CpxSimulator";
Expand All @@ -23,46 +23,46 @@ export class Cpx extends React.Component {
const CPX_TOOLBAR_BUTTONS: Array<{ label: any; image: any }> = [
{
image: TOOLBAR_SVG.SLIDER_SWITCH_SVG,
label: TOOLBAR_ICON_ID.SWITCH,
label: CPX_TOOLBAR_ICON_ID.SWITCH,
},
{
image: TOOLBAR_SVG.PUSH_BUTTON_SVG,
label: TOOLBAR_ICON_ID.PUSH_BUTTON,
label: CPX_TOOLBAR_ICON_ID.PUSH_BUTTON,
},
{
image: TOOLBAR_SVG.RED_LED_SVG,
label: TOOLBAR_ICON_ID.RED_LED,
label: CPX_TOOLBAR_ICON_ID.RED_LED,
},
{
image: TOOLBAR_SVG.SOUND_SVG,
label: TOOLBAR_ICON_ID.SOUND,
label: CPX_TOOLBAR_ICON_ID.SOUND,
},
{
image: TOOLBAR_SVG.TEMPERATURE_SVG,
label: TOOLBAR_ICON_ID.TEMPERATURE,
label: CPX_TOOLBAR_ICON_ID.TEMPERATURE,
},
{
image: TOOLBAR_SVG.LIGHT_SVG,
label: TOOLBAR_ICON_ID.LIGHT,
label: CPX_TOOLBAR_ICON_ID.LIGHT,
},
{
image: TOOLBAR_SVG.NEO_PIXEL_SVG,
label: TOOLBAR_ICON_ID.NEO_PIXEL,
label: CPX_TOOLBAR_ICON_ID.NEO_PIXEL,
},
{
image: TOOLBAR_SVG.SPEAKER_SVG,
label: TOOLBAR_ICON_ID.SPEAKER,
label: CPX_TOOLBAR_ICON_ID.SPEAKER,
},
{
image: TOOLBAR_SVG.MOTION_SVG,
label: TOOLBAR_ICON_ID.MOTION,
label: CPX_TOOLBAR_ICON_ID.MOTION,
},
{
image: TOOLBAR_SVG.IR_SVG,
label: TOOLBAR_ICON_ID.IR,
label: CPX_TOOLBAR_ICON_ID.IR,
},
{
image: TOOLBAR_SVG.GPIO_SVG,
label: TOOLBAR_ICON_ID.GPIO,
label: CPX_TOOLBAR_ICON_ID.GPIO,
},
];
2 changes: 1 addition & 1 deletion src/view/components/cpx/CpxSimulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license.

import * as React from "react";
import { CONSTANTS, WEBVIEW_MESSAGES, DEVICE_LIST_KEY } from "../../constants";
import { CONSTANTS, DEVICE_LIST_KEY, WEBVIEW_MESSAGES } from "../../constants";
import { sendMessage } from "../../utils/MessageUtils";

import "../../styles/Simulator.css";
Expand Down
28 changes: 27 additions & 1 deletion src/view/components/microbit/Microbit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// Licensed under the MIT license.

import * as React from "react";
import { MICROBIT_TOOLBAR_ID } from "../../components/toolbar/SensorModalUtils";
import "../../styles/Simulator.css";
import * as TOOLBAR_SVG from "../../svgs/toolbar_svg";
import ToolBar from "../toolbar/ToolBar";
import { MicrobitSimulator } from "./MicrobitSimulator";

// Component grouping the functionality for micro:bit functionalities
Expand All @@ -12,8 +15,31 @@ export class Microbit extends React.Component {
return (
<React.Fragment>
<MicrobitSimulator />
{/* Implement toolbar here */}
<ToolBar buttonList={MICROBIT_TOOLBAR_BUTTONS} />
</React.Fragment>
);
}
}

const MICROBIT_TOOLBAR_BUTTONS: Array<{ label: string; image: JSX.Element }> = [
{
image: TOOLBAR_SVG.PUSH_BUTTON_SVG,
label: MICROBIT_TOOLBAR_ID.PUSH_BUTTON,
},
{
image: TOOLBAR_SVG.RED_LED_SVG,
label: MICROBIT_TOOLBAR_ID.LEDS,
},
{
image: TOOLBAR_SVG.TEMPERATURE_SVG,
label: MICROBIT_TOOLBAR_ID.TEMPERATURE,
},
{
image: TOOLBAR_SVG.LIGHT_SVG,
label: MICROBIT_TOOLBAR_ID.LIGHT,
},
{
image: TOOLBAR_SVG.MOTION_SVG,
label: MICROBIT_TOOLBAR_ID.ACCELEROMETER,
},
];
2 changes: 1 addition & 1 deletion src/view/components/microbit/MicrobitImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const setupAllButtons = (eventTriggers: EventTriggers, buttonRefs: Object) => {
};
const updateAllLeds = (
leds: number[][],
ledRefs: Array<React.RefObject<SVGRectElement>>[]
ledRefs: Array<Array<React.RefObject<SVGRectElement>>>
) => {
for (let j = 0; j < leds.length; j++) {
for (let i = 0; i < leds[0].length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions src/view/components/microbit/MicrobitSimulator.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from "react";
import {
WEBVIEW_MESSAGES,
MICROBIT_BUTTONS_KEYS,
DEVICE_LIST_KEY,
MICROBIT_BUTTONS_KEYS,
WEBVIEW_MESSAGES,
} from "../../constants";
import PlayLogo from "../../svgs/play_svg";
import StopLogo from "../../svgs/stop_svg";
Expand Down
21 changes: 5 additions & 16 deletions src/view/components/toolbar/InputSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@
// Licensed under the MIT license.

import * as React from "react";
import { WEBVIEW_MESSAGES } from "../../constants";
import "../../styles/InputSlider.css";
import { sendMessage } from "../../utils/MessageUtils";
import { ISliderProps } from "../../viewUtils";

interface vscode {
postMessage(message: any): void;
}

declare const vscode: vscode;

const sendMessage = (state: any) => {
vscode.postMessage({ command: "sensor-changed", text: state });
};

class InputSlider extends React.Component<ISliderProps, any, any> {
constructor(props: ISliderProps) {
super(props);
Expand Down Expand Up @@ -63,7 +55,7 @@ class InputSlider extends React.Component<ISliderProps, any, any> {
value={this.state.value}
onInput={this.handleOnChange}
defaultValue={this.props.minValue.toLocaleString()}
pattern="^-?[0-9]{0,3}$"
pattern="^-?[0-9]{0,4}$"
onKeyUp={this.handleOnChange}
aria-label={`${this.props.type} sensor input ${this.props.axisLabel}`}
/>
Expand Down Expand Up @@ -100,7 +92,7 @@ class InputSlider extends React.Component<ISliderProps, any, any> {
const validatedValue = this.validateRange(this.updateValue(event));
const newSensorState = this.writeMessage(validatedValue);
if (newSensorState) {
sendMessage(newSensorState);
sendMessage(WEBVIEW_MESSAGES.SENSOR_CHANGED, newSensorState);
}
};

Expand All @@ -124,10 +116,7 @@ class InputSlider extends React.Component<ISliderProps, any, any> {
};

private sendTelemetry = () => {
vscode.postMessage({
command: "slider-telemetry",
text: this.props.type,
});
sendMessage(WEBVIEW_MESSAGES.SLIDER_TELEMETRY, this.props.type);
};

private validateRange = (valueString: string) => {
Expand Down
Loading

0 comments on commit ff0d18f

Please sign in to comment.