#author("2018-06-24T16:51:20+09:00","default:aiy_gadget","aiy_gadget")
- https://www.slideshare.net/takashiyamanoue/aiy-voice-kit-neopixels
- https://www.youtube.com/watch?v=XgzDTKNBak4&t=14s

#code(python){{
#!/usr/bin/env python3
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A demo of the Google CloudSpeech recognizer."""

import aiy.audio
import aiy.cloudspeech
import aiy.voicehat
import serial
import time

def main():
recognizer = aiy.cloudspeech.get_recognizer()
recognizer.expect_phrase('turn off the light')
recognizer.expect_phrase('turn on the light')
recognizer.expect_phrase('blink')
recognizer.expect_phrase('red')
recognizer.expect_phrase('green')
recognizer.expect_phrase('blue')
recognizer.expect_phrase('white')
recognizer.expect_phrase('yellow')
recognizer.expect_phrase('magenta')
recognizer.expect_phrase('rainbow')
recognizer.expect_phrase('cyan')
recognizer.expect_phrase('left')
recognizer.expect_phrase('right')
recognizer.expect_phrase('stop')
recognizer.expect_phrase('turn on the tape')
recognizer.expect_phrase('turn off the tape')

button = aiy.voicehat.get_button()
led = aiy.voicehat.get_led()
aiy.audio.get_recorder().start()
ser=serial.Serial('/dev/ttyUSB0',9600)
time.sleep(2)
aiy.audio.say('Hello, I\'m listening.')

while True:
# print('Press the button and speak')
# button.wait_for_press()
print('Listening...')
text = recognizer.recognize()
if not text:
print('Sorry, I did not hear you.')
aiy.audio.say('Sorry, I did not hear you.')
else:
print('You said "', text, '"')
aiy.audio.say('you said '+text+'.')
if 'turn on the light' in text:
led.set_state(aiy.voicehat.LED.ON)
elif 'turn off the light' in text:
led.set_state(aiy.voicehat.LED.OFF)
elif 'blink' in text:
led.set_state(aiy.voicehat.LED.BLINK)
elif 'red' in text:
ser.write(b"red;")
elif 'green' in text:
ser.write(b"green;")
elif 'blue' in text:
ser.write(b"blue;")
elif 'yellow' in text:
ser.write(b"yellow;")
elif 'cyan' in text:
ser.write(b"cyan;")
elif 'magenta' in text:
ser.write(b"magenta;")
elif 'White' in text:
ser.write(b"white;")
elif 'rainbow' in text:
ser.write(b"rainbow;")
elif 'left' in text:
ser.write(b"left;")
elif 'right' in text:
ser.write(b"right;")
elif 'stop' in text:
ser.write(b"stop;")
elif 'turn on the tape' in text:
ser.write(b"on;")
elif 'turn off the tape' in text:
ser.write(b"off;")
elif 'goodbye' in text:
break

if __name__ == '__main__':
main()
}}

#code(c){{
include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.

byte val=0;
int led=13;
int np=30;
uint32_t color=strip.Color(0,0,0); 
int lr=0;
uint32_t pcolor=strip.Color(0,0,0);

uint32_t pattern[30];
uint32_t originalPattern[30]
={0,0,1,1,1,0,0,1,1,1,
0,0,1,1,1,0,0,1,1,1,
0,0,1,1,1,0,0,1,1,1};
uint32_t prePattern[30];
int rbp=0; //rainbow position

void setup() {
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code

pinMode(led,OUTPUT);
Serial.begin(9600);

strip.begin();
strip.show(); // Initialize all pixels to 'off'

for(int i=0;i<strip.numPixels();i++){
pattern[i]=originalPattern[i];
}
}

void loop() {
if(Serial.available()>0){
String str = Serial.readStringUntil(';');
Serial.print(val); //for debug, echo back
if(str.indexOf("original")==0){
lr=0;
color=strip.Color(255,0,0,0);
setColor2pattern(color,originalPattern);
}
else if(str.indexOf("rainbow")==0){
setRainbowPattern();
}
else if(str.indexOf("red")==0){
color=strip.Color(255,0,0);
setColor2pattern(color,originalPattern);
}
else if(str.indexOf("green")==0){
color=strip.Color(0,255,0);
setColor2pattern(color,originalPattern);
}
else if(str.indexOf("blue")==0){
color=strip.Color(0,0,255);
setColor2pattern(color,pattern);
}
else if(str.indexOf("yellow")==0){
color=strip.Color(255,255,0);
setColor2pattern(color,pattern);
}
else if(str.indexOf("magenta")==0){
color=strip.Color(255,0,255);
setColor2pattern(color,pattern);
}
else if(str.indexOf("cyan")==0){
color=strip.Color(0,255,255);
setColor2pattern(color,pattern);
}
else if(str.indexOf("white")==0){
color=strip.Color(255,255,255);
setColor2pattern(color,originalPattern);
}
else if(str.indexOf("left")==0){
// Some example procedures showing how to display to the pixels:
lr=-1;
}
else if(str.indexOf("right")==0){
lr=1;
}
else if(str.indexOf("stop")==0){
lr=0;
}
else if(str.indexOf("off")==0){
for(int i=0;i<strip.numPixels();i++){
prePattern[i]=pattern[i];
}
for(int i=0;i<strip.numPixels();i++){
pattern[i]=strip.Color(0,0,0);
}
}
else if(str.indexOf("on")==0){
for(int i=0;i<strip.numPixels();i++){
pattern[i]=prePattern[i];
}
}
}
showCurrent();
}

void setColor2pattern(uint32_t c, uint32_t op[]){
for(int i=0;i<strip.numPixels();i++){
if(op[i]==0){
pattern[i]=0;
}
else{
pattern[i]=c;
}
}
}

void showCurrent() {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, pattern[i]);
}
strip.show();
delay(50);
if(lr<0){
uint32_t xc=pattern[0];
for(int i=0;i<strip.numPixels()-1;i++){
pattern[i]=pattern[i+1];
}
pattern[strip.numPixels()-1]=xc;
}
else if(lr>0){
uint32_t xc=pattern[strip.numPixels()-1];
for(int i=0;i<strip.numPixels()-1; i++){
pattern[strip.numPixels()-i-1]=pattern[strip.numPixels()-i-2];
}
pattern[0]=xc;
}
if(rbp>=255){
rbp=0;
}
else{
rbp++;
}
}

// turn off
void turnOff(uint8_t wait){
for(uint16_t i=0; i<strip.numPixels(); i++) {
uint16_t c=strip.Color(0,0,0);
strip.setPixelColor(i,c);
}
strip.show();
for(uint16_t i=0; i<strip.numPixels(); i++) {
delay(wait);
}
}

void rainbow(uint8_t wait) {
uint16_t i, j;

for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}

void setRainbowPattern(){
for(int i=0;i<strip.numPixels(); i++){
// strip.setPixelColor(i,Wheel(((i*256/strip.numPixels())+rbp) & 255));
pattern[i]=Wheel(((i*256/strip.numPixels())+rbp) & 255);
}
}

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

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

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();

delay(wait);

for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
strip.show();

delay(wait);

for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}

// 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);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}}


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS