Shownet2017

Dependencies:   SakuraIO mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SakuraIO.h"
00003 
00004 #define SEND_INTERVAL 5 // 送信間隔 秒単位
00005 
00006 PwmOut iine_led(PB_4);
00007 DigitalIn iine_button(PB_3);
00008 DigitalOut status_led(PB_5);
00009 
00010 I2C i2c(PB_7, PB_6); // sda, scl
00011 SakuraIO_I2C sakuraio(i2c);
00012 
00013 uint64_t counter = 0;   // 死亡検知用カウンタ
00014 uint32_t iine_count = 0; // 押された回数
00015 
00016 Ticker tick;
00017 
00018 void timer()
00019 {
00020     // 1秒間隔で呼び出される
00021     static int sec_count = 1;
00022     __disable_irq();
00023 
00024     if (sec_count >= SEND_INTERVAL)
00025     {
00026         // SEND_INTERVAL秒に1回iine_countを集計して方針
00027         sec_count = 0;
00028         sakuraio.clearTx();
00029         sakuraio.enqueueTx(0, counter);
00030         sakuraio.enqueueTx(1, iine_count);
00031         sakuraio.send();
00032         counter++;
00033         iine_count = 0;
00034     }
00035     sec_count++;
00036     __enable_irq();
00037 }
00038 
00039 int main()
00040 {
00041     int led_counter = 0;
00042     iine_button.mode(PullUp);
00043 
00044     // 通信モジュールリンクアップ待ち
00045     for (;;)
00046     {
00047         iine_led = 1;
00048 
00049         for (float p = 0.0f; p < 1.0f; p += 0.01f)
00050         {
00051             iine_led = p;
00052             wait(0.01);
00053         }
00054         for (float p = 1.0f; p > 0.0f; p -= 0.01f)
00055         {
00056             iine_led = p;
00057             wait(0.01);
00058         }
00059         iine_led = 0;
00060         if ((sakuraio.getConnectionStatus() & 0x80) == 0x80)
00061             break;
00062         wait(1);
00063     }
00064 
00065     // リンクアップしたよアピール
00066     iine_led = 1;
00067     wait(1);
00068     iine_led = 0;
00069     wait(1);
00070     iine_led = 1;
00071 
00072     // 1秒間隔のタイマー作成
00073     tick.attach(&timer, 1);
00074 
00075     for (;;)
00076     {
00077         if (iine_button == 0)
00078         {
00079             // ボタンが押されたらカウントアップしてから派手に点滅
00080             wait(0.5);
00081             while (iine_button == 0) // ボタンを放すまでブロック
00082                 ;
00083             iine_count++; // いいね回数カウントアップ
00084 
00085             // 以下派手に点滅
00086             iine_led = 1;
00087             for (int i = 0; i < 6; i++)
00088             {
00089                 iine_led = !iine_led;
00090                 wait(0.1);
00091             }
00092             iine_led = 1;
00093         }
00094         else
00095         {
00096             // ボタンが押されていなければ適当に点滅させておく
00097             if (led_counter < 100)
00098                 iine_led = 0;
00099             else
00100                 iine_led = 1;
00101 
00102             if (led_counter > 1100)
00103                 led_counter = 0;
00104             else
00105                 led_counter++;
00106             wait(0.1);
00107         }
00108     }
00109 }