ððŠ Mini Workshop 3: Particle Photon ããã€ã¹ãã©ã€ãã¢ãããã Rainbow/Unicorn ãªã¢ãã€ã«ã¢ããªãäœæãã
ãããžã§ã¯ãã®ãŽãŒã« | NativeScript-Vue ãäœæããŠãPhoton ã®çµã¿èŸŒã¿ LED ããŠãŒã¶ãŒã®éžæã«åºã¥ããŠç°ãªãè²ã§ç¹æ» ããããã«ããŸãã |
---|---|
ãã®ã¯ãŒã¯ã·ã§ããã§åŠã¶ããš | NativeScript ãš Vue.js ã§ã®ã¢ãã€ã«ã¢ããªã®äœãæ¹ãParticle Photon ãã»ããã¢ããããŠæ§æããæ¹æ³ããã㊠2 ã€ãç¹ããŠã¢ããªãããã€ã¹ãå¶åŸ¡ããæ¹æ³ |
å¿ èŠãªããŒã« | Wi-Fi ãžã®ã¢ã¯ã»ã¹ Chrome ã®ãããªã¢ãã³ãã©ãŠã¶ NativeScript Playground ãžã®ã¢ã¯ã»ã¹ âããŒãžã§ã³ç®¡çã®ããã¢ã«ãŠã³ãäœæãæšå¥šããŸããiOS ãŸã㯠Android ã®ã¹ããŒããã©ã³ãš NativeScript PlaygroundãPreview ã¢ããªã®ã€ã³ã¹ããŒã« ãã¬ã€ã°ã©ãŠã³ãçšã® 2 ã€ã® NativeScript ã³ã³ãããªã³ã¢ããªïŒNativeScript Viewer ãš NativeScript PlaygroundïŒAndroid ã®å ŽåïŒ NativeScript Playground ãš NativeScript PreviewãiOS ã®å ŽåïŒ NativeScript Playground ãš NativeScript Preview Particle Photon deviceã |
ãããæé | 1 æé |
ã¢ããªã±ãŒã·ã§ã³ãè©ŠããŠã¿ããã§ããïŒ | Playground ã¢ããªã§ãã®ãªã³ã¯ãéããŸã |
ã€ã³ãããã¯ã·ã§ã³
Photon ãã»ããã¢ãããã
1. Photon ã Wi-Fi ã«æ¥ç¶ããŸãïŒ
ãã®ã¯ãŒã¯ã·ã§ããã«ä»å±ã®ããããé梱ããããçšæããŠããããªãã®ããã€ã¹ã䜿çšããŠãmini-USB ã³ãŒãã Photon ãšã³ã³ãã¥ãŒã¿ãŒã® USB ããŒããŸã㯠USB 察å¿ã®å é»åšã«æ¥ç¶ããŠãããã€ã¹ã«é»åãäŸçµŠããŸãã
- Photon setup page ã«ã¢ã¯ã»ã¹ããŠããã€ã¹ãã»ããã¢ããããŸãããã°ã€ã³ãæ±ããããŸãããå ã«é²ã¿ãParticle ã¢ã«ãŠã³ããäœæããŸã
- ã¢ã«ãŠã³ããäœæãããšãããã€ã¹ã®ã»ããã¢ããã«ãªãã€ã¬ã¯ããããŸãããSetup a Photonããšãnextããã¯ãªãã¯ããŠéå§ããŸãã次ã®ããŒãžã«ãã¹ãŠã®èŠä»¶ãããå Žåã¯ããnextããã¯ãªãã¯ããŸããçæãããããŒã«ã«ãã¡ã€ã«ãã³ã³ãã¥ãŒã¿ãŒã«ããŠã³ããŒãããŸãããã©ãŠã¶ã§
photonsetup.html
ãã¡ã€ã«ãéããŸã - Photon ã®ç®±ã®è£é¢ãèŠãŠã©ã® Photon ããç¹å®ããŸããå·ŠåŽã«ããã€ã¹ã® ID ãèšèŒãããã¹ããã«ãŒããããŸãããã®çªå·ã®æåã® 6 æåã¯ãã³ã³ãã¥ãŒã¿ãŒã«è¡šç€ºãããæåã® Wi-Fi ã¢ãã¬ã¹ãšäžèŽããŸã
- ãªã¹ããããŠããæé ã«åŸã£ãŠ Photon ã® Wi-Fi ãããã¯ãŒã¯ã«æ¥ç¶ããŸããæ¥ç¶ãããšããéããã«ãã«ãããã¯ãã§ãïŒäžå€®ã®éè²ã®ã©ã€ãããã£ãããšç¹æ» ããŸãïŒ
ããã€ã¹ã«ååãã€ããŠã»ããã¢ãããå®äºããŸããããã§ãPhoton ã®ã³ãŒããæžãããã®ç¹å¥ãª IDE ã§ãã Particle Build ã§ããã€ã¹ã䜿çšããããšãã§ããŸãã
ð¡
åå è ã¯ãParticle Build 㧠Photon ãèŠã€ããã®ã«èŠåŽããããšããããŸããIDE ã«è¡šç€ºãããªãå Žåã¯ãæåã§è¿œå ããŠãã ããã
- ããã€ã¹ããŸã éè²ã«ç¹æ» ããŠããªãå Žåã¯ãã¡ã€ã³ã¹ããŒã¿ã¹ LED ãæ¿ãéè²ã«ç¹æ» ãããŸã§ Photon ã®ã»ããã¢ãããã¿ã³ãæŒãç¶ããŸããããã«ã¯çŽ 3 ç§ããããŸãã
- äžèšã®æé ã«åŸã£ãŠ Photon ã® Wi-Fi ã«æ¥ç¶ããWeb ãã©ãŠã¶ã§ http://192.168.0.1/device-id ãéããŸãããã®ããŒãžã«ã¯ã次ã®ãã㪠ID ã衚瀺ãããŸãïŒ
{"id":"30003F000347363339343638","c":"1"}
- ãã® ID ãã¡ã¢ããŠãã ãããããšã§äœ¿çšããŸã
- Particle Build ãéãããAdd New Deviceããã¯ãªãã¯ããŸã
- ã¡ã¢ããŠãããããã€ã¹ ID ãããã¯ã¹ã«å ¥åããŸããããã§ããã€ã¹ã IDE ã§äœ¿çšã§ããããã«ãªããŸã
2. Particle Build ã«ã€ããŠåŠã¶
Photon ã®ã¢ã«ãŠã³ãã¯ãã§ã«äœæããŠããããã°ã€ã³ããŠããã¯ãã§ãïŒãã°ã€ã³ããŠããªãå Žåã¯ããããããã°ã€ã³ããŠãã ããïŒ ãã㧠Particle Build ã«ç§»åã§ããŸããå·Šã®ããŒã®äžéšã«ããäžãã 3 çªç®ã®ã¢ã€ã³ã³ãã¯ãªãã¯ããŠãããã€ã¹ããªã¹ããããŠããããšã確èªããŸããã¹ã¿ãŒãã€ããŠããããšã確èªããŸãïŒã¹ã¿ãŒãã€ããŠããªãå Žåã¯ã¹ã¿ãŒãã¯ãªãã¯ããŸãïŒ âWi-Fi çµç±ã§ãã®ããã€ã¹ã«ã³ãŒãããã©ãã·ã¥ããŸãã
Particle Build ãèŠãŠã¿ãŸããããããã¯ãã©ãŠã¶ããŒã¹ã® IDE ã§ããããã©ãŠã¶ããå·ŠåŽã§éžæãã Photon ããã€ã¹ã«ã³ãŒããããã©ãã·ã¥ããã€ãŸãå ¬éã§ããŸãã
çŸåšããã®æ°ãããã¡ã€ã«ã«ã¯ãsetup() ãš loop() é¢æ°ä»¥å€ã¯äœããããŸããããã®ãã¡ã€ã«ã«ã³ãŒããè¿œå ããŠã¿ãŸãããã
ð¡
Photon ã¯åºæ¬çã« Wi-Fi æ¥ç¶ããã Arduino ã§ãããããArduino éçºã®çµéšãããã°ããã®ã³ãŒãã¯ãªãã¿æ·±ããã®ã«ãªããŸããArduino ã®ãã¹ã±ããããã€ãŸãããŒããŠã§ã¢ããã€ã¹ã«æžã蟌ãŸãããã¡ãŒã ãŠã§ã¢ã¯ãäžè¬çã« C ãŸã㯠C++ ã§æžãããŠããŸããã¯ãã ä»ããäœåãªé ç·ãé¿ããŠãããã€ã¹ã®äžå€®ã® LED ã©ã€ããå¶åŸ¡ããããã® C++ ã³ãŒããå°ãæžããŸãããã ããParticle ãããã«ã¯ããã€ãã®è¿œå æ©èœãä»å±ããŠããã®ã§ãåŸã§ãã£ãšèªç±ã«æ¢ã£ãŠã¿ãŠãã ããïŒ
çŸæç¹ã§ã¯ãPhoton ã¯äžå€®ã® LED ãç¹ç¯ããŠããããã¹ãŠãé 調ã«é²ãã§ããå Žåã¯ããéããã«ãã«ããŠãããããç·ããã£ãéè²ã®ãã«ã¹ãçºããŠããã¯ãã§ããããã€ãã®ã³ãŒãããã©ãã·ã¥ããŠãäžå€®ã®ã©ã€ããçœãããŸãããã
3. ããã€ãã®ã³ãŒãããã©ãã·ã¥ãã
Build IDE ã®å³åŽã«ãããã€ãã®ã³ãŒãè¡ãå«ãé»ããŠã£ã³ããŠã衚瀺ãããŸããloop() é¢æ°ã®æ³¢ãã£ãå ã«ã次ã®ã³ãŒãã貌ãä»ããŸãïŒ
RGB.control(true);
// è²ãå€ãã
RGB.color(RGB_COLOR_WHITE);
RGB.control(false);
2
3
4
ãã®ã³ãŒãã¯ãParticle API ã䜿çšããŠäžå€®ã® RGB LED ã®å¶åŸ¡ãååŸãããã®è²ãå€æŽããŠãããå¶åŸ¡ãæŸæ£ããŸãã
ãã®æ°ãããã¡ã€ã«ã¯ãParticle Build ã§ã¯ãã¢ããªããšèŠãªãããŸããã¢ããªã®ã³ãŒããã¹ã¿ãŒä»ãã®ããã€ã¹ã«ãã©ãã·ã¥ããåã«ãå·ŠåŽã®ããã«ã§ååãä»ããå¿ èŠããããŸãã
Particle Build ã®å·ŠåŽã®ããã²ãŒã·ã§ã³ã¹ããªããã«ãããã©ã«ãã¢ã€ã³ã³ãã¯ãªãã¯ããŠãå€æŽãä¿åããŸãããã®äžã«ãããäžžã§å²ãŸãããã§ãã¯ããŒã¯ãã¯ãªãã¯ããŠãã³ãŒããã確èªãããŸããããã¯åºæ¬çã«ã³ãŒãããã¹ãããŠãã³ã³ãã€ã«ã§ããããšã確èªããŠããŸããæåŸã«ãPhoton ã«ã¹ã¿ãŒãä»ããããŠããããšã確èªããäžéšã®çš²åŠ»ã¢ã€ã³ã³ãã¯ãªãã¯ããŠããã®ã³ãŒããããã€ã¹ã«ãã©ãã·ã¥ããŸããPhoton ãæŽæ°ãããããã€ãã®è²ãæ°ç§éç¹æ» ããåŸãäžå€®ã® LED ãçœè²ã«å€ããã¯ãã§ããã³ãŒããã«ãŒãé¢æ°ã«é 眮ããŠããã®ã§ãçœã®ãŸãŸã§ãã
ãã®ã¹ãããããå°ãããã£ãŠã¿ãŠãã ãããLED ãèµ€ãã§ããŸããïŒ
4. Photon ã³ãŒããå®æããã
ç§ãã¡ã®ã¢ãã€ã«ã¢ããªã§ã¯ãLED ã©ã€ãã® 3 ã€ã®ãã¢ãŒãããäœæããŸã âLED ãã¬ã€ã³ããŒã·ãªãŒãºãç¹æ» ããããã¬ã€ã³ããŒã¢ãŒãããLED ãéãšçœã亀äºã«ç¹æ» ãããããŠãã³ãŒã³ã¢ãŒããããããŠãåæ¢ãã¢ãŒã㧠LED ãçœè²ã«çãŸããŸãã
ãæ³åã®ãšããããããã®ã«ãŒãã³ã loop() é¢æ°ã«é 眮ããŸãã
ãã®ãã¡ã€ã«ã®å é ã«å€æ°ãè¿œå ããåæã®ã©ã€ãã¹ã¿ã€ã«ã 0 ã«ãªã¹ãããŸãïŒ
int style = 0;
次ã«ã loop() é¢æ°ã§ãè¿œå ããã¹ããããã次ã®ã³ãŒãã§äžæžãããŸãïŒ
if (style == 0) {
RGB.control(true);
// è²ãå€ãã
RGB.color(RGB_COLOR_WHITE);
RGB.control(false);
} else if (style == 1) {
RGB.control(true);
// è²ãå€ãã
RGB.color(RGB_COLOR_RED);
// 1ç§é
ããã
delay(500);
RGB.color(RGB_COLOR_ORANGE);
delay(500);
RGB.color(RGB_COLOR_YELLOW);
delay(500);
RGB.color(RGB_COLOR_GREEN);
delay(500);
RGB.color(RGB_COLOR_BLUE);
delay(500);
RGB.color(RGB_COLOR_MAGENTA);
delay(500);
// éåžžã®æäœãåéãã
RGB.control(false);
} else if (style == 2) {
RGB.control(true);
// è²ãå€ãã
RGB.color(RGB_COLOR_WHITE);
// 1ç§é
ããã
delay(500);
RGB.color(RGB_COLOR_MAGENTA);
delay(500);
// éåžžã®æäœãåéãã
RGB.control(false);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
ãã®ã³ãŒããæ€èšŒããããã€ã¹ã«ãã©ãã·ã¥ããŸããäœãå€æŽãããŠããªãããã§ã âã¹ã¿ã€ã«ã 0 ã«èšå®ãããŠããããããŸã çœããŸãŸã§ããæŽæ°å€ã 1 ã«å€æŽããŠãããã€ã¹ã«ãã©ãã·ã¥ããäœãèµ·ããããèŠãŠã¿ãŸããããRainbow ãèŠããŸããïŒ
ããã§ã¹ã¿ã€ã«ã 0 ã«æ»ãããšãã§ããã®ã§ãLED ã¯çœã§åæåãããŸãïŒ
int style = 0;
5. ã¢ãã€ã«ã¢ããªã§ Photon ã® REST API ã䜿çšããæºåããã
ã§ã¯ãPhoton ã®ã¯ãŒã«ãª REST API æ©èœã䜿çšããŠãã¢ãã€ã«ã¢ããªã Photon ãå¶åŸ¡ã§ããããã«ããå¿ èŠããããŸãããããè¡ãã«ã¯ã次ã®è¡ãäžãã£ãã®éã«ãã setup() é¢æ°ã«è¿œå ããŸãïŒ
Particle.function("launchMode",launchMode);
ã¢ãã€ã«ã¢ããªããå©çšã§ãã launchMode
ãšããååã® Particle é¢æ°ãè¿œå ããŸãããããã¯ãPhoton ã®ã³ãŒãã§åæ§ã®ååã®é¢æ°ãåŒã³åºããŸãã
loop() é¢æ°ã®æåŸã«ãã®é¢æ°ãè¿œå ããŸãïŒ
int launchMode(String mode) {
style = 0;
if(mode=="rainbow"){
style = 1;
}else if(mode=="unicorn"){
style = 2;
}
}
2
3
4
5
6
7
8
9
ããã§ãã¢ãã€ã«ã¢ããªã¯å
éšã® launchMode
é¢æ°ãåŒã³åºã launchMode
ãšãã Particle é¢æ°ãåŒã³åºãããšãã§ããLED ãã©ãã·ã¥ã¹ã¿ã€ã«ã®å¶åŸ¡ã«äœ¿çšããã¢ãŒãã瀺ãæååãæž¡ããŸãã
ä¿åããŠç¢ºèªãããã®ã³ãŒããããã€ã¹ã«ãã©ãã·ã¥ããŸããLED ã¯çœã«æ»ããŸãã
6. æçµç㪠Photon ã³ãŒã
æçµçãªã³ãŒãã¯æ¬¡ã®ããã«ãªããŸãïŒ
int style = 0;
void setup(){
Particle.function("launchMode",launchMode);
}
void loop(){
if(style == 0){
RGB.control(true);
// è²ãå€ãã
RGB.color(RGB_COLOR_WHITE);
RGB.control(false);
}
else if (style == 1){
RGB.control(true);
// è²ãå€ãã
RGB.color(RGB_COLOR_RED);
// 1ç§é
ããã
delay(500);
RGB.color(RGB_COLOR_ORANGE);
delay(500);
RGB.color(RGB_COLOR_YELLOW);
delay(500);
RGB.color(RGB_COLOR_GREEN);
delay(500);
RGB.color(RGB_COLOR_BLUE);
delay(500);
RGB.color(RGB_COLOR_MAGENTA);
delay(500);
// éåžžã®æäœãåéãã
RGB.control(false);
}
else if (style == 2){
RGB.control(true);
// è²ãå€ãã
RGB.color(RGB_COLOR_WHITE);
// 1ç§é
ããã
delay(500);
RGB.color(RGB_COLOR_MAGENTA);
delay(500);
// éåžžã®æäœãåéãã
RGB.control(false);
}
}
int launchMode(String mode) {
style = 0;
if (mode=="rainbow") {
style = 1;
} else if (mode=="unicorn") {
style = 2;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
ã¢ããªã Scaffold ãã
次ã«ãJavaScript ã䜿çšããŠãã€ãã£ããªã¢ãã€ã«ã¢ããªãäœæããŸããNativeScript ã䜿çšããŠã¢ããªãäœæããŸãããã®ã¢ããªã¯ãå ã»ã©ãã©ãã·ã¥ããã³ãŒããä»ã㊠Photon ãšéä¿¡ããŸãã
NativeScript Playground ãéããŠã¿ãŠãã ãããæåã®èšªåã§ã¯ãããã€ãã®ãã³ãŒãããŒã¯ãã衚瀺ãããäž»èŠãªæ©èœãããå Žæã瀺ãããŸãã
ãPlay Nowããã¯ãªãã¯ããŠãã¡ã€ã³ãšãã£ã¿ãéããŸããQR ã³ãŒãã衚瀺ãããŸã â NativeScriptPlay ã¢ããªã§ã¹ãã£ã³ããŠãã ãããããã«ãããã³ãŒãã£ã³ã°æã«ã¹ããŒããã©ã³ãèªåçã«æŽæ°ã§ããŸããããã§ãNativeScript-Vue ã¢ããªã Scaffold ããæºåãã§ããŸããïŒ
ããã©ã«ãã§ã¯ãæåã®ãã¬ã€ã°ã©ãŠã³ãã¢ããªã¯ Angular ã䜿çšããŠäœæãããŸããäžéšã®ãnewããã¯ãªãã¯ãããã³ãã¬ãŒããšããŠãNS-VueããéžæããŸãã å¥ã® QR ã³ãŒããååŸããã®ã§ããã®ã³ãŒããã¹ãã£ã³ããŠãã ãããã¹ããŒããã©ã³ãæŽæ°ãããåºæ¬ã®ã¢ããªã衚瀺ãããŸãïŒ
ããŒã¹ã®ããŒã¯ã¢ãããäœæãã
template
ã§å°ãã ã XML ããŒã¯ã¢ããã䜿ã£ãŠãActionBarãããã²ãŒã·ã§ã³çšã® SegmentedBarããããŠã¿ããããããšå€åããããã«ãåãã SegmentedBar ã®ã·ã³ãã«ãªã¬ã€ã¢ãŠããæ§ç¯ããŸãã
ð¡
NativeScript ã¬ã€ã¢ãŠãã¯ãWeb ã§äœæãã HTML ãšã¯ç°ãªããŸããGridLayouts ã StackLayouts ãªã©ã®ãã€ãã£ã UI ã«å€æããã NativeScript XML ããŒã¯ã¢ããã䜿çšããŠããŸãã
app/components/HelloWorld.vue
ãéãããã³ãã¬ãŒãã® <Page>
ã¿ã°ã®éã®ãã¹ãŠãåé€ããŠã以äžãè¿œå ããŸãïŒ
<ActionBar class="action-bar" title="Awesome Photons!" />
<StackLayout>
<SegmentedBar
color="white"
backgroundColor="blueviolet"
selectedBackgroundColor="hotpink"
class="bar"
:items="segmentedBarItems"
v-model="selectedBarIndex"
/>
<StackLayout height="100%" :class="mode"></StackLayout>
</StackLayout>
2
3
4
5
6
7
8
9
10
11
12
ãã€ã³ããããå€ :items
ãš v-model
ããŸã èšå®ãããŠããªããããSegmentedBar ã¯ãŸã 衚瀺ãããŠããŸãããããŒã¿ãããã¯ïŒãã³ãã¬ãŒããããã¯ã®äžãã¹ã¯ãªãããããã¯å
ïŒãèŠã€ãã data()
ã¡ãœãããäžæžãããŠãSegmentedBar ã®å€ãè¿œå ããŸãããŸããããã©ã«ãã® SelectedBarIndex ãèšå®ãã mode
ã®ãã¬ãŒã¹ãã«ããŒå€ãäœæããŸããåŸè
ã¯ãããŒãã¿ããããããšãã« UI ãå€æŽããããã«äœ¿ãããŸãïŒ
data() {
return {
segmentedBarItems: function () {
var segmentedBarModule = require("ui/segmented-bar");
let rainbow = new segmentedBarModule.SegmentedBarItem();
rainbow.title = "Rainbows";
let unicorn = new segmentedBarModule.SegmentedBarItem();
unicorn.title = "Unicorns";
let stop = new segmentedBarModule.SegmentedBarItem();
stop.title = "Stop!";
return [rainbow, unicorn, stop];
}(),
selectedBarIndex: 2,
mode: ''
};
},
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ããã§ãSegmentedBar ãããŸã衚瀺ãããã¯ãã§ãïŒ
CSS ãè¿œå ãã
NativeScript ã¯ãUI ã®ã¹ã¿ã€ã«ãèšå®ããããã«ãéåžžã® CSS ããã³ SASS ã®ãµãã»ããããµããŒãããŠããŸããããã掻çšã㊠UI ãæ¹åããŸãããã
<script>
ã¿ã°ã®äžã«ã <style>
ã¿ã°ããããŸãããã®ãããã¯ã® 2 ã€ã®ã¹ã¿ã€ã«ã次ã®ããã«äžæžãããŸãïŒ
.action-bar {
color: white;
background-color: blueviolet;
}
.bar {
margin: 20;
}
.rainbow {
background: linear-gradient(
to bottom,
red,
orange,
yellow,
green,
blue,
purple
);
}
.unicorn {
background-image: url("~/images/unicorn.jpg");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ãã®ãããžã§ã¯ãã«ç»åãè¿œå ããå¿ èŠããããŸãããã®ç»åã¯ãUnicorn SegmentedBarItem ãã¿ããããããšãã«è¡šç€ºãããŸãã
ãExplorerããšããåèªã®æšªã«ããå°ããªã+ããã¯ãªãã¯ãããimagesããšããã¿ã€ãã«ã®ãã©ã«ããè¿œå ããŸãã以äžã®ãã¡ã€ã«ãããŒã«ã«ã«ä¿åããã.jpgããã¡ã€ã«ãšããŠã¢ããããŒãããããããããŠã³ã§ãUpload resourcesããŠãŒãã£ãªãã£ã䜿çšããŸãã
ð¡
ãã¡ã€ã«åã unicorn.jpg
ã«å€æŽããã¢ããããŒãåŸã«ä¿åããŠãã ããã
ã¡ãœããã®äœæïŒ1ïŒ
Rainbows
ãUnicorns
ããŸã㯠Stop!
ãã¿ãããããã©ããã«å¿ããŠãUI ãå€æŽããŸãã
SegmentedBar ã®åäœã管çããããã«ã data
ã¡ãœããã®æåŸã®ã³ã³ãã®ããšïŒãŸã ãªãå Žåã¯ã³ã³ããè¿œå ããŸãïŒã« methods
ãããã¯ãäœæããŸãïŒ
methods: {
onSelectedIndexChange(args) {
let segmentedBarIndex = args.object.selectedIndex;
if (segmentedBarIndex == 0) {
this.mode = 'rainbow'
//this.launchMode(this.mode);
}
else if (segmentedBarIndex == 1) {
this.mode = 'unicorn'
//this.launchMode(this.mode);
}
else {
this.mode = 'stop'
//this.launchMode(this.mode);
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ð¡
貌ãä»ã㧠JavaScript ãè¿œå ãããšãNativeScript Playground ããã¡ããã¡ãã«ãªãå ŽåããããŸããä¿®æ£ããã«ã¯ã <command><X>
ïŒåãåãïŒãæŒããŸãã
次ã«ããã®ã¡ãœãããç£èŠããããã« <SegmentedBar>
èªäœãç·šéããŸãã@selectedIndexChange ã <template>
ã® <SegmentedBar>
ããŒã¯ã¢ããã«è¿œå ããŸãïŒ
@selectedIndexChange="onSelectedIndexChange($event)"
å®æãããã³ãã¬ãŒããããã¯ã¯æ¬¡ã®ããã«ãªããŸãïŒ
<template>
<Page>
<ActionBar class="action-bar" title="Awesome Photons!" />
<StackLayout>
<SegmentedBar
color="white"
backgroundColor="blueviolet"
selectedBackgroundColor="hotpink"
class="bar"
:items="segmentedBarItems"
v-model="selectedBarIndex"
@selectedIndexChange="onSelectedIndexChange($event)"
/>
<StackLayout height="100%" :class="mode"></StackLayout>
</StackLayout>
</Page>
</template>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ããã§ãSegmentedBar ãã¿ãããããšãUI ãå€æŽãããŸãã
Photon ãæ¥ç¶ãã
ããã§ãSegmentedBar ãã¿ãããããšãã« Photon ãç¹ç¯ããããã«ãªããŸããã
ãã¬ã€ã°ã©ãŠã³ãã® <script>
ã®ããäžã§ãããã€ãã® const
ã®å€ãèšå®ããŸãã
const http = require("http");
const baseUrl = "https://api.particle.io/v1/devices/xxxxx";
const token = "yyyyy";
const endpoint = "launchMode";
2
3
4
ããããèŠãŠãããŸãããããŸããParticle ã® REST API ã«å¯Ÿã㊠http åŒã³åºããè¡ãå¿
èŠãããããã http
ã¢ãžã¥ãŒã«ãå¿
èŠã§ãã
次ã«ãREST API ã«ã©ã®ããã€ã¹ãåŒã³åºããŠããŒã¯ã³ãéä¿¡ããããéç¥ããå¿ èŠããããŸãããã㧠2 ã€ã®å€ãå€æŽããå¿ èŠããããŸããParticle Build IDE ã«æ»ããäžãã 3 çªç®ã®ã¢ã€ã³ã³ãã¯ãªãã¯ããŸãïŒã¿ãŒã²ããã®ããã«èŠããŸãïŒãããã€ã¹ ID ã¯æ¬¡ã®ãšããã§ãïŒ
ãã®å€ãã³ããŒããbaseUrl const ã® xxxxx
ã®å€ãããã€ã¹ ID ã«å€æŽããŸãã
次ã«ãREST API èªäœã䜿çšããããã®ããŒã¯ã³ãå¿
èŠã§ããParticle Build ã®äžéšã«ãããæ¯è»ãã¢ã€ã³ã³ãã¯ãªãã¯ããã¢ã¯ã»ã¹ããŒã¯ã³ã®å€ãã³ããŒããŠã yyyyy
ãããŒã¯ã³ã«çœ®ããããŸãã
è¿œå ããæåŸã®å€ã¯ãåŒã³åºããšã³ããã€ã³ãã®ååã§ããäžã§èšå®ãã Particle é¢æ°ãèŠããŠããŸããïŒããã¯ã¢ãã€ã«ã¢ããªã§åŒã³åºãæ¹æ³ã§ãã
ã¡ãœããã®äœæïŒ2ïŒ
onSelectedIndexChange
åŒã³åºãã®éããã£ãã®åŸã«ã³ã³ããè¿œå ããREST API ãåŒã³åºãã¡ãœãããè¿œå ããŠãPhoton ã®è²ãå€æŽããŸãïŒ
launchMode(mode) {
http.request({
url: `${baseUrl}/${endpoint}?access_token=${token}`,
method: "POST",
headers: { 'content-type': 'application/json' },
content: JSON.stringify({
mode: mode
})
}).then((response) => {
console.log(JSON.stringify(response))
}, (e) => {
alert("error")
});
}
2
3
4
5
6
7
8
9
10
11
12
13
14
ããã§ã¯ãParticle é¢æ°ãšããŠèšå®ãããšã³ããã€ã³ã㧠baseUrl ã« http ãªã¯ãšã¹ããéä¿¡ããæååãæž¡ããŠããã©ãã·ã¥ããã«ã©ãŒã·ãŒã±ã³ã¹ãé¢æ°ã«äŒããŠããŸãã
æåŸã«ãSegmentedBar ãã¿ããããããšãã«å®éã« launchMode
ãåŒã³åºãå¿
èŠããããŸãã onSelectedIndexChange
ã¡ãœããã® // this.launchMode(this.mode)
ã³ã¡ã³ãã³ãŒããå€ããŠãã ããã
HelloWorld.vue ãã¡ã€ã«å šäœã¯æ¬¡ã®ããã«ãªããå人çšããã€ã¹ã® ID ãšããŒã¯ã³ãå ¥åãããŸãïŒ
<template>
<Page>
<ActionBar class="action-bar" title="Awesome Photons!" />
<StackLayout>
<SegmentedBar color="white" backgroundColor="blueviolet"
selectedBackgroundColor="hotpink" class="bar"
:items="segmentedBarItems" v-model="selectedBarIndex"
@selectedIndexChange="onSelectedIndexChange($event)" />
<StackLayout height="100%" :class="mode"></StackLayout>
</StackLayout>
</Page>
</template>
<script>
const http = require("http");
const baseUrl =
"https://api.particle.io/v1/devices/<your device id>";
const token = "<your token>";
const endpoint = "launchMode";
export default {
data() {
return {
segmentedBarItems: (function() {
var segmentedBarModule = require(
"ui/segmented-bar");
let rainbow = new segmentedBarModule
.SegmentedBarItem();
rainbow.title = "Rainbows";
let unicorn = new segmentedBarModule
.SegmentedBarItem();
unicorn.title = "Unicorns";
let stop = new segmentedBarModule
.SegmentedBarItem();
stop.title = "Stop!";
return [rainbow, unicorn, stop];
})(),
selectedBarIndex: 2,
mode: ""
};
},
methods: {
onSelectedIndexChange(args) {
let segmentedBarIndex = args.object.selectedIndex;
if (segmentedBarIndex == 0) {
this.mode = "rainbow";
this.launchMode(this.mode);
} else if (segmentedBarIndex == 1) {
this.mode = "unicorn";
this.launchMode(this.mode);
} else {
this.mode = "stop";
this.launchMode(this.mode);
}
},
launchMode(mode) {
http.request({
url: `${baseUrl}/${endpoint}?access_token=${token}`,
method: "POST",
headers: {
"content-type": "application/json"
},
content: JSON.stringify({
mode: mode
})
}).then(
response => {
console.log(JSON.stringify(response));
},
e => {
alert("error");
}
);
}
}
};
</script>
<style scoped>
.action-bar {
color: white;
background-color: blueviolet;
}
.bar {
margin: 20;
}
.rainbow {
background: linear-gradient(to bottom, red, orange, yellow, green, blue, purple);
}
.unicorn {
background-image: url('~/images/unicorn.jpg');
background-repeat: no-repeat;
background-size: cover;
background-position: center;
}
</style>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
ããã§ãæ£ããã³ãŒãããã©ãã·ã¥ãããç¶æ 㧠Photon ã Wi-Fi ã«æ¥ç¶ãããŠããå Žåãã¢ãã€ã«ã¢ããªã§ã®ãžã§ã¹ãã£ã Photon ã®äžå€®ã® LED ãç¹ç¯ããŠããã®ã確èªã§ããŸãïŒ
NativeScript Playground ã§ã¢ãã€ã«ããã€ã¹çšã® Vue.js ã¢ããªãäœæããããšã¯æ¬åœã«èå³æ·±ãã§ããããæ¹ãããã£ãã®ã§ãä»ã«ã©ããªãã®ãäœããŸããïŒãã®ã¢ããªãæ¡åŒµããŠãããŸããŸãªç §æã¢ãŒããäœæã§ããã®ã§ã¯ãªãã§ããããïŒ
Author
Made with ð±â€ïž by Jen Looper