https://github.com/ti-nspire/AVR/tree/master/bossButton2
pyserialモジュールでシリアル通信をする。webbrowserモジュールでWebページを開く。 -ではポケコンからpyserialへ向けてバイトデータを送り込んだが、今度はAVRから同じことをしてみる。
3つのタクトスイッチに応じて各地のアメダスのページを開く。
youtu.be
#include <avr/io.h> #include <util/delay.h> extern "C" { #include "USART.h" } /* PB0から1秒間Hを出力するための函数。*/ //static inline void blinkLED() { void blinkLED() { DDRB = (1 << PB0); // IOをOUTにして、 PORTB = (1 << PB0); // Hを出力して、 _delay_ms(1000); // そのまま1秒待って、 PORTB &= ~(1 << PB0); // Lを出力する。 } int main(void) { // PD2~4を内部プルアップし、そこにタクトスイッチをつないでディジタル入力にする。 PORTD |= ((1 << PD2) | (1 << PD3) | (1 << PD4)); // 準備ができた確認としてLEDを1秒間点灯する。 blinkLED(); // シリアルインターフェイスを初期化する。 initUSART(); // シリアルインターフェイスの動作確認としてpyserialに向けて何かを送信する。 transmitByte('0'); while(1) { // 押されたボタンに応じてpyserialに向けて何かを送信する。 // blinkLED()で1秒遅延するのでチャタリング防止は不要。 if (bit_is_clear(PIND, PD2)) {transmitByte('y'); blinkLED();} else if(bit_is_clear(PIND, PD3)) {transmitByte('t'); blinkLED();} else if(bit_is_clear(PIND, PD4)) {transmitByte('h'); blinkLED();} } return 0; }
import serial import webbrowser Amedases = {"0":"https://www.google.com/advanced_search", "y":"https://www.jma.go.jp/jp/amedas_h/today-46106.html?areaCode=000&groupCode=32", # 横浜 "t":"https://www.jma.go.jp/jp/amedas_h/today-46141.html?areaCode=000&groupCode=32", # 辻堂 "h":"https://www.jma.go.jp/jp/amedas_h/today-46136.html?areaCode=000&groupCode=32", # 平塚 } sp = serial.Serial("COM12", 9600, timeout = 2) sp.flush() # シリアルバッファーをクリアする。しなくてもよい。 while(1): # .read(1)でシリアルポートから1バイトをバイトデータとして読み出して文字列型に変換する。 location = sp.read(1).decode() if location: print("%s received." % location) webbrowser.open(Amedases[location]) else: print("Got nothing. Still waiting.")
ここまででChapter 6 Digital Input が概ね終わった。