Last active
August 29, 2015 14:21
-
-
Save iamdustan/99a3a6ca6bd4bb5c3fb8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import React from 'react'; | |
| import ReactHardware from '../'; | |
| const { | |
| Board, | |
| Led, | |
| } = ReactHardware; | |
| const HIGH = 255; | |
| const LOW = 0; | |
| class FlashingLed extends React.Component { | |
| constructor(props: any, context: any) { | |
| super(props, context); | |
| this.state = { | |
| voltage: typeof props.initialVoltage !== 'undefined' ? props.initialVoltage : HIGH, | |
| _interval: null, | |
| }; | |
| } | |
| componentDidMount() { | |
| this.state._interval = setInterval(_ => { | |
| var voltage = this.state.voltage === HIGH ? LOW : HIGH; | |
| this.setState({voltage}); | |
| }, this.props.interval); | |
| } | |
| componentWillUnmount() { | |
| clearInterval(this.state._interval); | |
| } | |
| render() { | |
| return ( | |
| <Led {...this.props} voltage={this.state.voltage} /> | |
| ); | |
| } | |
| } | |
| FlashingLed.defaultProps = { | |
| pin: 13, | |
| interval: 2000, | |
| }; | |
| class Application extends React.Component { | |
| render(): ?ReactElement { | |
| return ( | |
| <Board port="/dev/cu.usbmodem1411"> | |
| <FlashingLed pin={2} interval={500} initialVoltage={HIGH} /> | |
| <FlashingLed pin={3} interval={500} initialVoltage={LOW} /> | |
| </Board> | |
| ); | |
| } | |
| } | |
| class Application2 extends React.Component { | |
| render(): ?ReactElement { | |
| return ( | |
| <Board> | |
| <FlashingLed pin={2} interval={500} initialVoltage={HIGH} /> | |
| <FlashingLed pin={3} interval={500} initialVoltage={LOW} /> | |
| </Board> | |
| ); | |
| } | |
| } | |
| ReactHardware.render(<Application initialVoltage={255} />, '/dev/cu.usbmodem1411', _ => ( | |
| console.log('ReactHardware mounted'/*, arguments*/) | |
| )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment