Skip to content

Commit

Permalink
Updated log interface; removed discoverAll
Browse files Browse the repository at this point in the history
  • Loading branch information
memakura committed Apr 18, 2018
1 parent cffdcac commit 81dee1f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 75 deletions.
98 changes: 54 additions & 44 deletions app/index.html
Original file line number Diff line number Diff line change
@@ -1,55 +1,65 @@
<!DOCTYPE html>
<html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta charset="utf-8">
<title>s2microbit-ble</title>
<link rel="stylesheet" type="text/css" href="renderer.css" media="all">
</head>
<body>
<div class="title-box">
<div class="title-icon">
<a href="http://memakura.github.io/s2microbit-ble" target="_blank">
<img class="icon" width="80" src="images/s2microbit-ble.png">
</a>
</div>
<div class="title-text"> s2microbit-ble</div>
</div>
<div class="abstract basefont-ja">
Scratch 2 Offline Extension for BBC micro:bit
</div>
<a class="square_btn" href="http://github.com/memakura/s2microbit-ble/wiki" target="_blank">使い方</a>
<a class="square_btn" href="http://memakura.github.io/s2microbit-ble" target="_blank">プロジェクトページ</a>
<a class="square_btn" href="https://qiita.com/memakura/items/1acab55a37651e9081b4" target="_blank">しくみ</a>
<br>
<!-- All of the Node.js APIs are available in this renderer process. -->
<!-- We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
-->
<div class="projects">
<p class="basefont-ja">Scratch2プロジェクトとブロック拡張</p>
<a class="btn btn-circle" href="00scratch/cat.sb2" download="cat.sb2">豆猫</a>
<a class="btn btn-circle" href="00scratch/fly.sb2" download="fly.sb2">飛ぶ</a>
<a class="btn btn-circle" href="00scratch/small-tests.sb2" download="small-tests.sb2">いろいろ</a>
<a class="btn btn-rect" href="00scratch/s2microbit_JA.s2e" download="s2microbit_JA.s2e">ブロック (s2e)</a>
<!-- English -->
<p class="basefont-en">Scratch2 projects and block extension</p>
<a class="btn btn-circle" href="00scratch/cat_EN.sb2" download="cat_EN.sb2">cat</a></li>
<a class="btn btn-circle" href="00scratch/fly_EN.sb2" download="fly_EN.sb2">fly</a></li>
<a class="btn btn-circle" href="00scratch/small-tests_EN.sb2" download="small-tests_EN.sb2">small tests</a>
<a class="btn btn-rect" href="00scratch/s2microbit_EN.s2e" download="s2microbit_EN.s2e">block (s2e)</a>
<!-- HEX -->
<p class="basefont-ja">micro:bitへ転送しておくHEXファイル</p>
<a class="btn btn-rect" href="00microbit/microbit-scratch-extension2.hex" download="microbit-scratch-extension2.hex">hex</a>
</div>
</p>
<div id="container">
<div id="main_pane">
<div class="title-box">
<div class="title-icon">
<a href="http://memakura.github.io/s2microbit-ble" target="_blank">
<img class="icon" width="80" src="images/s2microbit-ble.png">
</a>
</div>
<div class="title-text"> s2microbit-ble</div>
</div>
<div class="abstract basefont-ja">
Scratch 2 Offline Extension for BBC micro:bit
</div>
<div class="help-links">
<a class="square-btn" href="http://github.com/memakura/s2microbit-ble/wiki" target="_blank">使い方</a>
<a class="square-btn" href="http://memakura.github.io/s2microbit-ble" target="_blank">プロジェクトページ</a>
<a class="square-btn" href="https://qiita.com/memakura/items/1acab55a37651e9081b4" target="_blank">しくみ</a>
</div>
<!-- All of the Node.js APIs are available in this renderer process. -->
<!-- We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
-->
<div class="projects">
<p class="basefont-ja">Scratch2プロジェクトとブロック拡張</p>
<div class="project-links">
<a class="btn btn-circle" href="00scratch/cat.sb2" download="cat.sb2">豆猫</a>
<a class="btn btn-circle" href="00scratch/fly.sb2" download="fly.sb2">飛ぶ</a>
<a class="btn btn-circle" href="00scratch/small-tests.sb2" download="small-tests.sb2">いろいろ</a>
<a class="btn btn-rect" href="00scratch/s2microbit_JA.s2e" download="s2microbit_JA.s2e">ブロック (s2e)</a>
</div>
<!-- English -->
<p class="basefont-en">Scratch2 projects and block extension</p>
<div class="project-links">
<a class="btn btn-circle" href="00scratch/cat_EN.sb2" download="cat_EN.sb2">cat</a></li>
<a class="btn btn-circle" href="00scratch/fly_EN.sb2" download="fly_EN.sb2">fly</a></li>
<a class="btn btn-circle" href="00scratch/small-tests_EN.sb2" download="small-tests_EN.sb2">small tests</a>
<a class="btn btn-rect" href="00scratch/s2microbit_EN.s2e" download="s2microbit_EN.s2e">block (s2e)</a>
</div>
<!-- HEX -->
<p class="basefont-ja">micro:bitへ転送しておくHEXファイル</p>
<div class="project-links">
<a class="btn btn-rect" href="00microbit/microbit-scratch-extension2.hex" download="microbit-scratch-extension2.hex">hex</a>
</div>
</div>
</p>
</div> <!-- end main_pane -->
<div id="log_pane">
<div id="console_log"></div>
</div> <!-- end log_pane -->
</div>

