Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Sep 26, 2013
1 parent 200075a commit f15b8e0
Show file tree
Hide file tree
Showing 34 changed files with 1,510 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
*~
html/*.html
node_modules
19 changes: 19 additions & 0 deletions Index.md
@@ -0,0 +1,19 @@
<!--- Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. -->
Index
=====

Tasks
-----

* APPEND_KEYWORD: tasks

Devices
------

* APPEND_KEYWORD: devices

Peripherals
----------

* APPEND_KEYWORD: peripherals

34 changes: 34 additions & 0 deletions LICENSE
@@ -0,0 +1,34 @@
Copyright (c) 2013 Gordon Williams, Pur3 Ltd

------------------------------------------------------------------------------

All sections of code within this repository are licensed under an MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

------------------------------------------------------------------------------

All remaining work in this repository (apart from that in the datasheets directory)
is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.

To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View,
California, 94041, USA.

------------------------------------------------------------------------------
26 changes: 25 additions & 1 deletion README.md
@@ -1,4 +1,28 @@
<!--- Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. -->
EspruinoDocs
============

Tutorials, Projects and Example code from the Espruino Website
Espruino Documentation and Code

This is basically [GitHub Markdown](https://help.github.com/articles/github-flavored-markdown) but it goes through a script which looks for the following (on the start of a line).

* KEYWORDS: Comma,Separated,List ; Defines keywords for this file
* APPEND_KEYWORD: Keyword ; Append a list of pages that match the keyword

There are a few extra bits too:
* ```[[My Page]]``` links to a wiki page
* ```[[#Math.random]]``` links to that page in the Espruino code reference

It then converts the markdown to HTML and shoves it on the Espruino website. lovely.

Building
-------

```
sudo apt-add-repository ppa:richarvey/nodejs
sudo apt-get update
sudo apt-get install npm nodejs
#npm install marked pygmentize-bundled --save
npm install marked highlight.js --save
```
155 changes: 155 additions & 0 deletions bin/build.js
@@ -0,0 +1,155 @@
// Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission.
var fs = require('fs');
var marked = require('marked');
//var pygmentize = require('pygmentize-bundled')
//var hljs = require('highlight.js')

var BASEDIR = ".";

// Set default options except highlight which has no default
marked.setOptions({
gfm: true, // github markdown
// yay. both broken.
/* highlight: function (code, lang) {
return hljs.highlightAuto(lang, code).value;
},*/
/* highlight: function (code, lang, callback) {
pygmentize({ lang: lang, format: 'html' }, code, function (err, result) {
if (err) return callback(err);
callback(null, result.toString());
});
},*/
tables: true,
breaks: false,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false,
langPrefix: 'lang-'
});


function getFiles(dir) {
var results = [];
var list = fs.readdirSync(dir);
for (i in list) {
var file = list[i];
if (file == "node_modules") continue;
file = dir + '/' + file;
var stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(getFiles(file));
} else {
results.push(file);
}
}
return results;
};

function getMarkdown(dir) {
var results = [];
getFiles(dir).forEach(function(f) {
if (f.substr(-3) == ".md" && f.substr(-9)!="README.md") {
results.push(f);
}
});
return results;
}

var markdownFiles = getMarkdown(BASEDIR);

function grabKeywords(markdownFiles) {
var keywords = {};
var regex = /KEYWORDS: (.*)/;
markdownFiles.forEach(function (file) {

// get file info
var contents = fs.readFileSync(file).toString();
var contentLines = contents.split("\n");
if (contentLines[0].substr(0,15)!="<!--- Copyright") console.log("WARNING: "+file+" doesn't have a copyright line");
if (contentLines[2].substr(0,3)!="===") console.log("WARNING: "+file+" doesn't have a title on the first line");
var fileInfo = {
path : file,
title : contentLines[1], // second line
};
// add keyword for directory
file.split("/").forEach(function (k) {
if (k.indexOf(".")>0) return; // no actual files
if (keywords[k] != undefined) {
keywords[k].push(fileInfo);
} else {
keywords[k] = [fileInfo];
}
});
// add keywords in file
var match = contents.match(regex);
if (match!=null) {
match[1].split(",").forEach(function(k) {
//console.log(k);
if (keywords[k] != undefined) {
keywords[k].push(fileInfo);
} else {
keywords[k] = [fileInfo];
}
});
}
});
return keywords;
}

console.log(markdownFiles);
var keywords = grabKeywords(markdownFiles);
console.log(keywords);

htmlFiles = {};
htmlLinks = {};
markdownFiles.forEach(function (file) {
var htmlFile = file.substring(file.lastIndexOf("/")+1);
htmlFile = htmlFile.replace(/ /g,"+");
htmlFile = htmlFile.substring(0,htmlFile.lastIndexOf("."))+".html";
htmlFiles[file] = "html/"+htmlFile;
htmlLinks[file] = htmlFile;
});

