AUP_Lab7_RTOS
Dependencies: C12832 MMA7660 mbed-rtos mbed
Fork of AUP_Lab6_Music by
main.cpp@3:640558c1c0d3, 2015-07-08 (annotated)
- Committer:
- BrentLei
- Date:
- Wed Jul 08 00:09:57 2015 +0000
- Revision:
- 3:640558c1c0d3
- Parent:
- 2:09ee85ab1717
- Child:
- 4:dbce53c4e44d
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gyth | 0:65e58765adf7 | 1 | #include "mbed.h" |
BrentLei | 1:08431d9e3d95 | 2 | #include "C12832.h" |
BrentLei | 3:640558c1c0d3 | 3 | #include "MMA7660.h" |
gyth | 0:65e58765adf7 | 4 | |
BrentLei | 3:640558c1c0d3 | 5 | // 添加旋律播放相关文件 |
BrentLei | 3:640558c1c0d3 | 6 | #include "SongPlayer.h" |
BrentLei | 3:640558c1c0d3 | 7 | |
BrentLei | 3:640558c1c0d3 | 8 | // 旋律数据 |
BrentLei | 3:640558c1c0d3 | 9 | float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9, |
BrentLei | 3:640558c1c0d3 | 10 | 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0 |
BrentLei | 3:640558c1c0d3 | 11 | }; |
BrentLei | 3:640558c1c0d3 | 12 | float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24, |
BrentLei | 3:640558c1c0d3 | 13 | 0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0 |
BrentLei | 3:640558c1c0d3 | 14 | }; |
BrentLei | 2:09ee85ab1717 | 15 | |
gyth | 0:65e58765adf7 | 16 | PwmOut led(D5); |
gyth | 0:65e58765adf7 | 17 | DigitalIn button_up(A2); |
gyth | 0:65e58765adf7 | 18 | DigitalIn button_center(D4); |
gyth | 0:65e58765adf7 | 19 | DigitalIn button_down(A3); |
BrentLei | 1:08431d9e3d95 | 20 | C12832 lcd(D11, D13, D12, D7, D10); |
BrentLei | 3:640558c1c0d3 | 21 | MMA7660 MMA(D14, D15); |
gyth | 0:65e58765adf7 | 22 | |
BrentLei | 3:640558c1c0d3 | 23 | // 初始化D6引脚作为PWM用于驱动扬声器 |
BrentLei | 3:640558c1c0d3 | 24 | SongPlayer mySpeaker(D6); |
BrentLei | 2:09ee85ab1717 | 25 | |
gyth | 0:65e58765adf7 | 26 | double brightness = 1.0; |
gyth | 0:65e58765adf7 | 27 | double brightness_inc = 0.1; |
gyth | 0:65e58765adf7 | 28 | |
gyth | 0:65e58765adf7 | 29 | int main() |
gyth | 0:65e58765adf7 | 30 | { |
gyth | 0:65e58765adf7 | 31 | int bt_flag = 0; |
BrentLei | 1:08431d9e3d95 | 32 | lcd.cls(); |
gyth | 0:65e58765adf7 | 33 | led.write(brightness); |
BrentLei | 3:640558c1c0d3 | 34 | |
BrentLei | 3:640558c1c0d3 | 35 | // 播放旋律 |
BrentLei | 3:640558c1c0d3 | 36 | mySpeaker.PlaySong(note,duration,0.05); |
BrentLei | 3:640558c1c0d3 | 37 | |
gyth | 0:65e58765adf7 | 38 | while (1) { |
gyth | 0:65e58765adf7 | 39 | bt_flag = 1; |
gyth | 0:65e58765adf7 | 40 | if(button_up==1) |
gyth | 0:65e58765adf7 | 41 | brightness -= brightness_inc; |
gyth | 0:65e58765adf7 | 42 | else if(button_down==1) |
gyth | 0:65e58765adf7 | 43 | brightness += brightness_inc; |
gyth | 0:65e58765adf7 | 44 | else if(button_center==1) |
gyth | 0:65e58765adf7 | 45 | brightness = (brightness>0.5)?1.0:0.0; |
gyth | 0:65e58765adf7 | 46 | else |
gyth | 0:65e58765adf7 | 47 | bt_flag = 0; |
gyth | 0:65e58765adf7 | 48 | if(bt_flag==1) |
gyth | 0:65e58765adf7 | 49 | { |
gyth | 0:65e58765adf7 | 50 | brightness = (brightness>1.0)?0.0:brightness; |
gyth | 0:65e58765adf7 | 51 | brightness = (brightness<0.0)?1.0:brightness; |
gyth | 0:65e58765adf7 | 52 | led.write(brightness); |
gyth | 0:65e58765adf7 | 53 | } |
BrentLei | 2:09ee85ab1717 | 54 | lcd.locate(0, 0); |
BrentLei | 2:09ee85ab1717 | 55 | lcd.printf("Brightness = %.1f\r\n", 1.0 - brightness); |
BrentLei | 2:09ee85ab1717 | 56 | lcd.printf("x=%.2f y=%.2f z=%.2f", MMA.x(), MMA.y(), MMA.z()); |
BrentLei | 2:09ee85ab1717 | 57 | wait(0.2); |
gyth | 0:65e58765adf7 | 58 | } |
gyth | 0:65e58765adf7 | 59 | } |