From 246df58947223991b912f577f0ae98fa6ab9a8f7 Mon Sep 17 00:00:00 2001 From: unlockable Date: Sat, 9 Sep 2023 22:58:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95clock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 8By8/8By8.ino | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/8By8/8By8.ino b/8By8/8By8.ino index 19f6a3c..13513b8 100644 --- a/8By8/8By8.ino +++ b/8By8/8By8.ino @@ -844,6 +844,12 @@ enum Modes { Words, }; +enum ClockStatus { + Normal, + AdjustingHour, + AdjustingMinute, +} + int Cube::layer_count = 0; int Cube::brightness_count = 0; uint16_t Cube::LED_status[8][8] = {0}; @@ -874,6 +880,10 @@ byte cube_step = 1; bool rain_create_new = false; +int clock_hour = 0, clock_minute = 0, clock_sec = 0, clock_sec_flip = 0; +ClockStatus clock_status; +bool clock_waiting_for_new_command = true; + void setup() { for (int i = 22; i < 46; i++) { pinMode(i, OUTPUT); @@ -1021,6 +1031,18 @@ ISR(TIMER4_COMPA_vect) { ISR(TIMER5_COMPA_vect) { SensorReader::read_data_from_sensor_interrupt(); + + if (clock_status == ClockStatus::Normal) { + clock_sec_flip++; + clock_sec += clock_sec_flip / 4; + clock_minute += clock_sec / 60; + clock_hour += clock_minute / 60; + + clock_sec_flip %= 4; + clock_sec %= 60; + clock_minute %= 60; + clock_hour %= 24; + } } void cube() { @@ -1089,7 +1111,34 @@ void rain() { } void clock() { - return; + int VRX_status = analogRead(JOYSTICK_VRX) + if (clock_waiting_for_new_command) { + if (clock_status == ClockStatus::AdjustingHour) { + if (VRX_status < 50) { + clock_hour = (clock_hour + 23) % 24; + } + else if (VRX_status > 974) { + clock_hour = (clock_hour + 1) % 24; + } + } + else if (clock_status == ClockStatus::AdjustingMinute) { + if (VRX_status < 50) { + clock_minute = (clock_minute + 59) % 60; + } + else if (VRX_status > 974) { + clock_minute = (clock_minute + 1) % 60; + } + } + } else { + if (VRX_status > 400 && VRX_status < 624) { + clock_waiting_for_new_command = true; + } + } + + Cube::draw_num(0, 1, 7, clock_hour / 10, 2, 3); + Cube::draw_num(4, 1, 7, clock_hour % 10, 2, 3); + Cube::draw_num(0, 0, 3, clock_minute / 10, 2, 3); + Cube::draw_num(4, 0, 3, clock_minute % 10, 2, 3); } void words() { @@ -1318,6 +1367,44 @@ void on_joystick_button() { Serial.println("Reset position."); return; } + if (current_mode == Modes::Clock) { + switch (clock_status) { + case ClockStatus::Normal: { + for (int i = 0; i < 8; i++) { + for (int j = 4; j < 8; j++) { + Cube::LED_blinking_status[i][j] = 0xff; + } + } + clock_status = ClockStatus::AdjustingHour; + break; + } + case ClockStatus::AdjustingHour: { + for (int i = 0; i < 8; i++) { + for (int j = 4; j < 8; j++) { + Cube::LED_blinking_status[i][j] = 0; + } + for (int j = 0; j < 4; j++) { + Cube::LED_blinking_status[i][j] = 0xff; + } + } + clock_status = ClockStatus::AdjustingMinute; + break; + } + case ClockStatus::AdjustingMinute: { + for (int i = 0; i < 8; i++) { + for (int j = 0; j < 4; j++) { + Cube::LED_blinking_status[i][j] = 0; + } + } + clock_sec = 0; + clock_sec_flip = 0; + clock_status = ClockStatus::Normal; + break; + } + default: + clock_status = ClockStatus::Normal; + } + } } String display_mode_name(Modes mode_name) {