Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-weng committed Aug 16, 2016
0 parents commit daeff36
Show file tree
Hide file tree
Showing 9 changed files with 426 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./node_modules/ruff-standard/eslint-base.json"
}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/node_modules/
/ruff_modules/
/.ruff/

.DS_Store

8 changes: 8 additions & 0 deletions .rapignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# See https://github.com/isaacs/node-glob

test/**

*.tar
*.zip

.*
95 changes: 95 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
[![Build Status](https://travis-ci.org/ruff-drivers/ruff-v1-usb-camera.svg)](https://travis-ci.org/ruff-drivers/ruff-v1-usb-camera)

# USB Camera Driver for Ruff

The driver of camera with USB interfae that is compatible with the protocol of UVC(USB Video Class)

## Supported Engines

* Ruff: >=1.4.0 <1.5.0

## Supported Models

- [UVC](https://rap.ruff.io/devices/ruff-v1-usb-camera)

## Installing

1. Execute following command and enter a **supported model** to install.

```sh
# Please replace `<device-id>` with a proper ID.
# And this will be what you are going to query while `$('#<device-id>')`.
rap device add <device-id>

# Then enter a supported model, for example:
# ? model: uvc-01
```

## Usage

Here is the basic usage of this driver.

```js
var resolution = {
width: 800,
height: 600
};
var picture = camera.capture(resolution, callback);

picture.on('data', function (data) {
// ...
});

picture.on('end', function () {
// ...
});
```

## API References

### Methods

#### `capture([resolution][, callback])`

Capture a picture and return an object which emits `data` and `end` events.

The `data` event supplies the data of captured picture with JPEG format, and the `end` event informs the data is ended.

- **resolution:** It is an object which contains `width` and `height` properties, i.e. the resolution is `width`x`height`.
Due to memory limitation, the `resolution` must be less than `1280x720`.
If you set some resolution that is not supported by the camera, this driver will change your setting to one supported resolution.

- **callback:** No argument other than a possible error is given to the completion callback. The callback will be invoked when the capture operation is finished.

Both `resolution` and `callback` are optional parameters. The default `resolution` is 320x240.

## Contributing

Contributions to this project are warmly welcome. But before you open a pull request, please make sure your changes are passing code linting and tests.

You will need the latest [Ruff SDK](https://ruff.io/) to install rap dependencies and then to run tests.

### Installing Dependencies

```sh
npm install
rap install
```

### Running Tests

```sh
npm test
```

## License

The MIT License (MIT)

Copyright (c) 2016 Nanchao Inc.

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.
5 changes: 5 additions & 0 deletions driver.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"models": [
"uvc-01"
]
}
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "ruff-v1-usb-camera",
"version": "0.1.0",
"description": "Capture a picture using USB camera.",
"author": "Nanchao Inc.",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/ruff-drivers/ruff-v1-usb-camera.git"
},
"bugs": {
"url": "https://github.com/ruff-drivers/ruff-v1-usb-camera/issues"
},
"homepage": "https://github.com/ruff-drivers/ruff-v1-usb-camera",
"keywords": [
"ruff",
"driver"
],
"main": "src/index.js",
"device-typings": "typings/device.d.ts",
"engines": {
"ruff": ">=1.4.0 <1.5.0"
},
"boards": {
"ruff-mbd-v1": "*"
},
"scripts": {
"install-sdk": "rvm install --local",
"install-rap-dependencies": "rap install",
"lint": "eslint .",
"pretest": "npm run lint"
},
"devDependencies": {
"eslint": "^2.11.1",
"ruff-standard": "^0.1.4",
"rvm": "^0.3.2"
},
"ruff": {
"devDependencies": {
"t": "^0.1.6"
}
}
}
198 changes: 198 additions & 0 deletions src/camera.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@

'use strict';

var util = require('util');
var EventEmitter = require('events');
var Poll = require('poll');
var fs = require('fs');
var async = require('ruff-async');

var MODE_666 = parseInt('666', 8);
var FIFO_NAME = 'pic.fifo';
var FIFO_PATH = '/tmp/' + FIFO_NAME;

var CHECK_DEVICE_INTERVAL = 100;
var CHECK_DEVICE_TIMES = 100;
var CHUNK_SIZE = 4096;

function mkfifo(name, callback) {
uv.spawn('/usr/bin/mkfifo', [name], '/tmp/', -1, -1, -1, function (code) {
if (code === 0) {
callback && callback();
return;
}
callback && callback(new Error('Camera initialize step 1 failed'));
});
}

function rmfifo(name, callback) {
uv.spawn('/bin/rm', ['-f', name], '/tmp/', -1, -1, -1, function (code) {
if (code === 0) {
callback && callback();
return;
}
callback && callback(new Error('Camera initialize step 2 failed'));
});
}

function grabPicture(device, resolution, callback) {
var args = [];
args.push('-v');
args.push('--skip', '3');
args.push('-d', device);
args.push('-r', resolution);
args.push('--save', FIFO_NAME);
uv.spawn('/usr/bin/fswebcam', args, '/tmp/', -1, -1, -1, function (code) {
if (code === 0) {
callback && callback();
return;
}
callback && callback(new Error('Camera capture failed'));
});
}

function Camera(option) {
this._device = option && option.device || '/dev/video0';
this._fifoFd = -1;
this._captureQueue = new async.Queue(this._captureHandler);
this._grabFinish = false;
this._readFinish = false;
this._checkDeviceInterval = CHECK_DEVICE_INTERVAL;
this._checkDeviceIntervalHandler = undefined;
this._checkDeviceTimes = 0;
}

function Picture() {
EventEmitter.call(this);
}
util.inherits(Picture, EventEmitter);

Camera.prototype._checkDevice = function (callback) {
var that = this;
checkCameraExisted();

function checkCameraExisted() {
if (that._checkDeviceTimes >= CHECK_DEVICE_TIMES) {
checkFinish(new Error('Cannot find USB camera'));
return;
}
that._checkDeviceTimes++;
try {
fs.statSync(that._device);
checkFinish();
return;
} catch (error) {
}
if (!that._checkDeviceIntervalHandler) {
that._checkDeviceIntervalHandler = setInterval(checkCameraExisted, that._checkDeviceInterval);
}
}

function checkFinish(error) {
clearInterval(that._checkDeviceIntervalHandler);
that._checkDeviceTimes = 0;
callback && callback(error);
}
};

Camera.prototype._grabInit = function (callback) {
var that = this;
async.series([
rmfifo.bind(undefined, FIFO_NAME),
mkfifo.bind(undefined, FIFO_NAME),
function (next) {
fs.open(FIFO_PATH, 'rn', MODE_666, function (error, fd) {
if (error) {
next(error);
return;
}
that._fifoFd = fd;
next();
});
}
], function (error) {
that._readPoll = new Poll(that._fifoFd);
callback && callback(error);
});
};

Camera.prototype._grab = function (option, picture, callback) {
var width = option && option.width || 320;
var height = option && option.height || 240;
var resolution = width + 'x' + height;
var that = this;
var buffer = new Buffer(CHUNK_SIZE);

this._readPoll.start(Poll.READ_EVENT, function (error, event) {
if (error) {
callback && callback(error);
return;
}
if (event === Poll.READ_EVENT) {
var length = fs.readSync(that._fifoFd, buffer, 0, CHUNK_SIZE, -1);
if (length === 0) {
that._readPoll.stop();
picture.emit('end');
that._readCleanup(callback);
return;
}
picture.emit('data', buffer.slice(0, length));
}
});
grabPicture(this._device, resolution, this._grabCleanup.bind(this, callback));
return picture;
};

Camera.prototype._grabCleanup = function (callback) {
this._grabFinish = true;
this._cleanup(callback);
};

Camera.prototype._readCleanup = function (callback) {
this._readFinish = true;
async.series([
this._readPoll.close.bind(this._readPoll),
fs.close.bind(fs, this._fifoFd),
this._cleanup.bind(this, callback)
]);
};

Camera.prototype._cleanup = function (callback) {
if (this._readFinish && this._grabFinish) {
this._readFinish = false;
this._grabFinish = false;
callback && callback();
return;
}
};

Camera.prototype._captureHandler = function (option, picture, callback) {
async.series([
this._checkDevice.bind(this),
this._grabInit.bind(this),
this._grab.bind(this, option, picture)
], callback);
};

Camera.prototype.capture = function () {
var option;
var callback;
if (arguments.length >= 2) {
option = arguments[0];
callback = arguments[1];
}
if (arguments.length === 1) {
if (typeof arguments[0] === 'object') {
option = arguments[0];
}
if (typeof arguments[0] === 'function') {
callback = arguments[0];
}
}

var picture = new Picture();
this._captureQueue.push(this, [option, picture], callback);
return picture;
};

module.exports = Camera;
Loading

0 comments on commit daeff36

Please sign in to comment.