<script>
require('./renderer.js');
const { ipcRenderer } = require('electron');
ipcRenderer.on('mainmsg', function (event, msg) {
console.log("[main] " + msg);
});
require('./renderer.js');
</script>

</body>
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s2microbit-ble",
"version": "2.2.2",
"version": "2.3.0",
"description": "BBC micro:bit (bluetooth)",
"main": "s2microbit-ble.js",
"scripts": {
Expand Down
62 changes: 52 additions & 10 deletions app/renderer.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
#container {
display: grid;
grid-template-columns: 390px 1fr;
}
#main_pane {
grid-row: 1 / 2;
grid-column: 1 / 2;
}
#log_pane {
grid-row: 1 / 2;
grid-column: 2 / 3;
}

#console_log {
height: 90vh; /* should be viewport height to use overflow, not '%' */
padding: 0.2em 0.5em;
margin: 10px 15px 10px 0px;
color: #666;
background: #d6ebff;
box-shadow: 0px 0px 0px 10px #d6ebff;
border: dashed 2px white;
overflow: auto; /* scroll */
border-radius: 130px 10px 110px 10px / 10px 130px 10px 110px;
}
::-webkit-scrollbar {
width: 10px;
}
::-webkit-scrollbar-track {
border-radius: 10px;
box-shadow: inset 0 0 6px rgba(0, 0, 0, .1);
}
::-webkit-scrollbar-thumb {
background-color: rgba(0, 0, 50, .5);
border-radius: 10px;
box-shadow:0 0 0 1px rgba(255, 255, 255, .3);
}

.basefont-ja {
font-size: 16px;
color: #666;
Expand Down Expand Up @@ -38,20 +75,25 @@
transform: translateX(-50%);
}
.title-icon {
margin: 0 0 0 0;
margin: 0px 0px 0px 0px;
}
.abstract {
text-align: center;
margin: 0 0 24px 0;
/* text-align: center; */
margin: 0px 0px 24px 30px;
}
.help-links {
/* text-align: center; */
margin: 0px 0px 0px 30px;
}

