Browse Source

Remove test window from test func; update Battery + Clock to use new UI

lib
master
Adam Pippin 4 years ago
parent
commit
1e0c57b788
  1. 91
      Battery.cpp
  2. 102
      Clock.cpp
  3. 3
      watchos.ino

91
Battery.cpp

@ -1,21 +1,20 @@
#include <M5StickC.h> #include <M5StickC.h>
#include "Kernel.h" #include "Kernel.h"
#include "UI.h"
#include "Util.h" #include "Util.h"
int voltage_to_percent(float voltage); int voltage_to_percent(float voltage);
void draw(int bars); void Battery_draw(int hwnd, int x, int y, int width, int height);
#define BATTERY_OFFSET_X 141 #define BATTERY_BARS 4
#define BATTERY_OFFSET_Y 1 #define BATTERY_TERMINAL_HEIGHT_PCNT 20
#define BATTERY_WIDTH 17 #define BATTERY_TERMINAL_WIDTH_PCNT 5
#define BATTERY_HEIGHT 8 #define BATTERY_PADDING_PCNT 5
#define BATTERY_BARS 4
#define BATTERY_TERMINAL_HEIGHT 4 #define BATTERY_COLOUR 0x999999
#define BATTERY_TERMINAL_WIDTH 2 #define BATTERY_LOW_COLOUR 0xAA0000
#define BATTERY_COLOUR 0x999999 #define BATTERY_BAR_COLOUR 0xAAAAAA
#define BATTERY_LOW_COLOUR 0xAA0000 #define BATTERY_BAR_LOW_COLOUR 0xFF0000
#define BATTERY_PADDING 1
#define BATTERY_BAR_COLOUR 0xAAAAAA
int battery_bars = -1; int battery_bars = -1;
@ -23,7 +22,8 @@ int Battery(int pid, unsigned int signal)
{ {
if (signal & SIGNAL_START) if (signal & SIGNAL_START)
{ {
Kernel_signal_mask(pid, SIGNAL_START | SIGNAL_STOP | SIGNAL_TICK | SIGNAL_REDRAW); Kernel_signal_mask(pid, SIGNAL_START | SIGNAL_STOP | SIGNAL_TICK);
UI_create_window(&Battery_draw, 1);
} }
if (signal & SIGNAL_STOP) if (signal & SIGNAL_STOP)
{ {
@ -41,55 +41,56 @@ int Battery(int pid, unsigned int signal)
} }
} }
if (signal & SIGNAL_REDRAW)
{
draw(battery_bars);
}
return 0; return 0;
} }
void draw(int bars) void Battery_draw(int hwnd, int wnd_x, int wnd_y, int wnd_width, int wnd_height)
{ {
// Draw terminal // Draw terminal
int terminal_width = wnd_width * (BATTERY_TERMINAL_WIDTH_PCNT / 100.0);
int terminal_height = wnd_height * (BATTERY_TERMINAL_HEIGHT_PCNT / 100.0);
M5.Lcd.fillRect( M5.Lcd.fillRect(
BATTERY_OFFSET_X, wnd_x,
BATTERY_OFFSET_Y + (BATTERY_HEIGHT / 2) - (BATTERY_TERMINAL_HEIGHT / 2), wnd_y + (wnd_height / 2) - (terminal_height / 2),
BATTERY_TERMINAL_WIDTH, terminal_width,
BATTERY_TERMINAL_HEIGHT, terminal_height,
bars == 0 ? rgb_to_colour(BATTERY_LOW_COLOUR) : rgb_to_colour(BATTERY_COLOUR) battery_bars == 0 ? rgb_to_colour(BATTERY_LOW_COLOUR) : rgb_to_colour(BATTERY_COLOUR)
); );
// Draw battery shell // Draw battery shell
M5.Lcd.drawRect( M5.Lcd.fillRect(
BATTERY_OFFSET_X + BATTERY_TERMINAL_WIDTH, wnd_x + terminal_width,
BATTERY_OFFSET_Y, wnd_y,
BATTERY_WIDTH - BATTERY_TERMINAL_WIDTH, wnd_width - terminal_width,
BATTERY_HEIGHT, wnd_height,
bars == 0 ? rgb_to_colour(BATTERY_LOW_COLOUR) : rgb_to_colour(BATTERY_COLOUR) battery_bars == 0 ? rgb_to_colour(BATTERY_LOW_COLOUR) : rgb_to_colour(BATTERY_COLOUR)
); );
// Do some math
int padding_horizontal = wnd_width * (BATTERY_PADDING_PCNT / 100.0);
int padding_vertical = wnd_height * (BATTERY_PADDING_PCNT / 100.0);
int bar_width = (wnd_width - terminal_width - 2 - (padding_horizontal * (BATTERY_BARS + 1))) / BATTERY_BARS;
int bar_height = wnd_height - (padding_vertical * 2) - 2;
// -2: for frame int bar_x = wnd_x + wnd_width - 1 - padding_horizontal - bar_width;
int bar_width = (BATTERY_WIDTH - BATTERY_TERMINAL_WIDTH - 2 - (BATTERY_PADDING * (BATTERY_BARS + 1))) / BATTERY_BARS; int bar_y = wnd_y + 1 + padding_vertical;
int bar_height = BATTERY_HEIGHT - (BATTERY_PADDING * 2) - 2;
int x = BATTERY_OFFSET_X + BATTERY_WIDTH - 1 - BATTERY_PADDING - bar_width;
int y = BATTERY_OFFSET_Y + 1 + BATTERY_PADDING;
// Draw battery bars
int bars = battery_bars;
Serial.printf("Drawing %d bars\n", bars);
while (bars > 0) while (bars > 0)
{ {
M5.Lcd.fillRect( M5.Lcd.fillRect(
x, bar_x,
y, bar_y,
bar_width, bar_width,
bar_height, bar_height,
rgb_to_colour(BATTERY_BAR_COLOUR) battery_bars == 0 ? rgb_to_colour(BATTERY_BAR_LOW_COLOUR) : rgb_to_colour(BATTERY_BAR_COLOUR)
); );
x -= (bar_width + BATTERY_PADDING); bar_x -= (bar_width + padding_horizontal);
bars--; bars--;
} }
//M5.Lcd.setCursor(80, 20);
//M5.Lcd.print(bars);
} }
int voltage_to_percent(float voltage) int voltage_to_percent(float voltage)

