
0609 practice 2
Dependencies: mbed ADXL345 Adafruit_GFX
main.cpp@0:0292cf4e9d92, 2022-06-09 (annotated)
- Committer:
- leejieun
- Date:
- Thu Jun 09 02:21:08 2022 +0000
- Revision:
- 0:0292cf4e9d92
0609 practice 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leejieun | 0:0292cf4e9d92 | 1 | #include "mbed.h" |
leejieun | 0:0292cf4e9d92 | 2 | #include "Adafruit_SSD1306.h" // Adafruit_GFX library |
leejieun | 0:0292cf4e9d92 | 3 | #include "ADXL345.h" // ADXL, SPI library |
leejieun | 0:0292cf4e9d92 | 4 | |
leejieun | 0:0292cf4e9d92 | 5 | #define NUM_CHAR 16 |
leejieun | 0:0292cf4e9d92 | 6 | |
leejieun | 0:0292cf4e9d92 | 7 | #define OLED_DISPLAY |
leejieun | 0:0292cf4e9d92 | 8 | #define ADC_TEST |
leejieun | 0:0292cf4e9d92 | 9 | |
leejieun | 0:0292cf4e9d92 | 10 | Serial pc(SERIAL_TX, SERIAL_RX); |
leejieun | 0:0292cf4e9d92 | 11 | DigitalOut greenLed(LED1); |
leejieun | 0:0292cf4e9d92 | 12 | DigitalOut redLed(PA_12); |
leejieun | 0:0292cf4e9d92 | 13 | Timer t1, t2; |
leejieun | 0:0292cf4e9d92 | 14 | Ticker t3, t4; |
leejieun | 0:0292cf4e9d92 | 15 | InterruptIn myButton(PC_13); |
leejieun | 0:0292cf4e9d92 | 16 | InterruptIn exButton(PC_11); |
leejieun | 0:0292cf4e9d92 | 17 | |
leejieun | 0:0292cf4e9d92 | 18 | BusOut my7Seg(PA_8, PA_9, PA_10, PC_9, PC_8, PC_7, PC_6, PA_11); // 8bit data |
leejieun | 0:0292cf4e9d92 | 19 | // LSB, , MSB |
leejieun | 0:0292cf4e9d92 | 20 | |
leejieun | 0:0292cf4e9d92 | 21 | int location_x = 0; |
leejieun | 0:0292cf4e9d92 | 22 | int location_y = 0; |
leejieun | 0:0292cf4e9d92 | 23 | ///picture |
leejieun | 0:0292cf4e9d92 | 24 | uint8_t pic1Logo[64 * 128 / 8] = |
leejieun | 0:0292cf4e9d92 | 25 | { |
leejieun | 0:0292cf4e9d92 | 26 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 27 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 28 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 29 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 30 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 31 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 32 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 33 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 36 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 37 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 38 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 39 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 40 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 42 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 43 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 44 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 45 | 0x00, 0x00, 0x80, 0xe0, 0xf0, 0xf0, 0xf8, 0xfc, 0xfc, 0x7c, 0x7e, 0x3e, 0x3e, 0x3e, 0x3e, 0x3e, |
leejieun | 0:0292cf4e9d92 | 46 | 0x7e, 0xfc, 0xfc, 0xf8, 0xf0, 0xf0, 0xe0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 47 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 48 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 49 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 50 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 51 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 52 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 53 | 0xf8, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x03, 0x02, 0x1c, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 54 | 0xf0, 0x0c, 0x04, 0x03, 0x07, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 55 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 56 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 58 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 59 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 60 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 61 | 0x03, 0x0f, 0x1f, 0x7f, 0xff, 0xfe, 0xfc, 0xfc, 0xf7, 0xe1, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, |
leejieun | 0:0292cf4e9d92 | 62 | 0xe1, 0xf7, 0xfc, 0xfc, 0xfe, 0xff, 0x7f, 0x1f, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 63 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 64 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 66 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 67 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 68 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 69 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
leejieun | 0:0292cf4e9d92 | 70 | 0x07, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 71 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 72 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 73 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 74 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 75 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 76 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 77 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 78 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 79 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 80 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 81 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 82 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 83 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 84 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 85 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 86 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 87 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 88 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
leejieun | 0:0292cf4e9d92 | 89 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
leejieun | 0:0292cf4e9d92 | 90 | }; |
leejieun | 0:0292cf4e9d92 | 91 | |
leejieun | 0:0292cf4e9d92 | 92 | ///picture |
leejieun | 0:0292cf4e9d92 | 93 | |
leejieun | 0:0292cf4e9d92 | 94 | AnalogOut myAnalogOut(PA_4); |
leejieun | 0:0292cf4e9d92 | 95 | AnalogIn lightSensor(PA_0); |
leejieun | 0:0292cf4e9d92 | 96 | ADXL345 accelerometer(PB_5, PB_4, PB_3, PB_6); // MOSI, MISO, SCK, CS |
leejieun | 0:0292cf4e9d92 | 97 | |
leejieun | 0:0292cf4e9d92 | 98 | char rxData[5]; |
leejieun | 0:0292cf4e9d92 | 99 | bool flagRx = 0; |
leejieun | 0:0292cf4e9d92 | 100 | int dir = 1; // 1: increase, -1: decrease |
leejieun | 0:0292cf4e9d92 | 101 | bool flagT3 = 0; |
leejieun | 0:0292cf4e9d92 | 102 | bool modeT3 = 0; // 0: stop, 1: working |
leejieun | 0:0292cf4e9d92 | 103 | bool flagDAC = 0; |
leejieun | 0:0292cf4e9d92 | 104 | bool flagADC = 0; |
leejieun | 0:0292cf4e9d92 | 105 | bool flagADXL = 0; // new added |
leejieun | 0:0292cf4e9d92 | 106 | |
leejieun | 0:0292cf4e9d92 | 107 | void ReceiveInt() { |
leejieun | 0:0292cf4e9d92 | 108 | char inChar; |
leejieun | 0:0292cf4e9d92 | 109 | static char rxCount = 0; |
leejieun | 0:0292cf4e9d92 | 110 | static char rxBuf[4]; |
leejieun | 0:0292cf4e9d92 | 111 | |
leejieun | 0:0292cf4e9d92 | 112 | while(1 == pc.readable()) { |
leejieun | 0:0292cf4e9d92 | 113 | inChar = pc.getc(); |
leejieun | 0:0292cf4e9d92 | 114 | if ('<' == inChar){ |
leejieun | 0:0292cf4e9d92 | 115 | rxCount = 1; |
leejieun | 0:0292cf4e9d92 | 116 | } |
leejieun | 0:0292cf4e9d92 | 117 | else if (rxCount > 0 && rxCount < 5) { |
leejieun | 0:0292cf4e9d92 | 118 | rxBuf[rxCount-1] = inChar; |
leejieun | 0:0292cf4e9d92 | 119 | rxCount++; |
leejieun | 0:0292cf4e9d92 | 120 | } |
leejieun | 0:0292cf4e9d92 | 121 | else if (5 == rxCount && '>' == inChar) { |
leejieun | 0:0292cf4e9d92 | 122 | rxCount = 0; |
leejieun | 0:0292cf4e9d92 | 123 | flagRx = 1; |
leejieun | 0:0292cf4e9d92 | 124 | memcpy(rxData, rxBuf, 4); |
leejieun | 0:0292cf4e9d92 | 125 | // pc.putc(rxData[0]); |
leejieun | 0:0292cf4e9d92 | 126 | // pc.putc(rxData[1]); |
leejieun | 0:0292cf4e9d92 | 127 | // pc.putc(rxData[2]); |
leejieun | 0:0292cf4e9d92 | 128 | // pc.putc(rxData[3]); |
leejieun | 0:0292cf4e9d92 | 129 | |
leejieun | 0:0292cf4e9d92 | 130 | // pc.puts(rxData); |
leejieun | 0:0292cf4e9d92 | 131 | } |
leejieun | 0:0292cf4e9d92 | 132 | else { |
leejieun | 0:0292cf4e9d92 | 133 | rxCount = 0; |
leejieun | 0:0292cf4e9d92 | 134 | } |
leejieun | 0:0292cf4e9d92 | 135 | } |
leejieun | 0:0292cf4e9d92 | 136 | } |
leejieun | 0:0292cf4e9d92 | 137 | |
leejieun | 0:0292cf4e9d92 | 138 | void tickerFunc3() { |
leejieun | 0:0292cf4e9d92 | 139 | flagT3 = 1; |
leejieun | 0:0292cf4e9d92 | 140 | } |
leejieun | 0:0292cf4e9d92 | 141 | |
leejieun | 0:0292cf4e9d92 | 142 | void Btn1Down() { |
leejieun | 0:0292cf4e9d92 | 143 | // pc.puts("1 pushed\n"); // for debugging |
leejieun | 0:0292cf4e9d92 | 144 | dir = -1*dir; |
leejieun | 0:0292cf4e9d92 | 145 | } |
leejieun | 0:0292cf4e9d92 | 146 | |
leejieun | 0:0292cf4e9d92 | 147 | void Btn1Up() { |
leejieun | 0:0292cf4e9d92 | 148 | // pc.puts("1 released\n"); // for debugging |
leejieun | 0:0292cf4e9d92 | 149 | greenLed = !greenLed; |
leejieun | 0:0292cf4e9d92 | 150 | } |
leejieun | 0:0292cf4e9d92 | 151 | |
leejieun | 0:0292cf4e9d92 | 152 | void Btn2Down() { |
leejieun | 0:0292cf4e9d92 | 153 | // pc.puts("2 pushed\n"); // for debugging |
leejieun | 0:0292cf4e9d92 | 154 | if (1 == modeT3) t3.detach(); |
leejieun | 0:0292cf4e9d92 | 155 | else t3.attach(tickerFunc3, 0.1); |
leejieun | 0:0292cf4e9d92 | 156 | modeT3 = !modeT3; |
leejieun | 0:0292cf4e9d92 | 157 | } |
leejieun | 0:0292cf4e9d92 | 158 | |
leejieun | 0:0292cf4e9d92 | 159 | void Btn2Up() { |
leejieun | 0:0292cf4e9d92 | 160 | // pc.puts("2 released\n"); // for debugging |
leejieun | 0:0292cf4e9d92 | 161 | redLed = !redLed; |
leejieun | 0:0292cf4e9d92 | 162 | } |
leejieun | 0:0292cf4e9d92 | 163 | |
leejieun | 0:0292cf4e9d92 | 164 | void DACInt() { |
leejieun | 0:0292cf4e9d92 | 165 | flagDAC = 1; |
leejieun | 0:0292cf4e9d92 | 166 | } |
leejieun | 0:0292cf4e9d92 | 167 | |
leejieun | 0:0292cf4e9d92 | 168 | void ADCint() { |
leejieun | 0:0292cf4e9d92 | 169 | flagADC = 1; |
leejieun | 0:0292cf4e9d92 | 170 | } |
leejieun | 0:0292cf4e9d92 | 171 | |
leejieun | 0:0292cf4e9d92 | 172 | // new added |
leejieun | 0:0292cf4e9d92 | 173 | void ADXLInt() |
leejieun | 0:0292cf4e9d92 | 174 | { |
leejieun | 0:0292cf4e9d92 | 175 | flagADXL = 1; |
leejieun | 0:0292cf4e9d92 | 176 | } |
leejieun | 0:0292cf4e9d92 | 177 | |
leejieun | 0:0292cf4e9d92 | 178 | int main() |
leejieun | 0:0292cf4e9d92 | 179 | { |
leejieun | 0:0292cf4e9d92 | 180 | pc.baud(115200); |
leejieun | 0:0292cf4e9d92 | 181 | pc.puts("\n<< ADXL Test Start>>\n"); |
leejieun | 0:0292cf4e9d92 | 182 | pc.printf("Device ID: 0x%02x\n", accelerometer.getDevId()); // 0xE5 |
leejieun | 0:0292cf4e9d92 | 183 | |
leejieun | 0:0292cf4e9d92 | 184 | pc.attach(&ReceiveInt, Serial::RxIrq); // RxIrq, TxIrq |
leejieun | 0:0292cf4e9d92 | 185 | myButton.fall(&Btn1Down); |
leejieun | 0:0292cf4e9d92 | 186 | myButton.rise(&Btn1Up); |
leejieun | 0:0292cf4e9d92 | 187 | exButton.fall(&Btn2Down); |
leejieun | 0:0292cf4e9d92 | 188 | exButton.rise(&Btn2Up); |
leejieun | 0:0292cf4e9d92 | 189 | |
leejieun | 0:0292cf4e9d92 | 190 | myButton.disable_irq(); // to avoid unexpected interrupt |
leejieun | 0:0292cf4e9d92 | 191 | exButton.disable_irq(); // to avoid unexpected interrupt |
leejieun | 0:0292cf4e9d92 | 192 | |
leejieun | 0:0292cf4e9d92 | 193 | //Go into standby mode to configure the device. |
leejieun | 0:0292cf4e9d92 | 194 | accelerometer.setPowerControl(0x00); |
leejieun | 0:0292cf4e9d92 | 195 | //Full resolution, +/-16g, 4mg/LSB. |
leejieun | 0:0292cf4e9d92 | 196 | accelerometer.setDataFormatControl(0x0B); // 0b 0000 1011 |
leejieun | 0:0292cf4e9d92 | 197 | //3.2kHz data rate. |
leejieun | 0:0292cf4e9d92 | 198 | accelerometer.setDataRate(ADXL345_3200HZ); |
leejieun | 0:0292cf4e9d92 | 199 | //Measurement mode. |
leejieun | 0:0292cf4e9d92 | 200 | accelerometer.setPowerControl(0x08); // 0b 0000 1000 |
leejieun | 0:0292cf4e9d92 | 201 | |
leejieun | 0:0292cf4e9d92 | 202 | Ticker tickerADXL; |
leejieun | 0:0292cf4e9d92 | 203 | tickerADXL.attach(&ADXLInt, 0.05); |
leejieun | 0:0292cf4e9d92 | 204 | |
leejieun | 0:0292cf4e9d92 | 205 | |
leejieun | 0:0292cf4e9d92 | 206 | I2C I2C_Oled(PB_7, PA_15); // SDA, SCL |
leejieun | 0:0292cf4e9d92 | 207 | I2C_Oled.frequency(400000); // 400kHz clock |
leejieun | 0:0292cf4e9d92 | 208 | Adafruit_SSD1306_I2c myOled(I2C_Oled, PD_2); // reset pin doesn't effect |
leejieun | 0:0292cf4e9d92 | 209 | // Adafruit_SSD1306_I2c myOled(I2C_Oled, PD_2, 64, 128); // make an error |
leejieun | 0:0292cf4e9d92 | 210 | |
leejieun | 0:0292cf4e9d92 | 211 | myOled.clearDisplay(); // clear buffer |
leejieun | 0:0292cf4e9d92 | 212 | myOled.printf("%u x %u OLED Display\r\n", myOled.width(), myOled.height()); |
leejieun | 0:0292cf4e9d92 | 213 | myOled.printf("Device ID: 0x%02x\n", accelerometer.getDevId()); |
leejieun | 0:0292cf4e9d92 | 214 | myOled.display(); // show a image on the OLED |
leejieun | 0:0292cf4e9d92 | 215 | wait(1); |
leejieun | 0:0292cf4e9d92 | 216 | |
leejieun | 0:0292cf4e9d92 | 217 | myButton.enable_irq(); // enable IRQ |
leejieun | 0:0292cf4e9d92 | 218 | exButton.enable_irq(); // enable IRQ |
leejieun | 0:0292cf4e9d92 | 219 | |
leejieun | 0:0292cf4e9d92 | 220 | time_t seconds = time(NULL); |
leejieun | 0:0292cf4e9d92 | 221 | |
leejieun | 0:0292cf4e9d92 | 222 | set_time(1617235200); // Set RTC time to 2021-04-01, 00:00:00 |
leejieun | 0:0292cf4e9d92 | 223 | pc.printf("Time as a basic string = %s", ctime(&seconds)); |
leejieun | 0:0292cf4e9d92 | 224 | |
leejieun | 0:0292cf4e9d92 | 225 | char buffer[32]; |
leejieun | 0:0292cf4e9d92 | 226 | strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
leejieun | 0:0292cf4e9d92 | 227 | pc.printf("1) Time as a custom formatted string = %s", buffer); |
leejieun | 0:0292cf4e9d92 | 228 | strftime(buffer, 32, "%y-%m-%d, %H:%M:%S\n", localtime(&seconds)); |
leejieun | 0:0292cf4e9d92 | 229 | pc.printf("2) Time as a custom formatted string = %s", buffer); |
leejieun | 0:0292cf4e9d92 | 230 | |
leejieun | 0:0292cf4e9d92 | 231 | // int preStatus1 = 0; |
leejieun | 0:0292cf4e9d92 | 232 | // int preStatus2 = 0; |
leejieun | 0:0292cf4e9d92 | 233 | int tempVal = 0; |
leejieun | 0:0292cf4e9d92 | 234 | char tmpCommand[3]; |
leejieun | 0:0292cf4e9d92 | 235 | int rxVal; |
leejieun | 0:0292cf4e9d92 | 236 | char val7Seg[NUM_CHAR] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71}; |
leejieun | 0:0292cf4e9d92 | 237 | |
leejieun | 0:0292cf4e9d92 | 238 | modeT3 = 1; |
leejieun | 0:0292cf4e9d92 | 239 | my7Seg = 0xFF; |
leejieun | 0:0292cf4e9d92 | 240 | |
leejieun | 0:0292cf4e9d92 | 241 | uint16_t n = 0; |
leejieun | 0:0292cf4e9d92 | 242 | |
leejieun | 0:0292cf4e9d92 | 243 | char tempVal2[10] = {0,}; |
leejieun | 0:0292cf4e9d92 | 244 | unsigned int exLight; |
leejieun | 0:0292cf4e9d92 | 245 | unsigned char posY[2] = {0,}; |
leejieun | 0:0292cf4e9d92 | 246 | int ADXLdata[3]; // new added |
leejieun | 0:0292cf4e9d92 | 247 | // t3.attach(&tickerFunc3, 0.1); // ticker3 start for display |
leejieun | 0:0292cf4e9d92 | 248 | |
leejieun | 0:0292cf4e9d92 | 249 | myOled.clearDisplay(); // clear buffer |
leejieun | 0:0292cf4e9d92 | 250 | // check the external button action |
leejieun | 0:0292cf4e9d92 | 251 | myOled.clearDisplay(); |
leejieun | 0:0292cf4e9d92 | 252 | myOled.drawBitmap(location_x, location_y, pic1Logo, 128, 64, 1); |
leejieun | 0:0292cf4e9d92 | 253 | myOled.display(); |
leejieun | 0:0292cf4e9d92 | 254 | //wait(1); |
leejieun | 0:0292cf4e9d92 | 255 | |
leejieun | 0:0292cf4e9d92 | 256 | |
leejieun | 0:0292cf4e9d92 | 257 | while(1) |
leejieun | 0:0292cf4e9d92 | 258 | { |
leejieun | 0:0292cf4e9d92 | 259 | if (1 == flagT3) { |
leejieun | 0:0292cf4e9d92 | 260 | flagT3 = 0; |
leejieun | 0:0292cf4e9d92 | 261 | n = n + dir; |
leejieun | 0:0292cf4e9d92 | 262 | // if (n > 200) n = 0; |
leejieun | 0:0292cf4e9d92 | 263 | // myAnalogOut = n*0.005f; |
leejieun | 0:0292cf4e9d92 | 264 | // sprintf(tempVal2, "%d, %1.2f\n", n, myAnalogOut.read()); |
leejieun | 0:0292cf4e9d92 | 265 | // pc.puts(tempVal2); |
leejieun | 0:0292cf4e9d92 | 266 | |
leejieun | 0:0292cf4e9d92 | 267 | my7Seg = ~val7Seg[n%16] & 0x7F; |
leejieun | 0:0292cf4e9d92 | 268 | |
leejieun | 0:0292cf4e9d92 | 269 | exLight = lightSensor.read() * 500; // ADC read & send data : about 300us consume |
leejieun | 0:0292cf4e9d92 | 270 | sprintf(tempVal2, "%d\n", exLight); |
leejieun | 0:0292cf4e9d92 | 271 | pc.puts(tempVal2); |
leejieun | 0:0292cf4e9d92 | 272 | |
leejieun | 0:0292cf4e9d92 | 273 | #ifdef OLED_DISPLAY |
leejieun | 0:0292cf4e9d92 | 274 | posY[1] = 64 - (unsigned char)((float)(exLight-300) * 0.256f); // 128/500 = 0.256 |
leejieun | 0:0292cf4e9d92 | 275 | myOled.drawLine(n, posY[0], n+1, posY[1], 1); |
leejieun | 0:0292cf4e9d92 | 276 | myOled.display(); // OLED display consumes about 28ms |
leejieun | 0:0292cf4e9d92 | 277 | posY[0] = posY[1]; |
leejieun | 0:0292cf4e9d92 | 278 | if (n > 127) { |
leejieun | 0:0292cf4e9d92 | 279 | n = 0; |
leejieun | 0:0292cf4e9d92 | 280 | myOled.clearDisplay(); |
leejieun | 0:0292cf4e9d92 | 281 | } |
leejieun | 0:0292cf4e9d92 | 282 | #endif |
leejieun | 0:0292cf4e9d92 | 283 | |
leejieun | 0:0292cf4e9d92 | 284 | greenLed = !greenLed; |
leejieun | 0:0292cf4e9d92 | 285 | } |
leejieun | 0:0292cf4e9d92 | 286 | |
leejieun | 0:0292cf4e9d92 | 287 | if (1 == flagADXL) { |
leejieun | 0:0292cf4e9d92 | 288 | flagADXL = 0; |
leejieun | 0:0292cf4e9d92 | 289 | accelerometer.getOutput(ADXLdata); // ADXL reading consumes about 80us |
leejieun | 0:0292cf4e9d92 | 290 | pc.printf("%d, %d, %d, %d\n", (int16_t)ADXLdata[0], (int16_t)ADXLdata[1], (int16_t)ADXLdata[2],location_x); // sending data consumes about 1ms (worst case) |
leejieun | 0:0292cf4e9d92 | 291 | } |
leejieun | 0:0292cf4e9d92 | 292 | ///// x axis moving |
leejieun | 0:0292cf4e9d92 | 293 | if ((int16_t)ADXLdata[0] <= -150){ |
leejieun | 0:0292cf4e9d92 | 294 | if (location_x > -64){ |
leejieun | 0:0292cf4e9d92 | 295 | location_x--; |
leejieun | 0:0292cf4e9d92 | 296 | } |
leejieun | 0:0292cf4e9d92 | 297 | } |
leejieun | 0:0292cf4e9d92 | 298 | if ((int16_t)ADXLdata[0] >= 150){ |
leejieun | 0:0292cf4e9d92 | 299 | if (location_x < 64){ |
leejieun | 0:0292cf4e9d92 | 300 | location_x++; |
leejieun | 0:0292cf4e9d92 | 301 | } |
leejieun | 0:0292cf4e9d92 | 302 | } |
leejieun | 0:0292cf4e9d92 | 303 | ///// y axis moving |
leejieun | 0:0292cf4e9d92 | 304 | if ((int16_t)ADXLdata[1] <= -150){ |
leejieun | 0:0292cf4e9d92 | 305 | if (location_y > -32){ |
leejieun | 0:0292cf4e9d92 | 306 | location_y++; |
leejieun | 0:0292cf4e9d92 | 307 | } |
leejieun | 0:0292cf4e9d92 | 308 | } |
leejieun | 0:0292cf4e9d92 | 309 | if ((int16_t)ADXLdata[1] >= 150){ |
leejieun | 0:0292cf4e9d92 | 310 | if (location_y < 32){ |
leejieun | 0:0292cf4e9d92 | 311 | location_y--; |
leejieun | 0:0292cf4e9d92 | 312 | } |
leejieun | 0:0292cf4e9d92 | 313 | } |
leejieun | 0:0292cf4e9d92 | 314 | |
leejieun | 0:0292cf4e9d92 | 315 | |
leejieun | 0:0292cf4e9d92 | 316 | t3.attach(&tickerFunc3, 0.1); // ticker3 start for display |
leejieun | 0:0292cf4e9d92 | 317 | |
leejieun | 0:0292cf4e9d92 | 318 | // check the external button action |
leejieun | 0:0292cf4e9d92 | 319 | myOled.clearDisplay(); |
leejieun | 0:0292cf4e9d92 | 320 | myOled.drawBitmap(location_x, location_y, pic1Logo, 128, 64, 1); |
leejieun | 0:0292cf4e9d92 | 321 | myOled.display(); |
leejieun | 0:0292cf4e9d92 | 322 | //wait(1); |
leejieun | 0:0292cf4e9d92 | 323 | |
leejieun | 0:0292cf4e9d92 | 324 | |
leejieun | 0:0292cf4e9d92 | 325 | |
leejieun | 0:0292cf4e9d92 | 326 | |
leejieun | 0:0292cf4e9d92 | 327 | |
leejieun | 0:0292cf4e9d92 | 328 | ////////////////////////////////////Serial |
leejieun | 0:0292cf4e9d92 | 329 | if (1 == flagRx){ |
leejieun | 0:0292cf4e9d92 | 330 | flagRx = 0; |
leejieun | 0:0292cf4e9d92 | 331 | tmpCommand[0] = rxData[0]; |
leejieun | 0:0292cf4e9d92 | 332 | tmpCommand[1] = rxData[1]; |
leejieun | 0:0292cf4e9d92 | 333 | tmpCommand[2] = 0; |
leejieun | 0:0292cf4e9d92 | 334 | rxVal = atoi(rxData+2); |
leejieun | 0:0292cf4e9d92 | 335 | |
leejieun | 0:0292cf4e9d92 | 336 | if (0 == strcmp(tmpCommand, "LD")) { // control a LED |
leejieun | 0:0292cf4e9d92 | 337 | pc.printf("val = %d\n", rxVal); |
leejieun | 0:0292cf4e9d92 | 338 | |
leejieun | 0:0292cf4e9d92 | 339 | greenLed = (1 == rxVal)? 1:0; |
leejieun | 0:0292cf4e9d92 | 340 | // greenLed = rxVal? 1:0; |
leejieun | 0:0292cf4e9d92 | 341 | } |
leejieun | 0:0292cf4e9d92 | 342 | if (0 == strcmp(tmpCommand, "RE")) { // reset all variables |
leejieun | 0:0292cf4e9d92 | 343 | greenLed = 0; |
leejieun | 0:0292cf4e9d92 | 344 | redLed = 1; |
leejieun | 0:0292cf4e9d92 | 345 | n = 0; |
leejieun | 0:0292cf4e9d92 | 346 | } |
leejieun | 0:0292cf4e9d92 | 347 | if (0 == strcmp(tmpCommand, "OL")) |
leejieun | 0:0292cf4e9d92 | 348 | { |
leejieun | 0:0292cf4e9d92 | 349 | greenLed = 1; |
leejieun | 0:0292cf4e9d92 | 350 | redLed = 1; |
leejieun | 0:0292cf4e9d92 | 351 | if (rxVal == 1){ |
leejieun | 0:0292cf4e9d92 | 352 | posY[1] = ADXLdata[0]; // 128/500 = 0.256 |
leejieun | 0:0292cf4e9d92 | 353 | myOled.drawLine(n, posY[0], n+1, posY[1], 1); |
leejieun | 0:0292cf4e9d92 | 354 | myOled.display(); // OLED display consumes about 28ms |
leejieun | 0:0292cf4e9d92 | 355 | posY[0] = posY[1]; |
leejieun | 0:0292cf4e9d92 | 356 | if (n > 127) { |
leejieun | 0:0292cf4e9d92 | 357 | n = 0; |
leejieun | 0:0292cf4e9d92 | 358 | myOled.clearDisplay(); |
leejieun | 0:0292cf4e9d92 | 359 | } |
leejieun | 0:0292cf4e9d92 | 360 | } |
leejieun | 0:0292cf4e9d92 | 361 | |
leejieun | 0:0292cf4e9d92 | 362 | if (rxVal == 2){ |
leejieun | 0:0292cf4e9d92 | 363 | posY[1] = ADXLdata[1]; // 128/500 = 0.256 |
leejieun | 0:0292cf4e9d92 | 364 | myOled.drawLine(n, posY[0], n+1, posY[1], 1); |
leejieun | 0:0292cf4e9d92 | 365 | myOled.display(); // OLED display consumes about 28ms |
leejieun | 0:0292cf4e9d92 | 366 | posY[0] = posY[1]; |
leejieun | 0:0292cf4e9d92 | 367 | if (n > 127) { |
leejieun | 0:0292cf4e9d92 | 368 | n = 0; |
leejieun | 0:0292cf4e9d92 | 369 | myOled.clearDisplay(); |
leejieun | 0:0292cf4e9d92 | 370 | } |
leejieun | 0:0292cf4e9d92 | 371 | } |
leejieun | 0:0292cf4e9d92 | 372 | |
leejieun | 0:0292cf4e9d92 | 373 | |
leejieun | 0:0292cf4e9d92 | 374 | if (rxVal == 3){ |
leejieun | 0:0292cf4e9d92 | 375 | posY[1] = ADXLdata[2]; // 128/500 = 0.256 |
leejieun | 0:0292cf4e9d92 | 376 | myOled.drawLine(n, posY[0], n+1, posY[1], 1); |
leejieun | 0:0292cf4e9d92 | 377 | myOled.display(); // OLED display consumes about 28ms |
leejieun | 0:0292cf4e9d92 | 378 | posY[0] = posY[1]; |
leejieun | 0:0292cf4e9d92 | 379 | if (n > 127) { |
leejieun | 0:0292cf4e9d92 | 380 | n = 0; |
leejieun | 0:0292cf4e9d92 | 381 | myOled.clearDisplay(); |
leejieun | 0:0292cf4e9d92 | 382 | } |
leejieun | 0:0292cf4e9d92 | 383 | } |
leejieun | 0:0292cf4e9d92 | 384 | |
leejieun | 0:0292cf4e9d92 | 385 | if (rxVal == 0){ |
leejieun | 0:0292cf4e9d92 | 386 | myOled.clearDisplay(); |
leejieun | 0:0292cf4e9d92 | 387 | } |
leejieun | 0:0292cf4e9d92 | 388 | |
leejieun | 0:0292cf4e9d92 | 389 | } |
leejieun | 0:0292cf4e9d92 | 390 | } |
leejieun | 0:0292cf4e9d92 | 391 | |
leejieun | 0:0292cf4e9d92 | 392 | |
leejieun | 0:0292cf4e9d92 | 393 | } |
leejieun | 0:0292cf4e9d92 | 394 | } |