2016年1月9日 星期六

[穿戴科技] Adafruit NeoPixel 學習

官方教材:https://learn.adafruit.com/getting-started-with-flora/blink-onboard-neopixel

記得要裝函式庫,可以選擇草稿碼(Sketch)->滙入函式庫(Include Library)->管理函式庫...(Manage Libaries...)安裝 Adafruit_NeoPixel

在官方網站上有程式碼可以複製,程式碼如下:

#include < adafruit_neopixel .h>;

#define PIN 8

Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  // Some example procedures showing how to display to the pixels:
  colorWipe(strip.Color(255, 0, 0), 500); // Red
  colorWipe(strip.Color(0, 255, 0), 500); // Green
  colorWipe(strip.Color(0, 0, 255), 500); // Blue
  rainbowCycle(20);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256 5="" all="" b="" colors="" cycles="" j="" of="" on="" wheel="">
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if(WheelPos < 170) {
    WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }

}

接下來我們來解釋程式碼的內容

首先我們會看到#include指令。
#include ;
我們使用Google查一下Adafruit_NeoPixel.h,可以發現https://github.com/adafruit/Adafruit_NeoPixel,該專案中可以發現Adafruit_NeoPixel.h和Adafruit_NeoPixel.cpp原始碼,有興趣的是可以看到esp8266.c,很有趣吧!經由敏哥追蹤程式碼,我們發現在Adafruit_NeoPixel.cpp檔案中,可以發現有使用到esp8266.c中的函式espShow()。

#elif defined(ESP8266)

// ESP8266 ----------------------------------------------------------------

  // ESP8266 show() is external to enforce ICACHE_RAM_ATTR execution
  espShow(pin, pixels, numBytes, is800KHz);


#endif // ESP8266

接下來則是使用#define來定義使用到的腳位。

#define PIN 8

隨著物件導向(OO)程式設計廣泛受到大家重視,就連微電腦控制也採用OO的設計方式,不過在建立新物件時,並不需要像C++一樣使用到new關鍵字,Adafruit_NeoPixel就是類別,其建構子有3個參數,分別為LED的數量、硬體連接的腳位、以及LED的型別。

Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, PIN, NEO_GRB + NEO_KHZ800);

以上例來說,只有1個LED,連接在第8支腳,採用GRB,800KHZ通訊訊號的速率。
為何LED跟通訊訊號的速率有關,因為它採用串接方式,當送出10顆LED命令時,接收到的第一顆LED會取下其命令,然後把9個命令再往下送,以此類推。下圖是從https://www.adafruit.com/category/168取得。




接下來則是setup函式,直接呼叫strip物件中,begin函式以及show函式。
void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

loop函式裏呼叫2個函式,其一為colorWipe和rainbowCycle兩個函式,前者是固定顏色的顯示,後者則是以顏色變化來顯示。 void loop() {
  // Some example procedures showing how to display to the pixels:
  colorWipe(strip.Color(255, 0, 0), 500); // Red
  colorWipe(strip.Color(0, 255, 0), 500); // Green
  colorWipe(strip.Color(0, 0, 255), 500); // Blue
  rainbowCycle(20);
}
colorWipe第一個參數為顏色值,第二個為延遲的時間。
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256 5="" all="" b="" colors="" cycles="" j="" of="" on="" wheel="">
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if(WheelPos < 170) {
    WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }

}

2016年1月2日 星期六

[穿戴科技] Adafruit FLORA初接觸

Adafruit出一款大約美金20美元的穿戴科技模組,我們稱它為 FLORA,讓晶片相容於開放式硬體Adruino。
在網站https://www.adafruit.com/products/659有詳細的說明,FLORA體積很小和重量很輕,直徑1.75“,重量約4.4克。對FLORA系列而言,擁有配件包括:最好的不銹鋼線、感知器、GPS模塊和可鏈接的LED NeoPixels。2015年5月12日開賣的FLORA V2是採用micro-USB介面。




初學者的教材在https://learn.adafruit.com/getting-started-with-flora Arduino IDE版本至少要在 1.6
(含)以上。
軟體的安裝在https://learn.adafruit.com/adafruit-arduino-ide-setup/arduino-1-dot-6-x-ide。要在Arduino IDE中使用Adafruit FLORA,您必須在"檔案->偏好設定"功能選項,打開偏好設定的功能表,並在額外的板子管理員網址中輸入:https://adafruit.github.io/arduino-board-index/package_adafruit_index.json字串。
另外也要記得裝FLORA的驅動,可參考https://learn.adafruit.com/adafruit-arduino-ide-setup/windows-setup

接下來就可以打開Arduino IDE,選擇範例檔Blink。

其程式碼如下:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at http://www.arduino.cc

  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}

因為板子內建LED在D7,所以必須把上面範例中腳位13改成腳位7。

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(7, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(7, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(7, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second
}