Skip to content

Commit 96704c8

Browse files
committed
Update samples
IoT.js-Samples-DCO-1.0-Signed-off-by: Daeyeon Jeong [email protected]
1 parent 67b852f commit 96704c8

File tree

140 files changed

+1817
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+1817
-0
lines changed

bridge-sample/README.md

+21

bridge-sample/js/bridge_sample.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var Bridge = require('bridge');
17+
18+
function bridge_sample(){
19+
this.bridge = new Bridge(native.MODULE_NAME);
20+
}
21+
22+
bridge_sample.prototype.getResPath = function(){
23+
return this.bridge.sendSync('getResPath', '');
24+
};
25+
26+
bridge_sample.prototype.getSystemInfo = function(callback){
27+
this.bridge.send('getSystemInfo', '', function(err, msg){
28+
callback(err, msg);
29+
});
30+
};
31+
32+
bridge_sample.prototype.testThread = function(callback){
33+
this.bridge.send('testThread', '', function(err, msg){
34+
callback(err, msg);
35+
});
36+
};
37+
38+
module.exports = new bridge_sample();

bridge-sample/module.cmake

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# General variables usable from IoT.js cmake:
16+
# - TARGET_ARCH - the target architecture (as specified during cmake step)
17+
# - TARGET_BOARD - the target board(/device)
18+
# - TARGET_OS - the target operating system
19+
#
20+
# Module related variables usable from IoT.js cmake:
21+
# - MODULE_DIR - the modules root directory
22+
# - MODULE_BINARY_DIR - the build directory for the current module
23+
# - MODULE_LIBS - list of libraries to use during linking (set this)
24+
set(MODULE_NAME "bridge_sample")
25+
26+
# DO NOT include the source files which are already in the modules.json file.
27+
28+
# If the module builds its own files into a lib please use the line below.
29+
# Note: the subdir 'lib' should contain the CMakeLists.txt describing how the
30+
# module should be built.
31+
#add_subdirectory(${MODULE_DIR}/lib/ ${MODULE_BINARY_DIR}/${MODULE_NAME})
32+
33+
# If you wish to link external libraries please add it to
34+
# the MODULE_LIBS list.
35+
#
36+
# IMPORTANT!
37+
# if the module builds its own library that should also be specified!
38+
#
39+
# Example (to add the 'demo' library for linking):
40+
#
41+
# list(APPEND MODULE_LIBS demo)

bridge-sample/modules.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"modules": {
3+
"bridge_sample": {
4+
"js_file": "js/bridge_sample.js",
5+
"native_files": ["src/iotjs_bridge_sample.c"],
6+
"init": "InitBridgeSample",
7+
"cmakefile": "module.cmake",
8+
"require": ["bridge"]
9+
}
10+
}
11+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "iotjs_def.h"
17+
#include "modules/iotjs_module_bridge.h"
18+
19+
20+
char* iotjs_bridge_sample_getSystemInfo(const char* message) {
21+
return "{'OS':'tizen'}";
22+
}
23+
24+
void thread1_worker(void* return_handle) {
25+
uv_sleep(500);
26+
iotjs_bridge_set_msg(return_handle, "{'return':'from thread..'}");
27+
}
28+
29+
void iotjs_bridge_sample_func(const char* command, const char* message,
30+
void* return_handle) {
31+
char* result = 0;
32+
if (strncmp(command, "getSystemInfo", strlen("getSystemInfo")) == 0) {
33+
result = iotjs_bridge_sample_getSystemInfo(message);
34+
if (result == 0) {
35+
iotjs_bridge_set_err(return_handle, "Can't get the resource path");
36+
} else {
37+
iotjs_bridge_set_msg(return_handle, result);
38+
}
39+
} else if (strncmp(command, "testThread", strlen("testThread")) == 0) {
40+
uv_thread_t thread1;
41+
uv_thread_create(&thread1, thread1_worker, return_handle);
42+
} else if (strncmp(command, "getResPath", strlen("getResPath")) == 0) {
43+
iotjs_bridge_set_msg(return_handle, "res/");
44+
} else {
45+
iotjs_bridge_set_err(return_handle, "Can't find command");
46+
}
47+
}
48+
49+
/**
50+
* Init method called by IoT.js
51+
*/
52+
jerry_value_t InitBridgeSample() {
53+
char* module_name = "bridge_sample";
54+
jerry_value_t mymodule = jerry_create_object();
55+
iotjs_jval_set_property_string_raw(mymodule, IOTJS_MAGIC_STRING_MODULE_NAME,
56+
module_name);
57+
iotjs_bridge_register(module_name, iotjs_bridge_sample_func);
58+
return mymodule;
59+
}