.projects p {
margin: 30px 0px 10px 0px;
background: linear-gradient(transparent 70%, #ffe798 70%);
width: 90%;
margin: 30px 0px 10px 5px;
background: linear-gradient(transparent 60%, #ffdb98 70%);
width: 80%;
}

.square_btn{
.project-links {
margin: 0px 0px 0px 20px;
}
.square-btn{
position: relative;
display: inline-block;
font-weight: bold;
Expand All @@ -62,7 +104,7 @@
transition: .4s;
}

.square_btn:hover {
.square-btn:hover {
background: #00bcd4; /* #008a9c; */ /* #668ad8; */
color: white;
}
Expand Down
11 changes: 9 additions & 2 deletions app/renderer.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
"use strict";

const { shell } = require('electron');
const { ipcRenderer, shell } = require('electron');
const $ = require('jquery');
//const open = require('open');

ipcRenderer.on('mainmsg', function (event, msg) {
//console.log("[main] " + msg);
var divLogElem = document.getElementById('console_log');
divLogElem.innerHTML += msg + "<br>";
divLogElem.scrollTop = divLogElem.scrollHeight;
});

// left button
$(document).on('click', 'a[href^="http"]', function (event) {
event.preventDefault();
Expand All @@ -14,6 +21,6 @@ $(document).on('click', 'a[href^="http"]', function (event) {
$(document).on('mousedown', 'a[href^="http"]', function (event) {
if(event.which==2) {
event.preventDefault();
shell.openExternal(this.href);
shell.openExternal(this.href);
}
});
46 changes: 28 additions & 18 deletions app/s2microbit-ble.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ const { app, ipcMain, BrowserWindow } = require('electron');
const path = require('path');
const url = require('url');

const BBCMicrobit = require('bbc-microbit');
let device = null;
let microbitConnected = false;


// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;


function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 960,
height: 600
width: 820,
height: 730
});

// and load the index.html of the app.
Expand All @@ -27,10 +32,12 @@ function createWindow () {
}));

// Open the DevTools.
mainWindow.webContents.openDevTools();
//mainWindow.webContents.openDevTools();
mainWindow.webContents.on('did-finish-load', function() {
// Find microbits
microbitScanner();
if (device === null) {
microbitScanner();
}
});

// Emitted when the window is closed.
Expand Down Expand Up @@ -70,10 +77,6 @@ app.on('activate', function () {

// https://github.com/sandeepmistry/node-bbc-microbit/blob/master/API.md

const BBCMicrobit = require('bbc-microbit');
let device = null;
let microbitConnected = false;

const BUTTON_VALUE_MAPPER = ['Not Pressed', 'Pressed', 'Long Press'];

let debug = false;
Expand Down Expand Up @@ -194,11 +197,11 @@ function initValues () {

initValues();


console.log("=== BBC micro:bit Scratch 2.0 offline extension ===");
// createWindow (mainWindow.webContents.on('did-finish-load') )
// -> microbitScanner -> microbitFound -> connectAndSetup -> startHTTPServer


// Output log to both terminal and renderer's console (in the developer tool)
function logBothConsole (msg) {
console.log(msg);
Expand All @@ -208,14 +211,20 @@ function logBothConsole (msg) {
//var id = 'dd628ee75dfe';
var id = 'd5a250cd6035';

//let scanning_all = false;

// Discover microbit
function microbitScanner() {
logBothConsole("microbit: scanning...");

BBCMicrobit.discoverAll( function (microbit) {
logBothConsole(" found microbit : " + microbit);
}); // find all microbits

/*
if (!scanning_all) {
BBCMicrobit.discoverAll( function (microbit) { // keep scanning until stop is called
//logBothConsole(" found microbit : " + microbit);
logBothConsole(" found microbit : " + microbit.id);
scanning_all = true;
}); // find all microbits
}
*/
//BBCMicrobit.discoverById(id, microbitFound);
BBCMicrobit.discover(microbitFound);
}
Expand All @@ -240,21 +249,21 @@ function initializePinSetting(microbit) {
// Setting up pin mode (analog/digital and input/output)
function setupPinMode(data) {
if (device) {
logBothConsole('setupPinMode: pin ' + data.pin + ' is originally configured as: ' + pinMode[data.pin]);
if (debug) { logBothConsole('setupPinMode: pin ' + data.pin + ' is originally configured as: ' + pinMode[data.pin]); }
function log(data) {
logBothConsole('microbit: setup pin ' + data.pin + ' as ' + data.ADmode + ' ' + data.IOmode);
}
// SubscribeData
function subscribe(device, data) {
log(data);
device.readPin(data.pin, function(error, value) { // trigger a pinDataChange
showPinSetting(device);
if (debug) { showPinSetting(device); }
});
}
// UnsubscribeData
function unsubscribe(device) {
log(data);
showPinSetting(device);
if (debug) { showPinSetting(device); }
}

pinMode[data.pin] = PINMODE_OUTPUT_DIGITAL;
Expand Down Expand Up @@ -299,6 +308,7 @@ function microbitFound(microbit) {
microbitConnected = false;
device = null;
logBothConsole('microbit: disconnected. microbitConnected= ' + microbitConnected);
initValues();
microbitScanner();
});

Expand Down Expand Up @@ -440,7 +450,7 @@ exapp.get('/reset_all', function(req, res){
// LED matrix (image pattern)
function writeLedBuffer(error) {
device.writeLedMatrixState(ledBuffer, function(error) {
logBothConsole("microbit: writeLedBuffer: buf= " + ledBuffer.toString('hex'));
if (debug) { logBothConsole("microbit: writeLedBuffer: buf= " + ledBuffer.toString('hex')); }
});
}
// LED display preset image
Expand Down

0 comments on commit 81dee1f

Please sign in to comment.