Skip to content

Commit

Permalink
Merge pull request #80 from tessel/kb-flash
Browse files Browse the repository at this point in the history
adds USB storage module tutorial
  • Loading branch information
Frijol committed Feb 24, 2016
2 parents 2837614 + afa9bba commit d6a513d
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
4 changes: 4 additions & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ <h1><a href="https://tessel.io/"><nobr><img src=
<a href="{{ site.baseurl }}/modules/servo.html" class=
"side-bar-modules">servo</a>
</li>
<li style="padding-left:10px;">
<a href="{{ site.baseurl }}/modules/storage.html" class=
"side-bar-modules">storage</a>
</li>
</ul></a>
</li>
<li>
Expand Down
1 change: 1 addition & 0 deletions modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ If you've already tried out your modules, move on to [Tweet.](tweet.html)
* [Relay](modules/relay.html)
* [RFID](modules/rfid.html)
* [Servo](modules/servo.html)
* [Storage](modules/storage.html)

<div class="greyBar"></div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions modules/_module_footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ <h2>Choose another module</h2>
<a href="servo.html"><span class=
"moduleLink">Servo</span></a>
</li>
<li>
<a href="storage.html"><span class=
"moduleLink">Storage</span></a>
</li>
</ul>

</div>
Expand Down
10 changes: 10 additions & 0 deletions modules/storage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
layout: default
---

{% capture include_install %}
{% include_relative storage.md %}
{% endcapture %}
{{ include_install | markdownify }}

{% include_relative _module_footer.html %}
114 changes: 114 additions & 0 deletions modules/storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{::options parse_block_html="true" /}

<div class="row">
<div class="large-12 columns">

## <img class="constrain-sm" src="https://s3.amazonaws.com/technicalmachine-assets/technical-io/modules/usb.png"> Storage (Flash Drive)

[<i class="fa fa-github"> View source on Github</i>](https://github.com/nodejs/node/blob/master/lib/fs.js)

### Step 1

Make a directory inside your "tessel-code" folder called "storage", change directory into that folder, and initialize a tessel project:

`mkdir storage; cd storage; t2 init`

### Step 2
</div>
</div>

<div class="row">
<div class="large-6 columns">

Plug Tessel into your computer via USB, then plug a USB mass storage device (flash drive) into either of Tessel's USB ports.

</div>
<div class="large-6 columns">

![](http://i.imgur.com/uifn1p7.jpg)

</div>
</div>

<div class="row">
<div class="large-12 columns">

### Step 3

You don't need to install a library to use a USB storage module. We will use the [fs](https://nodejs.org/api/fs.html) (filesystem) library that's built into Node.js to interact with USB mass storage devices.

In the folder where you ran `t2 init`, rename "index.js" to "storage.js" and replace the file's contents with the following:

{% highlight js %}
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/

/*********************************************
Write some text to a file on a USB mass storage
device, then read it back.
*********************************************/

// Import the fs library
var fs = require('fs');
var path = require('path');
var mountPoint = '/mnt/sda1'; // The first flash drive you plug in will be mounted here, the second will be at '/mnt/sdb1'
var filepath = path.join(mountPoint, 'myFile.txt');

var textToWrite = 'Hello Tessel!';

// Write the text to a file on the flash drive
fs.writeFile(filepath, textToWrite, function () {
console.log('Wrote', textToWrite, 'to', filepath, 'on USB mass storage device.');
});

// Read the text we wrote from the file
fs.readFile(filepath, function (err, data) {
console.log('Read', data.toString(), 'from USB mass storage device.');
});
{% endhighlight %}

Save the file.

</div>
</div>

<div class="row">
<div class="large-12 columns">

### Step 4

</div>
</div>

<div class="row">
<div class="large-6 columns">

In your command line, `t2 run storage.js`

Now you can create, write to, read from, and otherwise manipulate files on USB storage devices!

**Bonus:** Try writing some longer text (or output from a different module) by using streams. Hint: look up the fs function `createWriteStream`.

To see what else you can do with a USB storage module, read the [fs](https://nodejs.org/api/fs.html) documentation.

</div>
<div class="large-6 columns">

![](http://i.imgur.com/wKksk4X.gif)

</div>
</div>

<div class="row">
<div class="large-12 columns">

### Step 5

What else can you do with a USB mass storage device? Get inspired by a [community-created project.](http://tessel.io/projects)

What are you making? [Share your invention!](//tessel.io/projects)

If you run into any issues you can check out the [module forums](http://forums.tessel.io/c/modules).

</div>
</div>

0 comments on commit d6a513d

Please sign in to comment.