bridge-sample/test.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var bridgeSample = require('bridge_sample');
17+
18+
19+
console.log('TestApp: getResPath(): ' + bridgeSample.getResPath());
20+
21+
22+
bridgeSample.testThread(function(err, msg) {
23+
console.log('TestApp: testThread(): err: ' + err + ' msg: ' + msg);
24+
});
25+
26+
bridgeSample.getSystemInfo(function(err, msg) {
27+
console.log('TestApp: getSystemInfo(): err: ' + err + ' msg: ' + msg);
28+
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

fur-elise/play.js

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
/**
16+
* Description:
17+
*
18+
* Plays Bethovens Fur Elise using PWM
19+
*
20+
* Usage:
21+
*
22+
* To run this sample please connect a low-power speaker, like a buzzer
23+
* (piezoelectric speaker), negative feed (-) to GND and positive feed (+) to
24+
* pin 7 on Artik053 CON703, and run the code by executing
25+
*
26+
* $ iotjs play.js
27+
*
28+
*/
29+
30+
var pwm = require('pwm'),
31+
// note indexes definition
32+
// please remember that D# is same as Bb here
33+
notes = {
34+
"C": 0,
35+
"C#": 1,
36+
"D": 2,
37+
"D#": 3,
38+
"E": 4,
39+
"F": 5,
40+
"F#": 6,
41+
"G": 7,
42+
"G#": 8,
43+
"A": 9,
44+
"Bb": 10,
45+
"B": 11
46+
},
47+
// note frequencies
48+
frequencies = [
49+
//C, C#, D, Eb, E, F, F#, G, G#, A, Bb, B in ocatves from 0 to 8
50+
[16.35, 17.32, 18.35, 19.45, 20.60, 21.83, 23.12, 24.50, 25.96, 27.50,
51+
29.14, 30.87],
52+
[32.70, 34.65, 36.71, 38.89, 41.20, 43.65, 46.25, 49.00, 51.91, 55.00,
53+
58.27, 61.74],
54+
[65.41, 69.30, 73.42, 77.78, 82.41, 87.31, 92.50, 98.00, 103.8, 110.0,
55+
116.5, 123.5],
56+
[130.8, 138.6, 146.8, 155.6, 164.8, 174.6, 185.0, 196.0, 207.7, 220.0,
57+
233.1, 246.9],
58+
[261.6, 277.2, 293.7, 311.1, 329.6, 349.2, 370.0, 392.0, 415.3, 440.0,
59+
466.2, 493.9],
60+
[523.3, 554.4, 587.3, 622.3, 659.3, 698.5, 740.0, 784.0, 830.6, 880.0,
61+
932.3, 987.8],
62+
[1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976],
63+
[2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951],
64+
[4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902]
65+
],
66+
// fur elise notes
67+
song = [
68+
["E6", 0.2], ["D#6", 0.2], ["E6", 0.2], ["D#6", 0.2], ["E6", 0.2],
69+
["B5", 0.2], ["D6", 0.2], ["C6", 0.2], ["A5", 0.2], ["A3", 0.2],
70+
["E4", 0.2], ["A4", 0.2], ["C5", 0.2], ["E5", 0.2], ["A5", 0.2],
71+
["E3", 0.2], ["B5", 0.2], ["E4", 0.2], ["G#4", 0.2], ["E5", 0.2],
72+
["G#5", 0.2], ["B5", 0.2], ["A3", 0.2], ["C6", 0.2], ["E4", 0.2],
73+
["A4", 0.2], ["E5", 0.2], ["E6", 0.2], ["E6", 0.2], ["D#6", 0.2],
74+
["D#6", 0.2], ["E6", 0.2], ["E6", 0.2], ["D#6", 0.2], ["D#6", 0.2],
75+
["E6", 0.2], ["E6", 0.2], ["B5", 0.2], ["B5", 0.2], ["D6", 0.2],
76+
["D6", 0.2], ["C6", 0.2], ["C6", 0.2], ["A3", 0.2], ["A5", 0.2],
77+
["A5", 0.2], ["E4", 0.2], ["A4", 0.2], ["C5", 0.2], ["E5", 0.2],
78+
["A5", 0.2], ["E3", 0.2], ["B5", 0.2], ["E4", 0.2], ["G#4", 0.2],
79+
["E5", 0.2], ["C6", 0.2], ["B5", 0.2], ["A5", 0.2], ["A3", 0.2],
80+
["E4", 0.2], ["A4", 0.2], ["B5", 0.2], ["C6", 0.2], ["D6", 0.2],
81+
["C4", 0.2], ["E6", 0.2], ["G4", 0.2], ["C5", 0.2], ["G5", 0.2],
82+
["F6", 0.2], ["E6", 0.2], ["G3", 0.2], ["D6", 0.2], ["G4", 0.2],
83+
["B4", 0.2], ["F5", 0.2], ["E6", 0.2], ["D6", 0.2], ["A3", 0.2],
84+
["C6", 0.2], ["E4", 0.2], ["A4", 0.2], ["E5", 0.2], ["D6", 0.2],
85+
["C6", 0.2], ["E3", 0.2], ["B5", 0.2], ["E4", 0.4],
86+
["E5", 0.2], ["E6", 0.2], ["E5", 0.4], ["E6", 0.2],
87+
["E7", 0.2], ["D#6", 0.2], ["E6", 0.2], ["D#6", 0.2], ["E6", 0.2],
88+
["D#6", 0.2], ["E6", 0.2], ["D#6", 0.2], ["E6", 0.2], ["D#6", 0.2],
89+
["E6", 0.2], ["D#6", 0.2], ["E6", 0.2], ["B5", 0.2], ["D6", 0.2],
90+
["C6", 0.2], ["A3", 0.2], ["A5", 0.2], ["E4", 0.2], ["A4", 0.2],
91+
["C5", 0.2], ["E5", 0.2], ["A5", 0.2], ["E3", 0.2], ["B5", 0.2],
92+
["E4", 0.2], ["G#4", 0.2], ["E5", 0.2], ["G#5", 0.2], ["B5", 0.2],
93+
["A3", 0.2], ["C6", 0.2], ["E4", 0.2], ["A4", 0.2], ["E5", 0.2],
94+
["E6", 0.2], ["D#6", 0.2], ["E6", 0.2], ["D#6", 0.2], ["E6", 0.2],
95+
["B5", 0.2], ["D6", 0.2], ["C6", 0.2], ["A3", 0.2], ["A5", 0.2],
96+
["E4", 0.2], ["A4", 0.2], ["C5", 0.2], ["E5", 0.2], ["A5", 0.2],
97+
["E3", 0.2], ["B5", 0.2], ["E4", 0.2], ["G#4", 0.2], ["E5", 0.2],
98+
["C6", 0.2], ["B5", 0.2], ["A3", 0.2], ["A5", 0.2], ["E4", 0.2],
99+
["A4", 0.8]
100+
],
101+
log_enable = true,
102+
device = null;
103+
104+
// log only when log_enable flag is set to true
105+
function log(/*...args*/) {
106+
if (log_enable) {
107+
console.log.apply(console, [].slice.call(arguments));
108+
}
109+
}
110+
111+
// extracts frequency from freq array based on supplied note
112+
function note2freq(noteStr) {
113+
var matches = noteStr.match(/([a-zA-Z\#]+)([0-9]+)/i),
114+
freq = 0;
115+
116+
if (matches && matches.length === 3) {
117+
return frequencies[parseInt(matches[2], 10)][notes[matches[1]]];
118+
}
119+
120+
return 0;
121+
}
122+
123+
// sets pwm period and runs callback after specified length of time
124+
function setPeriod(period, length, callback) {
125+
log('period: ' + period + ', length: ' + length + ' ms');
126+
device.setPeriod(period, function (err) {
127+
if (err) {
128+
callback(err);
129+
} else {
130+
setTimeout(callback, length);
131+
}
132+
});
133+
}
134+
135+
// plays each note of song recursively and runs callback on end
136+
function playSong(song, callback, currentNote) {
137+
var idx = currentNote === undefined ? 0 : currentNote,
138+
freq = 0;
139+
if (idx < song.length) {
140+
freq = note2freq(song[idx][0]);
141+
// period = 1 second / frequency
142+
setPeriod(freq !== 0 ? 1 / freq : 0.5, 1000 * song[idx][1],
143+
playSong.bind(null, song, callback, ++idx));
144+
} else {
145+
callback();
146+
}
147+
}
148+
149+
device = pwm.open({
150+
pin: 0,
151+
dutyCycle: 0.5,
152+
period: 1 / 10
153+
}, function (err) {
154+
if (err) {
155+
log('could not open pwm device: ' + err);
156+
} else {
157+
device.setEnableSync(true);
158+
log('playing song');
159+
playSong(song, function () {
160+
device.close(function (err) {
161+
if (err) {
162+
log('error while closing device: ' + err);
163+
} else {
164+
log('done');
165+
}
166+
});
167+
});
168+
}
169+
});

0 commit comments

Comments
 (0)