markdownFiles.forEach(function (file) {
var contents = fs.readFileSync(file).toString();
// replace simple links
contents = contents.replace(/\[\[([a-zA-Z0-9_ ]+)\]\]/g,"[$1]($1.html)");
// TODO - 'Tutorial 2' -> 'Tutorial+2', recognize pages that are references in docs themselves
var contentLines = contents.split("\n");
var regex = /APPEND_KEYWORD: (.*)/;
for (i in contentLines) {
var match = contentLines[i].match(regex);
if (match!=null) {
var kw = match[1];
if (keywords[kw]!=undefined) {
var links = keywords[kw].map(function(a,b) { return "* ["+a.title+"]("+htmlLinks[a.path]+")"; });
contentLines[i] = links.join("\n");
} else {
console.log("WARNING: APPEND_KEYWORD for '"+kw+"' in "+file+" found nothing");
contentLines[i] = "";
}
}
}

contentLines.splice(0,1); // remove first line (copyright)

html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+
'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'+
'<head>'+
' <link rel="stylesheet" href="css/sitewide.css" type="text/css" />'+
' <link rel="stylesheet" href="css/sh_style.css" type="text/css" />'+
' <script type="text/javascript" src="js/sh_main.min.js"></script>'+
' <script type="text/javascript" src="js/sh_javascript.min.js"></script>'+
' <title>'+contentLines[0]+'</title>'+
'</head>'+
'<body onload="sh_highlightDocument();"><div id="wrap"><div id="main">'+
marked(contentLines.join("\n")).replace(/lang-JavaScript/g, 'sh_javascript')+
'</div></div></body>'+
'</html>';


fs.writeFile(htmlFiles[file], html);
});


5 changes: 5 additions & 0 deletions build.sh
@@ -0,0 +1,5 @@
#!/bin/bash
# Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission.
cd `dirname $0`
rm html/*.html
node bin/build.js
Binary file added datasheets/ADS7843.pdf
Binary file not shown.
Binary file added datasheets/LIS302DL.pdf
Binary file not shown.
Binary file added datasheets/LSM303DLHC.pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions devices/ADS7843.md
@@ -0,0 +1,9 @@
<!--- Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. -->
ADS7843 Touchscreen
=================

* KEYWORDS: Touchscreen,SPI,ADS7843,Sensor

[Datasheet](/datasheets/ADS7843.pdf)

See [Touchscreen](Touchscreen)
5 changes: 5 additions & 0 deletions devices/Accelerometer.md
@@ -0,0 +1,5 @@
<!--- Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. -->
Accelerometers
============

* APPEND_KEYWORD: Accelerometer
7 changes: 7 additions & 0 deletions devices/Bluetooth.md
@@ -0,0 +1,7 @@
<!--- Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. -->
Bluetooth
=========

* KEYWORDS: Bluetooth,Wireless,BT,HC-05,HC05

Plugging Espruino into a Bluetooth dongle like the HC-05 is really easy - you just need 4 wires. See the [[Wiring Up]] page.
29 changes: 29 additions & 0 deletions devices/DS18B20.js
@@ -0,0 +1,29 @@
/* Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. */
/*
Module for the DS18B20 temperature sensor
```
var sensor = require("Encoder").connect(hardware,A1);
console.log(sensor.getTemp());
```
*/
exports.connect = function(hardware,pin) {
var ow = new OneWire(pin);
ow.device = ow.search()[0];
if (ow.device==undefined) {
print("No OneWire devices found");
return undefined;
}
ow.getTemp = function () {
this.reset();
this.select(this.device);
this.write(0x44, true); // convert
this.reset();
this.select(this.device);
this.write(0xBE);
var temp = this.read() + (this.read()<<8);
if (temp > 32767) temp -= 65536;
return temp / 16.0;
}
return ow;
}
66 changes: 66 additions & 0 deletions devices/DS18B20.md
@@ -0,0 +1,66 @@
<!--- Copyright (c) 2013 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. -->
DS18B20 Temperature Sensor
=======================

* KEYWORDS: OneWire,DS18B20,Temperature,Sensor

One of the most common 1-Wire devices is the DS18B20 thermometer. Below is a very simple driver for it:

```JavaScript
OneWire.prototype.getTemp = function (addr) {
this.reset();
this.select(addr);
this.write(0x44, true); // convert
this.reset();
this.select(addr);
this.write(0xBE);
var temp = this.read() + (this.read()<<8);
if (temp > 32767) temp -= 65536;
return temp / 16.0;
}
```

To use it, connect the thermometer up - to 0v, 3.3v, and the data line to any free signal line of your Espruino device, with a 4.7k resistor between data and the 3.3v line. For this example I've chosen C14.

Note: On the devices with cables, if you have Red, Yellow and Green wires then most likely they represent:
Green Ground
Red 3.3v
Yellow Data
However please check the datasheet that came with your device.


Now initialise the OneWire driver:
```JavaScript
var ow = new OneWire(C14);
```

And copy and paste in the temperature driver above.

Then scan for devices:
```JavaScript
ow.search()
```
This will return an array containing all devices. You'll get something like:
```JavaScript
=[-8574853671160008920]
```

Then simply copy this number into parameters of the getTemp function. For instance:
```JavaScript
ow.getTemp(-8574853671160008920)
```

And the current temperature will be displayed:
```JavaScript
=27.625
```

Note that you could also save copying the address by saving it to a variable:

```JavaScript
var addr = ow.search()[0];
ow.getTemp(addr);
```

For an example of a complete project using the DS18B20, see [Tutorial 6](/Tutorial+6)

0 comments on commit f15b8e0

Please sign in to comment.