![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
test
main.cpp@0:c5ecf6f65c65, 2020-07-09 (annotated)
- Committer:
- takuminomura
- Date:
- Thu Jul 09 02:33:13 2020 +0000
- Revision:
- 0:c5ecf6f65c65
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
takuminomura | 0:c5ecf6f65c65 | 1 | /* mbed Microcontroller Library |
takuminomura | 0:c5ecf6f65c65 | 2 | * Copyright (c) 2019 ARM Limited |
takuminomura | 0:c5ecf6f65c65 | 3 | * SPDX-License-Identifier: Apache-2.0 |
takuminomura | 0:c5ecf6f65c65 | 4 | */ |
takuminomura | 0:c5ecf6f65c65 | 5 | #include "mbed.h" |
takuminomura | 0:c5ecf6f65c65 | 6 | #include "platform/mbed_thread.h" |
takuminomura | 0:c5ecf6f65c65 | 7 | #include "rtos.h" |
takuminomura | 0:c5ecf6f65c65 | 8 | |
takuminomura | 0:c5ecf6f65c65 | 9 | // Blinking rate in milliseconds |
takuminomura | 0:c5ecf6f65c65 | 10 | #define BLINKING_RATE_MS 100 |
takuminomura | 0:c5ecf6f65c65 | 11 | #define TH 0.5 |
takuminomura | 0:c5ecf6f65c65 | 12 | |
takuminomura | 0:c5ecf6f65c65 | 13 | const int sensArray[4] = {0, 1, 2, 3}; |
takuminomura | 0:c5ecf6f65c65 | 14 | /* |
takuminomura | 0:c5ecf6f65c65 | 15 | const int sensArray[32] = { 0, 4, 2, 3, 0, 0, 1, 0, |
takuminomura | 0:c5ecf6f65c65 | 16 | -2, 0, 0, 0, -1, 0, 0, 0, |
takuminomura | 0:c5ecf6f65c65 | 17 | -4, 0, 0, 0, 0, 0, 0, 0, |
takuminomura | 0:c5ecf6f65c65 | 18 | -3, 0, 0, 0, 0, 0, 0, 0}; |
takuminomura | 0:c5ecf6f65c65 | 19 | */ |
takuminomura | 0:c5ecf6f65c65 | 20 | //DigitalOut led1(LED1); // デジタル出力 |
takuminomura | 0:c5ecf6f65c65 | 21 | |
takuminomura | 0:c5ecf6f65c65 | 22 | //PwmOut led1(LED1); // PWM出力 |
takuminomura | 0:c5ecf6f65c65 | 23 | |
takuminomura | 0:c5ecf6f65c65 | 24 | DigitalOut led1(LED1); |
takuminomura | 0:c5ecf6f65c65 | 25 | DigitalOut led2(LED2); |
takuminomura | 0:c5ecf6f65c65 | 26 | DigitalOut led3(LED3); |
takuminomura | 0:c5ecf6f65c65 | 27 | DigitalOut led4(LED4); |
takuminomura | 0:c5ecf6f65c65 | 28 | |
takuminomura | 0:c5ecf6f65c65 | 29 | DigitalOut green(p24); |
takuminomura | 0:c5ecf6f65c65 | 30 | DigitalOut blue(p23); |
takuminomura | 0:c5ecf6f65c65 | 31 | DigitalOut red(p22); |
takuminomura | 0:c5ecf6f65c65 | 32 | DigitalOut yellow(p21); |
takuminomura | 0:c5ecf6f65c65 | 33 | |
takuminomura | 0:c5ecf6f65c65 | 34 | //AnalogIn s0(p20); // アナログ入力 |
takuminomura | 0:c5ecf6f65c65 | 35 | //AnalogIn s1(p18); |
takuminomura | 0:c5ecf6f65c65 | 36 | |
takuminomura | 0:c5ecf6f65c65 | 37 | DigitalIn s0(p20); // アナログ入力 |
takuminomura | 0:c5ecf6f65c65 | 38 | DigitalIn s1(p18); |
takuminomura | 0:c5ecf6f65c65 | 39 | |
takuminomura | 0:c5ecf6f65c65 | 40 | Timeout sensor_tick; // タイマ割込み |
takuminomura | 0:c5ecf6f65c65 | 41 | |
takuminomura | 0:c5ecf6f65c65 | 42 | |
takuminomura | 0:c5ecf6f65c65 | 43 | Serial pc(USBTX, USBRX); // tx, rx |
takuminomura | 0:c5ecf6f65c65 | 44 | //volatile int count = 0; |
takuminomura | 0:c5ecf6f65c65 | 45 | volatile float val0, val1; |
takuminomura | 0:c5ecf6f65c65 | 46 | volatile int flag = 0, sens_val; |
takuminomura | 0:c5ecf6f65c65 | 47 | |
takuminomura | 0:c5ecf6f65c65 | 48 | Thread thread1; |
takuminomura | 0:c5ecf6f65c65 | 49 | Thread thread2; |
takuminomura | 0:c5ecf6f65c65 | 50 | Thread thread3; |
takuminomura | 0:c5ecf6f65c65 | 51 | Thread thread4; |
takuminomura | 0:c5ecf6f65c65 | 52 | Thread thread5; |
takuminomura | 0:c5ecf6f65c65 | 53 | Thread thread6; |
takuminomura | 0:c5ecf6f65c65 | 54 | /*void const *argument*/ |
takuminomura | 0:c5ecf6f65c65 | 55 | |
takuminomura | 0:c5ecf6f65c65 | 56 | void Led(DigitalOut *led){ |
takuminomura | 0:c5ecf6f65c65 | 57 | while(true){ |
takuminomura | 0:c5ecf6f65c65 | 58 | ThisThread::flags_wait_all(0x1); |
takuminomura | 0:c5ecf6f65c65 | 59 | *led = !*led; |
takuminomura | 0:c5ecf6f65c65 | 60 | //ThisThread::sleep_for(100); |
takuminomura | 0:c5ecf6f65c65 | 61 | } |
takuminomura | 0:c5ecf6f65c65 | 62 | } |
takuminomura | 0:c5ecf6f65c65 | 63 | void check_sensor(){ |
takuminomura | 0:c5ecf6f65c65 | 64 | ::val0 = s0; |
takuminomura | 0:c5ecf6f65c65 | 65 | ::val1 = s1; |
takuminomura | 0:c5ecf6f65c65 | 66 | } |
takuminomura | 0:c5ecf6f65c65 | 67 | void sensor(void const *argument){ |
takuminomura | 0:c5ecf6f65c65 | 68 | led4 = !led4; |
takuminomura | 0:c5ecf6f65c65 | 69 | check_sensor(); |
takuminomura | 0:c5ecf6f65c65 | 70 | flag = 0; |
takuminomura | 0:c5ecf6f65c65 | 71 | if(::val0 == 1) flag |= 0x2; |
takuminomura | 0:c5ecf6f65c65 | 72 | if(::val1 == 1) flag |= 0x1; |
takuminomura | 0:c5ecf6f65c65 | 73 | sens_val = sensArray[flag]; |
takuminomura | 0:c5ecf6f65c65 | 74 | switch(sens_val){ |
takuminomura | 0:c5ecf6f65c65 | 75 | case 0: |
takuminomura | 0:c5ecf6f65c65 | 76 | thread3.flags_set(0x1); |
takuminomura | 0:c5ecf6f65c65 | 77 | ThisThread::flags_clear(0x1); |
takuminomura | 0:c5ecf6f65c65 | 78 | break; |
takuminomura | 0:c5ecf6f65c65 | 79 | case 1: |
takuminomura | 0:c5ecf6f65c65 | 80 | thread1.flags_set(0x1); |
takuminomura | 0:c5ecf6f65c65 | 81 | thread4.flags_set(0x1); |
takuminomura | 0:c5ecf6f65c65 | 82 | ThisThread::flags_clear(0x1); |
takuminomura | 0:c5ecf6f65c65 | 83 | break; |
takuminomura | 0:c5ecf6f65c65 | 84 | case 2: |
takuminomura | 0:c5ecf6f65c65 | 85 | thread2.flags_set(0x1); |
takuminomura | 0:c5ecf6f65c65 | 86 | thread5.flags_set(0x1); |
takuminomura | 0:c5ecf6f65c65 | 87 | ThisThread::flags_clear(0x1); |
takuminomura | 0:c5ecf6f65c65 | 88 | break; |
takuminomura | 0:c5ecf6f65c65 | 89 | case 3: |
takuminomura | 0:c5ecf6f65c65 | 90 | thread1.flags_set(0x1); |
takuminomura | 0:c5ecf6f65c65 | 91 | thread2.flags_set(0x1); |
takuminomura | 0:c5ecf6f65c65 | 92 | thread6.flags_set(0x1); |
takuminomura | 0:c5ecf6f65c65 | 93 | ThisThread::flags_clear(0x1); |
takuminomura | 0:c5ecf6f65c65 | 94 | break; |
takuminomura | 0:c5ecf6f65c65 | 95 | } |
takuminomura | 0:c5ecf6f65c65 | 96 | } |
takuminomura | 0:c5ecf6f65c65 | 97 | int main() |
takuminomura | 0:c5ecf6f65c65 | 98 | { |
takuminomura | 0:c5ecf6f65c65 | 99 | RtosTimer sensor_timer(sensor, osTimerPeriodic, (void *)0); // タイマ割込み |
takuminomura | 0:c5ecf6f65c65 | 100 | sensor_timer.start(50); |
takuminomura | 0:c5ecf6f65c65 | 101 | |
takuminomura | 0:c5ecf6f65c65 | 102 | //sensor_tick.attach_us(&sensor, 1000000); // これもタイマ割込み |
takuminomura | 0:c5ecf6f65c65 | 103 | |
takuminomura | 0:c5ecf6f65c65 | 104 | thread1.start(callback(Led, &led1)); |
takuminomura | 0:c5ecf6f65c65 | 105 | thread2.start(callback(Led, &led2)); |
takuminomura | 0:c5ecf6f65c65 | 106 | thread3.start(callback(Led, &green)); |
takuminomura | 0:c5ecf6f65c65 | 107 | thread4.start(callback(Led, &blue)); |
takuminomura | 0:c5ecf6f65c65 | 108 | thread5.start(callback(Led, &red)); |
takuminomura | 0:c5ecf6f65c65 | 109 | thread6.start(callback(Led, &yellow)); |
takuminomura | 0:c5ecf6f65c65 | 110 | |
takuminomura | 0:c5ecf6f65c65 | 111 | while (true){ |
takuminomura | 0:c5ecf6f65c65 | 112 | led3 = !led3; |
takuminomura | 0:c5ecf6f65c65 | 113 | ThisThread::sleep_for(10); |
takuminomura | 0:c5ecf6f65c65 | 114 | } |
takuminomura | 0:c5ecf6f65c65 | 115 | } |