-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simulator embedding (very incomplete, behind flag) #816
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
80e058b
Quick simulator embedding.
microbit-matt-hillsdon e87cac0
Add the sensor.
microbit-matt-hillsdon b40e505
Safari fix.
microbit-matt-hillsdon 51a7f32
More sensor support.
microbit-matt-hillsdon 97088c7
Placeholder icons.
microbit-matt-hillsdon 9eed091
Lint.
microbit-matt-hillsdon 6beb121
Merge branch 'main' into simulator
microbit-matt-hillsdon 89a98b2
Merge branch 'main' into simulator
microbit-matt-hillsdon 9eb9940
Merge branch 'main' into simulator
microbit-matt-hillsdon 044dd3c
Use new flash message on simulator branch.
microbit-matt-hillsdon d1018cf
Merge branch 'main' into simulator
microbit-matt-hillsdon a236522
Merge branch 'main' into simulator
microbit-matt-hillsdon e993127
Merge branch 'main' into simulator
microbit-matt-hillsdon 698174b
Merge branch 'main' into simulator
microbit-matt-hillsdon d2e107b
Use microbit-owned simulator URL.
microbit-matt-hillsdon fe90326
Tidying
microbit-matt-hillsdon 87324fe
More unintended change
microbit-matt-hillsdon 3a6691e
Merge branch 'main' into simulator
microbit-matt-hillsdon 29734a4
Split the simulator up.
microbit-matt-hillsdon 6d1a4cb
Import fixup.
microbit-matt-hillsdon 8724b05
Fix ratio remove hack.
microbit-matt-hillsdon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { Icon } from "@chakra-ui/icons"; | ||
import { | ||
HStack, | ||
Slider, | ||
SliderFilledTrack, | ||
SliderMark, | ||
SliderThumb, | ||
SliderTrack, | ||
} from "@chakra-ui/react"; | ||
import { useCallback } from "react"; | ||
import { RiQuestionFill } from "react-icons/ri"; | ||
import { Sensor, sensorIcons } from "./model"; | ||
|
||
interface RangeSensorProps { | ||
value: Sensor; | ||
onSensorChange: (id: string, value: number) => void; | ||
} | ||
|
||
const RangeSensor = ({ | ||
value: { id, min, max, value, unit }, | ||
onSensorChange, | ||
}: RangeSensorProps) => { | ||
const handleChange = useCallback( | ||
(value: number) => { | ||
onSensorChange(id, value); | ||
}, | ||
[onSensorChange, id] | ||
); | ||
const valueText = unit ? `${value} ${unit}` : value.toString(); | ||
return ( | ||
<HStack pt={5}> | ||
<Icon | ||
as={sensorIcons[id] || RiQuestionFill} | ||
aria-label={id} | ||
color="blimpTeal.400" | ||
boxSize="6" | ||
/> | ||
<Slider | ||
aria-label={id} | ||
value={value} | ||
min={min} | ||
max={max} | ||
onChange={handleChange} | ||
my={5} | ||
> | ||
<SliderTrack> | ||
<SliderFilledTrack bgColor="blimpTeal.600" /> | ||
</SliderTrack> | ||
<SliderThumb /> | ||
<SliderMark value={min} mt="1" fontSize="xs"> | ||
{min} | ||
</SliderMark> | ||
<SliderMark value={max} mt="1" ml="-3ch" fontSize="xs"> | ||
{max} | ||
</SliderMark> | ||
<SliderMark | ||
value={value} | ||
textAlign="center" | ||
mt="-8" | ||
ml={-valueText.length / 2 + "ch"} | ||
fontSize="xs" | ||
> | ||
{valueText} | ||
</SliderMark> | ||
</Slider> | ||
</HStack> | ||
); | ||
}; | ||
|
||
export default RangeSensor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { BoxProps, Stack } from "@chakra-ui/react"; | ||
import { Sensor } from "./model"; | ||
import RangeSensor from "./RangeSensor"; | ||
|
||
interface SensorsProps extends BoxProps { | ||
value: Sensor[]; | ||
onSensorChange: (id: string, value: number) => void; | ||
} | ||
|
||
const Sensors = ({ value, onSensorChange, ...props }: SensorsProps) => { | ||
return ( | ||
<Stack {...props} height="100%" width="100%" p={5} spacing={3}> | ||
{value.map((sensor) => { | ||
switch (sensor.type) { | ||
case "range": | ||
return ( | ||
<RangeSensor | ||
key={sensor.id} | ||
value={sensor} | ||
onSensorChange={onSensorChange} | ||
/> | ||
); | ||
default: | ||
return null; | ||
} | ||
})} | ||
</Stack> | ||
); | ||
}; | ||
|
||
export default Sensors; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { AspectRatio, Box, HStack, IconButton, VStack } from "@chakra-ui/react"; | ||
import { useRef } from "react"; | ||
import { RiPlayFill, RiStopFill } from "react-icons/ri"; | ||
import Sensors from "./Sensors"; | ||
import { useSimulator } from "./simulator-hooks"; | ||
|
||
const Simulator = () => { | ||
const ref = useRef<HTMLIFrameElement>(null); | ||
const { play, stop, sensors, onSensorChange } = useSimulator(ref); | ||
return ( | ||
<VStack spacing={5}> | ||
<Box width="100%"> | ||
<AspectRatio ratio={191.27 / 155.77} width="100%"> | ||
<Box | ||
ref={ref} | ||
as="iframe" | ||
// This needs changing before we remove the flag. | ||
src="https://stage-python-simulator.microbit.org/simulator.html" | ||
title="Simulator" | ||
frameBorder="no" | ||
scrolling="no" | ||
/> | ||
</AspectRatio> | ||
<HStack justifyContent="center"> | ||
<IconButton | ||
variant="solid" | ||
onClick={play} | ||
icon={<RiPlayFill />} | ||
aria-label="Run" | ||
/> | ||
<IconButton | ||
variant="outline" | ||
onClick={stop} | ||
icon={<RiStopFill />} | ||
aria-label="Stop" | ||
/> | ||
</HStack> | ||
</Box> | ||
<Sensors | ||
value={sensors} | ||
flex="1 1 auto" | ||
onSensorChange={onSensorChange} | ||
/> | ||
</VStack> | ||
); | ||
}; | ||
|
||
export default Simulator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { IconType } from "react-icons"; | ||
import { RiSunFill, RiTempHotFill } from "react-icons/ri"; | ||
|
||
export const sensorIcons: Record<string, IconType> = { | ||
temperature: RiTempHotFill, | ||
lightLevel: RiSunFill, | ||
}; | ||
|
||
export interface Sensor { | ||
type: "range"; | ||
id: string; | ||
min: number; | ||
max: number; | ||
value: number; | ||
unit?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import { useFileSystem } from "../fs/fs-hooks"; | ||
import { Sensor } from "./model"; | ||
|
||
export const useSimulator = (ref: React.RefObject<HTMLIFrameElement>) => { | ||
const fs = useFileSystem(); | ||
const [sensors, setSensors] = useState<Record<string, Sensor>>({}); | ||
const readyCallbacks = useRef([] as Array<() => void>); | ||
useEffect(() => { | ||
if (ref.current) { | ||
const messageListener = (e: MessageEvent) => { | ||
const simulator = ref.current!.contentWindow; | ||
if (e.source === simulator) { | ||
switch (e.data.kind) { | ||
case "ready": { | ||
setSensors( | ||
Object.fromEntries( | ||
e.data.sensors.map((json: Sensor) => { | ||
return [json.id, json]; | ||
}) | ||
) | ||
); | ||
while (readyCallbacks.current.length) { | ||
readyCallbacks.current.pop()!(); | ||
} | ||
break; | ||
} | ||
case "serial_output": { | ||
// TODO: serial | ||
break; | ||
} | ||
} | ||
} | ||
}; | ||
window.addEventListener("message", messageListener); | ||
return () => { | ||
window.removeEventListener("message", messageListener); | ||
}; | ||
} | ||
}, [ref, readyCallbacks]); | ||
|
||
const onSensorChange = useCallback( | ||
(id: string, value: number) => { | ||
setSensors((sensors) => ({ | ||
...sensors, | ||
[id]: { ...sensors[id], value }, | ||
})); | ||
const simulator = ref.current!.contentWindow!; | ||
simulator.postMessage( | ||
{ | ||
kind: "sensor_set", | ||
sensor: id, | ||
value, | ||
}, | ||
"*" | ||
); | ||
}, | ||
[ref, setSensors] | ||
); | ||
|
||
const play = useCallback(async () => { | ||
const filesystem: Record<string, Uint8Array> = Object.fromEntries( | ||
await Promise.all( | ||
fs.project.files.map(async (f) => [ | ||
f.name, | ||
(await fs.read(f.name)).data, | ||
]) | ||
) | ||
); | ||
const simulator = ref.current!.contentWindow!; | ||
simulator.postMessage( | ||
{ | ||
kind: "flash", | ||
filesystem, | ||
}, | ||
"*" | ||
); | ||
}, [ref, fs]); | ||
|
||
const stop = useCallback(async () => { | ||
const simulator = ref.current!.contentWindow!; | ||
simulator.postMessage( | ||
{ | ||
kind: "serial_input", | ||
// Ctrl-C to interrupt. | ||
// A specific message would be useful as probably best to clear display etc. here. | ||
data: `\x03`, | ||
}, | ||
"*" | ||
); | ||
}, [ref]); | ||
|
||
return { | ||
play, | ||
stop, | ||
sensors: Object.values(sensors), | ||
onSensorChange, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This split view behaves poorly when nested like this (perhaps due to minimums).
The layout as a whole isn't consistent with the latest designs.
However, so long as it has no effect on the non-flagged layout I think it's worth merging so we can avoid maintaining a separate branch as we iterate on the simulator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I think it's the equivalent of this bug:
tomkp/react-split-pane#30
So we need to add an overlay element over the iframe (or for simplicity, each side) during the drag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I understand it I feel less bad about merging it. I'm going to leave this one to fix afterwards.