102
Clock.cpp

@ -2,28 +2,25 @@
#include "Kernel.h" #include "Kernel.h"
#include "EAT.h" #include "EAT.h"
#include "Util.h" #include "Util.h"
#include "UI.h"
#define CLOCK_OFFSET_X 1 #define CLOCK_FACE_OUTLINE_SIZE_PCNT 4
#define CLOCK_OFFSET_Y 1 #define CLOCK_FACE_OUTLINE_COLOUR 0x666666
#define CLOCK_SIZE 78 #define CLOCK_FACE_COLOUR 0xAAAAAA
#define CLOCK_FACE_COLOUR 0xAAAAAA #define CLOCK_FACE_MARKER_COLOUR 0x666666
#define CLOCK_FACE_OUTLINE_COLOUR 0x666666 #define CLOCK_FACE_MARKER_TOP_COLOUR 0x333333
#define CLOCK_FACE_OUTLINE_WIDTH 4 #define CLOCK_FACE_MARKER_SIZE_PCNT 10
#define CLOCK_FACE_HOUR_MARKER_TOP_COLOUR 0x333333 #define CLOCK_HOUR_LENGTH_PCNT 20
#define CLOCK_FACE_HOUR_MARKER_COLOUR 0x666666 #define CLOCK_HOUR_COLOUR 0x333333
#define CLOCK_FACE_HOUR_MARKER_SIZE 4 #define CLOCK_MINUTE_LENGTH_PCNT 30
#define CLOCK_HOUR_COLOUR 0x333333 #define CLOCK_MINUTE_COLOUR 0x666666
#define CLOCK_HOUR_LENGTH 20
#define CLOCK_MINUTE_COLOUR 0x666666
#define CLOCK_MINUTE_LENGTH 25
#define DEGREES_TO_RADIANS 0.0174533 #define DEGREES_TO_RADIANS 0.0174533
RTC_TimeTypeDef time_to_rtc(char* time); RTC_TimeTypeDef time_to_rtc(char* time);
RTC_DateTypeDef date_to_rtc(char* date); RTC_DateTypeDef date_to_rtc(char* date);
void redraw(RTC_TimeTypeDef time, RTC_DateTypeDef date); void Clock_draw(int hwnd, int wnd_x, int wnd_y, int wnd_width, int wnd_height);
void draw_face(); void Clock_draw_arm(int center_x, int center_y, int angle, int length, unsigned int colour);
unsigned int rgb_to_colour(unsigned long colour);
int last_minute = -1; int last_minute = -1;
@ -31,7 +28,8 @@ int Clock(int pid, unsigned int signal)
{ {
if (signal & SIGNAL_START) if (signal & SIGNAL_START)
{ {
Kernel_signal_mask(pid, SIGNAL_START | SIGNAL_STOP | SIGNAL_TICK | SIGNAL_REDRAW); Kernel_signal_mask(pid, SIGNAL_START | SIGNAL_STOP | SIGNAL_TICK);
UI_create_window(&Clock_draw, 1);
RTC_TimeTypeDef compile_time = time_to_rtc(__TIME__); RTC_TimeTypeDef compile_time = time_to_rtc(__TIME__);
RTC_DateTypeDef compile_date = date_to_rtc(__DATE__); RTC_DateTypeDef compile_date = date_to_rtc(__DATE__);
@ -61,73 +59,86 @@ int Clock(int pid, unsigned int signal)
return 255; return 255;
} }
if (signal & SIGNAL_TICK || signal & SIGNAL_REDRAW) if (signal & SIGNAL_TICK)
{ {
RTC_TimeTypeDef time; RTC_TimeTypeDef time;
M5.Rtc.GetTime(&time); M5.Rtc.GetTime(&time);
if (signal & SIGNAL_TICK && time.Minutes != last_minute) if (time.Minutes != last_minute)
{ {
last_minute = time.Minutes; last_minute = time.Minutes;
Kernel_signal(SIGNAL_REDRAW); Kernel_signal(SIGNAL_REDRAW);
} }
if (signal & SIGNAL_REDRAW)
{
RTC_DateTypeDef date;
M5.Rtc.GetData(&date);
redraw(time, date);
}
} }
return 0; return 0;
} }
void draw_face() void Clock_draw(int hwnd, int wnd_x, int wnd_y, int wnd_width, int wnd_height)
{ {
int center_x = CLOCK_OFFSET_X + (CLOCK_SIZE / 2); RTC_TimeTypeDef time;
int center_y = CLOCK_OFFSET_Y + (CLOCK_SIZE / 2); M5.Rtc.GetTime(&time);
int radius = CLOCK_SIZE / 2; RTC_DateTypeDef date;
int inner_radius = radius - (CLOCK_FACE_OUTLINE_WIDTH / 2); M5.Rtc.GetData(&date);
int size = 0;
if (wnd_width < wnd_height)
{
size = wnd_width;
}
else
{
size = wnd_height;
}
int center_x = wnd_x + (size / 2);
int center_y = wnd_y + (size / 2);
int radius = size / 2;
int outline_size = size * (CLOCK_FACE_OUTLINE_SIZE_PCNT / 100.0);
int inner_radius = radius - (outline_size / 2);
// Draw face // Draw face
M5.Lcd.fillCircle(center_x, center_y, radius, rgb_to_colour(CLOCK_FACE_OUTLINE_COLOUR)); M5.Lcd.fillCircle(center_x, center_y, radius, rgb_to_colour(CLOCK_FACE_OUTLINE_COLOUR));
M5.Lcd.fillCircle(center_x, center_y, inner_radius, rgb_to_colour(CLOCK_FACE_COLOUR)); M5.Lcd.fillCircle(center_x, center_y, inner_radius, rgb_to_colour(CLOCK_FACE_COLOUR));
// Draw lines // Draw lines
int marker_size = size * (CLOCK_FACE_MARKER_SIZE_PCNT / 100.0);
for (int i=0; i<360; i+=(360/12)) for (int i=0; i<360; i+=(360/12))
{ {
// Effectively our polar coordinates are int x1 = (inner_radius - marker_size) * cos(i * DEGREES_TO_RADIANS);
// i,inner_radius -> i,inner_radius-CLOCK_FACE_HOUR_MARKER_SIZE int y1 = (inner_radius - marker_size) * sin(i * DEGREES_TO_RADIANS);
int x1 = (inner_radius - CLOCK_FACE_HOUR_MARKER_SIZE) * cos(i * DEGREES_TO_RADIANS);
int y1 = (inner_radius - CLOCK_FACE_HOUR_MARKER_SIZE) * sin(i * DEGREES_TO_RADIANS);
int x2 = (inner_radius + 1) * cos(i * DEGREES_TO_RADIANS); int x2 = (inner_radius + 1) * cos(i * DEGREES_TO_RADIANS);
int y2 = (inner_radius + 1) * sin(i * DEGREES_TO_RADIANS); int y2 = (inner_radius + 1) * sin(i * DEGREES_TO_RADIANS);
x1 += center_x; x1 += center_x;
x2 += center_x; x2 += center_x;
y1 += center_y; y1 += center_y;
y2 += center_y; y2 += center_y;
// 0 degrees = right, draw top at 270 // 0 degrees = right, draw top at 270
M5.Lcd.drawLine(x1, y1, x2, y2, i == 270 ? rgb_to_colour(CLOCK_FACE_HOUR_MARKER_TOP_COLOUR) : rgb_to_colour(CLOCK_FACE_HOUR_MARKER_COLOUR)); M5.Lcd.drawLine(x1, y1, x2, y2, i == 270 ? rgb_to_colour(CLOCK_FACE_MARKER_TOP_COLOUR) : rgb_to_colour(CLOCK_FACE_MARKER_COLOUR));
} }
int hour = time.Hours;
if (hour > 12) hour -= 12;
int minute = time.Minutes;
int minute_angle = 360.0 * (minute / 60.0);
int hour_angle = (360.0 * (hour / 12.0)) + (minute_angle / 12.0);
Clock_draw_arm(center_x, center_y, hour_angle, size * (CLOCK_HOUR_LENGTH_PCNT / 100.0), rgb_to_colour(CLOCK_HOUR_COLOUR));
Clock_draw_arm(center_x, center_y, minute_angle, size * (CLOCK_MINUTE_LENGTH_PCNT / 100.0), rgb_to_colour(CLOCK_MINUTE_COLOUR));
} }
void draw_arm(int angle, int length, unsigned int colour) void Clock_draw_arm(int center_x, int center_y, int angle, int length, unsigned int colour)
{ {
// 0 = right, adjust for 0=up
angle -= 90; angle -= 90;
// clock center
int center_x = CLOCK_OFFSET_X + (CLOCK_SIZE / 2);
int center_y = CLOCK_OFFSET_Y + (CLOCK_SIZE / 2);
int x = length * cos(angle * DEGREES_TO_RADIANS); int x = length * cos(angle * DEGREES_TO_RADIANS);
int y = length * sin(angle * DEGREES_TO_RADIANS); int y = length * sin(angle * DEGREES_TO_RADIANS);
M5.Lcd.drawLine(center_x, center_y, center_x + x, center_y + y, colour); M5.Lcd.drawLine(center_x, center_y, center_x + x, center_y + y, colour);
} }
/*
void redraw(RTC_TimeTypeDef time, RTC_DateTypeDef date) void redraw(RTC_TimeTypeDef time, RTC_DateTypeDef date)
{ {
draw_face(); draw_face();
@ -153,6 +164,7 @@ void redraw(RTC_TimeTypeDef time, RTC_DateTypeDef date)
//sprintf(time_str, "%02d:%02d", time.Hours, time.Minutes); //sprintf(time_str, "%02d:%02d", time.Hours, time.Minutes);
//M5.Lcd.print(time_str); //M5.Lcd.print(time_str);
} }
*/
RTC_TimeTypeDef time_to_rtc(char* time) RTC_TimeTypeDef time_to_rtc(char* time)
{ {

3
watchos.ino

@ -36,9 +36,6 @@ int test_func(int pid, unsigned int signal)
if (signal & SIGNAL_START) if (signal & SIGNAL_START)
{ {
Kernel_signal_mask(pid, SIGNAL_START | SIGNAL_STOP | SIGNAL_TICK | SIGNAL_INPUT_A | SIGNAL_INPUT_B); Kernel_signal_mask(pid, SIGNAL_START | SIGNAL_STOP | SIGNAL_TICK | SIGNAL_INPUT_A | SIGNAL_INPUT_B);
UI_set_layout_mode(1, LAYOUT_MODE_SPLIT_VERTICAL);
int hwnd = UI_create_window(&test_draw, 1);
hwnd = UI_create_window(&test_draw, 1);
if (EAT_allocate(1, 2)) if (EAT_allocate(1, 2))
{ {
EAT_write(1, 0, 0); EAT_write(1, 0, 0);

Loading…
Cancel
Save