OLED_MISSION2

Dependencies:   mbed Adafruit_GFX

Committer:
leejieun
Date:
Sun May 08 09:37:07 2022 +0000
Revision:
0:a69af42d193a
Child:
1:8d6620a8f26b
OLED_MISSION2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leejieun 0:a69af42d193a 1 #include "mbed.h"
leejieun 0:a69af42d193a 2 #include "Adafruit_SSD1306.h" // Adafruit_GFX library
leejieun 0:a69af42d193a 3
leejieun 0:a69af42d193a 4 #define NUM_CHAR 16
leejieun 0:a69af42d193a 5
leejieun 0:a69af42d193a 6 Serial pc(SERIAL_TX, SERIAL_RX);
leejieun 0:a69af42d193a 7 DigitalOut greenLed(LED1);
leejieun 0:a69af42d193a 8 DigitalOut redLed(PA_12);
leejieun 0:a69af42d193a 9 AnalogOut myAnalogOut(PA_4);
leejieun 0:a69af42d193a 10 AnalogIn lightSensor(PA_0);
leejieun 0:a69af42d193a 11 Timer t1, t2;
leejieun 0:a69af42d193a 12 Ticker t3, t4;
leejieun 0:a69af42d193a 13 InterruptIn myButton(PC_13);
leejieun 0:a69af42d193a 14 InterruptIn exButton(PC_11);
leejieun 0:a69af42d193a 15
leejieun 0:a69af42d193a 16 BusOut my7Seg(PA_8, PA_9, PA_10, PC_9, PC_8, PC_7, PC_6, PA_11); // 8bit data
leejieun 0:a69af42d193a 17 // LSB, , MSB
leejieun 0:a69af42d193a 18
leejieun 0:a69af42d193a 19 char rxData[5];
leejieun 0:a69af42d193a 20 bool flagRx = 0;
leejieun 0:a69af42d193a 21 int dir = 1; // 1: increase, -1: decrease
leejieun 0:a69af42d193a 22 bool flagT3 = 0;
leejieun 0:a69af42d193a 23 bool modeT3 = 0; // 0: stop, 1: working
leejieun 0:a69af42d193a 24
leejieun 0:a69af42d193a 25
leejieun 0:a69af42d193a 26
leejieun 0:a69af42d193a 27
leejieun 0:a69af42d193a 28 void ReceiveInt() {
leejieun 0:a69af42d193a 29 char inChar;
leejieun 0:a69af42d193a 30 static char rxCount = 0;
leejieun 0:a69af42d193a 31 static char rxBuf[4];
leejieun 0:a69af42d193a 32
leejieun 0:a69af42d193a 33 while(1 == pc.readable()) {
leejieun 0:a69af42d193a 34 inChar = pc.getc();
leejieun 0:a69af42d193a 35 if ('<' == inChar){
leejieun 0:a69af42d193a 36 rxCount = 1;
leejieun 0:a69af42d193a 37 }
leejieun 0:a69af42d193a 38 else if (rxCount > 0 && rxCount < 5) {
leejieun 0:a69af42d193a 39 rxBuf[rxCount-1] = inChar;
leejieun 0:a69af42d193a 40 rxCount++;
leejieun 0:a69af42d193a 41 }
leejieun 0:a69af42d193a 42 else if (5 == rxCount && '>' == inChar) {
leejieun 0:a69af42d193a 43 rxCount = 0;
leejieun 0:a69af42d193a 44 flagRx = 1;
leejieun 0:a69af42d193a 45 memcpy(rxData, rxBuf, 4);
leejieun 0:a69af42d193a 46 // pc.putc(rxData[0]);
leejieun 0:a69af42d193a 47 // pc.putc(rxData[1]);
leejieun 0:a69af42d193a 48 // pc.putc(rxData[2]);
leejieun 0:a69af42d193a 49 // pc.putc(rxData[3]);
leejieun 0:a69af42d193a 50
leejieun 0:a69af42d193a 51 // pc.puts(rxData);
leejieun 0:a69af42d193a 52 }
leejieun 0:a69af42d193a 53 else {
leejieun 0:a69af42d193a 54 rxCount = 0;
leejieun 0:a69af42d193a 55 }
leejieun 0:a69af42d193a 56 }
leejieun 0:a69af42d193a 57 }
leejieun 0:a69af42d193a 58
leejieun 0:a69af42d193a 59 void tickerFunc3() {
leejieun 0:a69af42d193a 60 flagT3 = 1;
leejieun 0:a69af42d193a 61 }
leejieun 0:a69af42d193a 62 // 버튼1을 2초 이상 누르면 LED 변환 (led1, led2 선택)
leejieun 0:a69af42d193a 63 // 버튼2를 2초 이상 누르면 현재 LED 초기화 (초기 깜빡임 속도 0.5초)
leejieun 0:a69af42d193a 64 // 버튼1을 살짝 누르면 깜빡임 속도 감소, 버튼2를 살짝 누르면 깜빡임 속도 증가 (0.1초 ~ 1초, 0.1초 단계)
leejieun 0:a69af42d193a 65
leejieun 0:a69af42d193a 66
leejieun 0:a69af42d193a 67 //// 스위치 플래그
leejieun 0:a69af42d193a 68 int flagDown = 0; // 1: speed down, 2: change LED
leejieun 0:a69af42d193a 69 int flagUp = 0; // 1: speed up, 2: reset biliking speed
leejieun 0:a69af42d193a 70
leejieun 0:a69af42d193a 71 void Btn1Down() {
leejieun 0:a69af42d193a 72 // pc.puts("1 pushed\n"); // for debugging
leejieun 0:a69af42d193a 73 t1.start();
leejieun 0:a69af42d193a 74 }
leejieun 0:a69af42d193a 75
leejieun 0:a69af42d193a 76 void Btn1Up() {
leejieun 0:a69af42d193a 77 // pc.puts("1 released\n"); // for debugging
leejieun 0:a69af42d193a 78 float tempT1 = t1.read();
leejieun 0:a69af42d193a 79 if (tempT1 > 2.0f) {
leejieun 0:a69af42d193a 80 flagDown = 2;
leejieun 0:a69af42d193a 81 }
leejieun 0:a69af42d193a 82 else if (tempT1 > 0) {
leejieun 0:a69af42d193a 83 flagDown = 1;
leejieun 0:a69af42d193a 84 }
leejieun 0:a69af42d193a 85 t1.stop();
leejieun 0:a69af42d193a 86 t1.reset();
leejieun 0:a69af42d193a 87 }
leejieun 0:a69af42d193a 88
leejieun 0:a69af42d193a 89 void Btn2Down() {
leejieun 0:a69af42d193a 90 // pc.puts("2 pushed\n"); // for debugging
leejieun 0:a69af42d193a 91 t2.start();
leejieun 0:a69af42d193a 92 }
leejieun 0:a69af42d193a 93 //플래그 업이 2가 되면 설정모드.
leejieun 0:a69af42d193a 94 void Btn2Up() {
leejieun 0:a69af42d193a 95 // pc.puts("2 released\n"); // for debugging
leejieun 0:a69af42d193a 96 float tempT2 = t2.read();
leejieun 0:a69af42d193a 97 if (tempT2 > 2.0f) {
leejieun 0:a69af42d193a 98 flagUp = 2;
leejieun 0:a69af42d193a 99 }
leejieun 0:a69af42d193a 100 //플래그 업이 1이면 그냥 눌렀을때
leejieun 0:a69af42d193a 101 else if (tempT2 > 0) {
leejieun 0:a69af42d193a 102 flagUp = 1;
leejieun 0:a69af42d193a 103 }
leejieun 0:a69af42d193a 104 t2.stop();
leejieun 0:a69af42d193a 105 t2.reset();
leejieun 0:a69af42d193a 106 }
leejieun 0:a69af42d193a 107
leejieun 0:a69af42d193a 108
leejieun 0:a69af42d193a 109 //
leejieun 0:a69af42d193a 110 int main()
leejieun 0:a69af42d193a 111 {
leejieun 0:a69af42d193a 112 pc.baud(115200);
leejieun 0:a69af42d193a 113 pc.puts("\nStart!\n");
leejieun 0:a69af42d193a 114
leejieun 0:a69af42d193a 115 myButton.disable_irq(); // to avoid unexpected interrupt
leejieun 0:a69af42d193a 116 exButton.disable_irq(); // to avoid unexpected interrupt
leejieun 0:a69af42d193a 117
leejieun 0:a69af42d193a 118 pc.attach(&ReceiveInt, Serial::RxIrq); // RxIrq, TxIrq
leejieun 0:a69af42d193a 119 myButton.fall(&Btn1Down);
leejieun 0:a69af42d193a 120 myButton.rise(&Btn1Up);
leejieun 0:a69af42d193a 121 exButton.fall(&Btn2Down);
leejieun 0:a69af42d193a 122 exButton.rise(&Btn2Up);
leejieun 0:a69af42d193a 123
leejieun 0:a69af42d193a 124 I2C I2C_Oled(PB_7, PA_15); // SDA, SCL
leejieun 0:a69af42d193a 125 I2C_Oled.frequency(400000); // 400kHz clock
leejieun 0:a69af42d193a 126 Adafruit_SSD1306_I2c myOled(I2C_Oled, PD_2, 0x78, 64, 128); // reset pin doesn't effect
leejieun 0:a69af42d193a 127
leejieun 0:a69af42d193a 128 myOled.splash(); // logo output
leejieun 0:a69af42d193a 129 myOled.display();
leejieun 0:a69af42d193a 130 wait(0.5);
leejieun 0:a69af42d193a 131
leejieun 0:a69af42d193a 132 myOled.clearDisplay(); // clear buffer
leejieun 0:a69af42d193a 133 myOled.printf("%u x %u OLED Display\r\n", myOled.width(), myOled.height());
leejieun 0:a69af42d193a 134 myOled.display(); // show a image on the OLED
leejieun 0:a69af42d193a 135 wait(1);
leejieun 0:a69af42d193a 136
leejieun 0:a69af42d193a 137 myButton.enable_irq(); // enable IRQ
leejieun 0:a69af42d193a 138 exButton.enable_irq(); // enable IRQ
leejieun 0:a69af42d193a 139
leejieun 0:a69af42d193a 140 t3.attach(&tickerFunc3, 0.1); // ticker3 start
leejieun 0:a69af42d193a 141
leejieun 0:a69af42d193a 142 time_t seconds = time(NULL);
leejieun 0:a69af42d193a 143
leejieun 0:a69af42d193a 144 set_time(1651121217); // Set RTC time to 2022-4월-28, PM 1:46:57
leejieun 0:a69af42d193a 145 pc.printf("Time as a basic string = %s", ctime(&seconds));
leejieun 0:a69af42d193a 146
leejieun 0:a69af42d193a 147 char buffer[32];
leejieun 0:a69af42d193a 148 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
leejieun 0:a69af42d193a 149 pc.printf("1) Time as a custom formatted string = %s", buffer);
leejieun 0:a69af42d193a 150 strftime(buffer, 32, "%y-%m-%d, %H:%M:%S\n", localtime(&seconds));
leejieun 0:a69af42d193a 151 pc.printf("2) Time as a custom formatted string = %s", buffer);
leejieun 0:a69af42d193a 152
leejieun 0:a69af42d193a 153 char tmpCommand[3];
leejieun 0:a69af42d193a 154 int rxVal;
leejieun 0:a69af42d193a 155 char val7Seg[NUM_CHAR] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
leejieun 0:a69af42d193a 156
leejieun 0:a69af42d193a 157 modeT3 = 1;
leejieun 0:a69af42d193a 158 my7Seg = 0xFF;
leejieun 0:a69af42d193a 159
leejieun 0:a69af42d193a 160 // 엔이 여깄음
leejieun 0:a69af42d193a 161 uint16_t n = 0;
leejieun 0:a69af42d193a 162
leejieun 0:a69af42d193a 163 //커서 설정
leejieun 0:a69af42d193a 164 int cursor = 0;
leejieun 0:a69af42d193a 165
leejieun 0:a69af42d193a 166 //에딧 모드 설정
leejieun 0:a69af42d193a 167 int edit = 0;
leejieun 0:a69af42d193a 168
leejieun 0:a69af42d193a 169 //뇌절
leejieun 0:a69af42d193a 170 int brain = 0;
leejieun 0:a69af42d193a 171 while(1)
leejieun 0:a69af42d193a 172 {
leejieun 0:a69af42d193a 173
leejieun 0:a69af42d193a 174 //if (1 == flagT3) {
leejieun 0:a69af42d193a 175 //flagT3 = 0;
leejieun 0:a69af42d193a 176 //n = n + dir;
leejieun 0:a69af42d193a 177 //myOled.clearDisplay();
leejieun 0:a69af42d193a 178 //myOled.setTextCursor(1, 5);
leejieun 0:a69af42d193a 179 //myOled.printf("%S",buffer);
leejieun 0:a69af42d193a 180 //myOled.display();
leejieun 0:a69af42d193a 181 //my7Seg = ~val7Seg[n%16] & 0x7F;
leejieun 0:a69af42d193a 182 //}
leejieun 0:a69af42d193a 183
leejieun 0:a69af42d193a 184 if (1 == flagRx){
leejieun 0:a69af42d193a 185 flagRx = 0;
leejieun 0:a69af42d193a 186 tmpCommand[0] = rxData[0];
leejieun 0:a69af42d193a 187 tmpCommand[1] = rxData[1];
leejieun 0:a69af42d193a 188 tmpCommand[2] = 0;
leejieun 0:a69af42d193a 189 rxVal = atoi(rxData+2);
leejieun 0:a69af42d193a 190
leejieun 0:a69af42d193a 191 if (0 == strcmp(tmpCommand, "LD")) { // control a LED
leejieun 0:a69af42d193a 192 pc.printf("val = %d\n", rxVal);
leejieun 0:a69af42d193a 193
leejieun 0:a69af42d193a 194 greenLed = (1 == rxVal)? 1:0;
leejieun 0:a69af42d193a 195 // greenLed = rxVal? 1:0;
leejieun 0:a69af42d193a 196 }
leejieun 0:a69af42d193a 197 if (0 == strcmp(tmpCommand, "RE")) { // reset all variables
leejieun 0:a69af42d193a 198 greenLed = 0;
leejieun 0:a69af42d193a 199 redLed = 1;
leejieun 0:a69af42d193a 200 n = 0;
leejieun 0:a69af42d193a 201 }
leejieun 0:a69af42d193a 202 //greenLed = !greenLed;
leejieun 0:a69af42d193a 203 //time_t seconds = time(NULL);
leejieun 0:a69af42d193a 204
leejieun 0:a69af42d193a 205 //pc.printf("\nTime as seconds since Janurary 1, 1970 = %u\n", (unsigned int)seconds);
leejieun 0:a69af42d193a 206
leejieun 0:a69af42d193a 207 //pc.printf("Time as a basic string = %s" , ctime(&seconds));
leejieun 0:a69af42d193a 208
leejieun 0:a69af42d193a 209
leejieun 0:a69af42d193a 210
leejieun 0:a69af42d193a 211 }
leejieun 0:a69af42d193a 212
leejieun 0:a69af42d193a 213 greenLed = !greenLed;
leejieun 0:a69af42d193a 214 time_t seconds = time(NULL);
leejieun 0:a69af42d193a 215
leejieun 0:a69af42d193a 216 pc.printf("\nTime as seconds since Janurary 1, 1970 = %u\n", (unsigned int)seconds);
leejieun 0:a69af42d193a 217 pc.printf("Time as a basic string = %s" , ctime(&seconds));
leejieun 0:a69af42d193a 218
leejieun 0:a69af42d193a 219 //컴포트 마스터 나오는거
leejieun 0:a69af42d193a 220 char buffer[32];
leejieun 0:a69af42d193a 221 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
leejieun 0:a69af42d193a 222 pc.printf("1) Time as custom formatted string = %s", buffer);
leejieun 0:a69af42d193a 223 strftime(buffer, 32, "%y-%m-%d, %H:%M:%S\n", localtime(&seconds));
leejieun 0:a69af42d193a 224 pc.printf("2) Time as a custom formatted string = %s", buffer);
leejieun 0:a69af42d193a 225
leejieun 0:a69af42d193a 226
leejieun 0:a69af42d193a 227 // 오엘이디 나오는거 평상시
leejieun 0:a69af42d193a 228 if (1 == flagT3) {
leejieun 0:a69af42d193a 229 flagT3 = 0;
leejieun 0:a69af42d193a 230 n = n + dir;
leejieun 0:a69af42d193a 231 myOled.clearDisplay();
leejieun 0:a69af42d193a 232 myOled.setTextCursor(1, 5);
leejieun 0:a69af42d193a 233 myOled.printf("%S",buffer);
leejieun 0:a69af42d193a 234 pc.printf(buffer);
leejieun 0:a69af42d193a 235 myOled.display();
leejieun 0:a69af42d193a 236 my7Seg = ~val7Seg[n%16] & 0x7F;
leejieun 0:a69af42d193a 237 }
leejieun 0:a69af42d193a 238
leejieun 0:a69af42d193a 239
leejieun 0:a69af42d193a 240 // 2초 이상 누른거 (버튼 1)
leejieun 0:a69af42d193a 241 if (2 == flagDown) {
leejieun 0:a69af42d193a 242 edit = edit + 1;
leejieun 0:a69af42d193a 243 }
leejieun 0:a69af42d193a 244 if (edit == 2){
leejieun 0:a69af42d193a 245 edit = 0;
leejieun 0:a69af42d193a 246 }
leejieun 0:a69af42d193a 247 switch(edit){
leejieun 0:a69af42d193a 248 case 0:
leejieun 0:a69af42d193a 249 pc.puts("edit mode off");
leejieun 0:a69af42d193a 250 flagDown = 0;
leejieun 0:a69af42d193a 251 brain = 0;
leejieun 0:a69af42d193a 252 break;
leejieun 0:a69af42d193a 253 case 1:
leejieun 0:a69af42d193a 254 pc.puts("edit mode on");
leejieun 0:a69af42d193a 255 brain = 1;
leejieun 0:a69af42d193a 256 edit = 1;
leejieun 0:a69af42d193a 257 // flagDown = 0;
leejieun 0:a69af42d193a 258 // currentLED = !currentLED;
leejieun 0:a69af42d193a 259 // pc.puts(selLED[currentLED]);
leejieun 0:a69af42d193a 260 {
leejieun 0:a69af42d193a 261
leejieun 0:a69af42d193a 262 // 오엘이디 외부 버튼을 눌렀다.
leejieun 0:a69af42d193a 263 // 살짝 누른거 (버튼 1)
leejieun 0:a69af42d193a 264 flagDown = 0;
leejieun 0:a69af42d193a 265 break;
leejieun 0:a69af42d193a 266 }
leejieun 0:a69af42d193a 267 if (1 == brain && edit == 1) {
leejieun 0:a69af42d193a 268 flagDown = 0;
leejieun 0:a69af42d193a 269 pc.puts("Cursor Select.\n");
leejieun 0:a69af42d193a 270 // float tempVal = blinkSpeed[currentLED];
leejieun 0:a69af42d193a 271 // tempVal = tempVal + 0.1f;
leejieun 0:a69af42d193a 272 // if (tempVal > 1.0f) tempVal = 1.0f;
leejieun 0:a69af42d193a 273 // if (0 == currentLED) t3.attach(&BlinkLED1, tempVal);
leejieun 0:a69af42d193a 274 // else t4.attach(&BlinkLED2, tempVal);
leejieun 0:a69af42d193a 275 // blinkSpeed[currentLED] = tempVal;
leejieun 0:a69af42d193a 276
leejieun 0:a69af42d193a 277 switch(cursor){
leejieun 0:a69af42d193a 278 case 0:
leejieun 0:a69af42d193a 279 pc.puts(" second.\n");
leejieun 0:a69af42d193a 280 break;
leejieun 0:a69af42d193a 281 case 1:
leejieun 0:a69af42d193a 282 pc.puts(" minute.\n");
leejieun 0:a69af42d193a 283 break;
leejieun 0:a69af42d193a 284 case 2:
leejieun 0:a69af42d193a 285 pc.puts(" hour.\n");
leejieun 0:a69af42d193a 286 break;
leejieun 0:a69af42d193a 287 case 3:
leejieun 0:a69af42d193a 288 pc.puts(" day.\n");
leejieun 0:a69af42d193a 289 break;
leejieun 0:a69af42d193a 290 case 4:
leejieun 0:a69af42d193a 291 pc.puts(" month.\n");
leejieun 0:a69af42d193a 292 break;
leejieun 0:a69af42d193a 293 case 5:
leejieun 0:a69af42d193a 294 pc.puts(" year.\n");
leejieun 0:a69af42d193a 295 break;
leejieun 0:a69af42d193a 296 }
leejieun 0:a69af42d193a 297 brain = 0;
leejieun 0:a69af42d193a 298 cursor += 1;
leejieun 0:a69af42d193a 299 if (cursor == 6){
leejieun 0:a69af42d193a 300 cursor = 0;
leejieun 0:a69af42d193a 301 }
leejieun 0:a69af42d193a 302
leejieun 0:a69af42d193a 303 //에딧 모드 끄기
leejieun 0:a69af42d193a 304
leejieun 0:a69af42d193a 305 }//큰 이프 닫는애
leejieun 0:a69af42d193a 306 }
leejieun 0:a69af42d193a 307
leejieun 0:a69af42d193a 308
leejieun 0:a69af42d193a 309
leejieun 0:a69af42d193a 310 // 살짝 누른거 (버튼 2)
leejieun 0:a69af42d193a 311 if (1 == flagUp) {
leejieun 0:a69af42d193a 312 flagUp = 0;
leejieun 0:a69af42d193a 313 pc.puts("Speed increase\n");
leejieun 0:a69af42d193a 314 // float tempVal = blinkSpeed[currentLED];
leejieun 0:a69af42d193a 315 // tempVal = tempVal - 0.1f;
leejieun 0:a69af42d193a 316 // if (tempVal < 0.1f) tempVal = 0.1f;
leejieun 0:a69af42d193a 317 // if (0 == currentLED) t3.attach(&BlinkLED1, tempVal);
leejieun 0:a69af42d193a 318 // else t4.attach(&BlinkLED2, tempVal);
leejieun 0:a69af42d193a 319 // blinkSpeed[currentLED] = tempVal;
leejieun 0:a69af42d193a 320 }
leejieun 0:a69af42d193a 321 // 2초 이상 누른거 (버튼 2)
leejieun 0:a69af42d193a 322 else if (2 == flagUp) {
leejieun 0:a69af42d193a 323 // flagUp = 0;
leejieun 0:a69af42d193a 324 // blinkSpeed[currentLED] = 0.5;
leejieun 0:a69af42d193a 325 // if (0 == currentLED) t3.attach(&BlinkLED1, 0.5);
leejieun 0:a69af42d193a 326 // else t4.attach(&BlinkLED2, 0.5);
leejieun 0:a69af42d193a 327 // pc.puts(selLED[currentLED]);
leejieun 0:a69af42d193a 328 pc.puts(" : the speed is reset to 0.5s\n");
leejieun 0:a69af42d193a 329
leejieun 0:a69af42d193a 330 }
leejieun 0:a69af42d193a 331 wait(1);
leejieun 0:a69af42d193a 332
leejieun 0:a69af42d193a 333
leejieun 0:a69af42d193a 334
leejieun 0:a69af42d193a 335
leejieun 0:a69af42d193a 336 }
leejieun 0:a69af42d193a 337
leejieun 0:a69af42d193a 338 }
leejieun 0:a69af42d193a 339
leejieun 0:a69af42d193a 340 // 버튼1을 2초 이상 누르면 LED 변환 (led1, led2 선택)
leejieun 0:a69af42d193a 341 // 버튼2를 2초 이상 누르면 현재 LED 초기화 (초기 깜빡임 속도 0.5초)
leejieun 0:a69af42d193a 342 // 버튼1을 살짝 누르면 깜빡임 속도 감소, 버튼2를 살짝 누르면 깜빡임 속도 증가 (0.1초 ~ 1초, 0.1초 단계)
leejieun 0:a69af42d193a 343
leejieun 0:a69af42d193a 344
leejieun 0:a69af42d193a 345