Giter Site home page Giter Site logo

doodlewind / react-ssd1306 Goto Github PK

View Code? Open in Web Editor NEW
360.0 9.0 24.0 92 KB

📟 A React Renderer for SSD1306 OLED chip on Raspberry Pi.

Home Page: https://github.com/doodlewind/react-ssd1306/blob/master/docs/tutorial.md

JavaScript 5.04% Makefile 0.60% C 94.19% HTML 0.17%
react-native raspberry-pi react-renderer ssd1306 quickjs c-module tutorial demo

react-ssd1306's Introduction

React SSD1306

A React Renderer for SSD1306 OLED chip on Raspberry Pi

For those who doesn't have the device, a canvas-based web emulator is also included!

Introduction

This project demonstrates how to:

  • Use React together with QuickJS on Raspberry Pi.
  • Develop basic C module for QuickJS.
  • Build a custom "native & dynamic" renderer for React.

Checkout the Tutorial, or my Chinese blog post for details.

Getting Started

This project is originally designed to work on Raspberry Pi, but a web emulator is also available and works out of the box. Notice that no matter you run it on web or native, the whole React-related codebase is exactly the same.

Web Approach

You can try out the reconciler example, even if you don't have a Raspberry Pi. In this way only Node.js and ParcelJS are required:

cd react-ssd1306/app
parcel src/index.html

Then just open https://localhost:1234 to see the emulator.

Native Approach

Connect the chip, make sure you have QuickJS and Node.js installed on your Raspberry Pi, with I2C interface enabled. Few extra packages are also required:

sudo apt-get install i2c-tools libi2c-dev

Node.js is only required for JS module bundling and package management here.

Init the project:

cd react-ssd1306/app
npm install && cd ..
npm run build # build JS and C modules
npm start # start the compiled binary

Usage

Simply edit ./app/index.js as main entrance:

import './polyfill.js'
import React from 'react'
import { SSD1306Renderer, Text, Pixel } from './renderer.js'

class App extends React.Component {
  constructor () {
    super()
    this.state = { hello: 'Hello React!', p: 0 }
  }

  render () {
    const { hello, p } = this.state
    return (
      <React.Fragment>
        <Text row={0} col={0}>{hello}</Text>
        <Text row={1} col={0}>Hello QuickJS!</Text>
        <Pixel x={p} y={p} />
      </React.Fragment>
    )
  }

  componentDidMount () {
    // XXX: Emulate event driven update
    setTimeout(() => this.setState({ hello: 'Hello Pi!', p: 42 }), 2000)
    setTimeout(() => this.setState({ hello: '', p: -1 }), 4000)
  }
}

SSD1306Renderer.render(<App />)

License

MIT

react-ssd1306's People

Contributors

dependabot[bot] avatar doodlewind avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-ssd1306's Issues

Thanks!

Thanks for taking the time to thoroughly share this original & fun experiment! Love it!

Would love to try to port this to an esp32 microcontroller!

can't import react "ReferenceError: could not load module filename 'react'"

I download react.js from https://unpkg.com/react@16/umd/react.production.min.js

as your code:
import './polyfill.js'
import React from 'react'

const App = props => {
console.log(props.hello)
return null
}
console.log(<App hello={'QuickJS'} />)

qjs to run it said: "ReferenceError: could not load module filename 'react'"
so I changed import React from 'react' to import React from './react.js', this time
I got "SyntaxError: export 'default' in module './react.js' is ambiguous"

Could your help me to point where I made mistake.

Compiling QuickJS

If you have trouble compiling QuickJS on Raspberry Pi, just link the atomic lib as seem in this discussion, this following patch should work:

diff --git a/Makefile b/Makefile
index efd3a32..3150f80 100644
--- a/Makefile
+++ b/Makefile
@@ -33,6 +33,10 @@ CONFIG_LTO=y
 #CONFIG_WERROR=y
 # force 32 bit build for some utilities
 #CONFIG_M32=y
+# compilation on ARM Linux
+ifeq ($(shell uname -m),armv7l)
+CONFIG_ARMV7=y
+endif
 ifdef CONFIG_DARWIN
 # use clang instead of gcc

@@ -167,6 +171,9 @@ LIBS=-lm
 ifndef CONFIG_WIN32
 LIBS+=-ldl
 endif
+ifdef CONFIG_ARMV7
+LIBS+=-latomic
+endif
 $(OBJDIR):
 	mkdir -p $(OBJDIR) $(OBJDIR)/examples $(OBJDIR)/tests

You can also simply change LIBS=-lm to LIBS=-lm -latomic in makefile :D

抛一个问题:关于交互

前端侵入嵌入式领域是必然的。
这里想问的是,在物理层面交互的处理上,quickjs可以做桥梁吗?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.