Refactoring
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Jan Zípek 2024-06-06 14:34:20 +02:00
parent f50abec60f
commit 17e604a03f
Signed by: kamen
GPG Key ID: A17882625B33AC31
8 changed files with 180 additions and 79 deletions

View File

@ -50,12 +50,14 @@ void dow_get_text_item_rect(GxEPD2_BW<GxEPD2_583_GDEQ0583T31, GxEPD2_583_GDEQ058
{ {
case ITEM_ALIGN_CENTER: case ITEM_ALIGN_CENTER:
{ {
y += tby;
y += round((double)h / 2); y += round((double)h / 2);
break; break;
} }
case ITEM_ALIGN_START: case ITEM_ALIGN_START:
{ {
y += tby;
y += h; y += h;
break; break;
} }

View File

@ -3,22 +3,10 @@ import { route } from '../route'
import { router } from '../router' import { router } from '../router'
import { calendarService } from '../services/calendar' import { calendarService } from '../services/calendar'
import { weatherService } from '../services/weather' import { weatherService } from '../services/weather'
// import { getSunrise, getSunset } from 'sunrise-sunset-js' import { TYPES } from '../../lib/dow/constants'
import { ALIGN } from '../../lib/dow/constants'
const TYPES = { import { DowWidget } from '../../lib/dow/constants'
TEXT: 0, import { rectangleWithTitle } from '../../lib/dow/widgets/rectangleWithTitle'
LINE: 1,
CIRCLE: 2,
CIRCLE_FILL: 3,
RECT: 4,
RECT_FILL: 5,
}
const ALIGN = {
START: 0,
CENTER: 1,
END: 2,
}
const weekdaysInCs = [ const weekdaysInCs = [
'Pondeli', 'Pondeli',
@ -37,18 +25,6 @@ const CALENDARS = [
'primary', 'primary',
] ]
type Widget = {
x: number
y: number
x2?: number
y2?: number
c?: string
t: number
va?: number
ha?: number
f?: number
}
export const dowRoutes = router((router, ctx) => { export const dowRoutes = router((router, ctx) => {
router.get( router.get(
'/', '/',
@ -91,7 +67,7 @@ export const dowRoutes = router((router, ctx) => {
summary = summary.replace(/_day|_night/, '') summary = summary.replace(/_day|_night/, '')
} }
const widgets: Widget[] = [ const widgets: DowWidget[] = [
{ {
x: WIDTH / 2, x: WIDTH / 2,
y: 30, y: 30,
@ -220,31 +196,17 @@ export const dowRoutes = router((router, ctx) => {
const tempGraphP = 10 const tempGraphP = 10
const tempGraphW = tempW - tempGraphP * 2 const tempGraphW = tempW - tempGraphP * 2
widgets.push({ widgets.push(
x: tempX, ...rectangleWithTitle({
y: tempY - 40, x: tempX,
x2: tempW, y: tempY - 40,
y2: tempH, width: tempW,
t: TYPES.RECT, height: tempH,
}) title: 'Teplota',
titleHeight: 20,
widgets.push({ titleFont: 3,
x: tempX, }),
y: tempY - 20, )
x2: tempX + tempW,
y2: tempY - 20,
t: TYPES.LINE,
})
widgets.push({
x: Math.floor(tempX + tempW / 2),
y: tempY - 30,
t: TYPES.TEXT,
ha: ALIGN.CENTER,
va: ALIGN.CENTER,
c: 'Teplota',
f: 3,
})
for (const [offset, hour] of next12Hours) { for (const [offset, hour] of next12Hours) {
const offsetInt = +offset const offsetInt = +offset
@ -312,31 +274,17 @@ export const dowRoutes = router((router, ctx) => {
const preGraphP = 10 const preGraphP = 10
const preGraphW = preW - preGraphP * 2 const preGraphW = preW - preGraphP * 2
widgets.push({ widgets.push(
x: preX, ...rectangleWithTitle({
y: preY - 40, x: preX,
x2: preW, y: preY - 40,
y2: preH, width: preW,
t: TYPES.RECT, height: preH,
}) title: 'Srazky',
titleHeight: 20,
widgets.push({ titleFont: 3,
x: preX, }),
y: preY - 20, )
x2: preX + preW,
y2: preY - 20,
t: TYPES.LINE,
})
widgets.push({
x: Math.floor(preX + preW / 2),
y: preY - 30,
t: TYPES.TEXT,
ha: ALIGN.CENTER,
va: ALIGN.CENTER,
c: 'Dest',
f: 3,
})
for (const [offset, hour] of next12Hours) { for (const [offset, hour] of next12Hours) {
const offsetInt = +offset const offsetInt = +offset

View File

@ -0,0 +1,38 @@
export type DowWidget = {
x: number
y: number
x2?: number
y2?: number
c?: string
t: DowWidgetType
va?: DowWidgetAlign
ha?: DowWidgetAlign
f?: number
co?: DowWidgetColor
}
export type DowWidgetType = (typeof TYPES)[keyof typeof TYPES]
export const TYPES = {
TEXT: 0,
LINE: 1,
CIRCLE: 2,
CIRCLE_FILL: 3,
RECT: 4,
RECT_FILL: 5,
} as const
export type DowWidgetAlign = (typeof ALIGN)[keyof typeof ALIGN]
export const ALIGN = {
START: 0,
CENTER: 1,
END: 2,
} as const
export type DowWidgetColor = (typeof COLORS)[keyof typeof COLORS]
export const COLORS = {
BLACK: 0,
WHITE: 1,
}

View File

@ -0,0 +1,7 @@
import { DowWidget } from './constants'
export const defineWidget = <TOptions, TResult extends DowWidget | DowWidget[]>(
factory: (options: TOptions) => TResult,
) => {
return factory
}

View File

@ -0,0 +1,19 @@
import { DowWidgetColor, TYPES } from '../constants'
import { defineWidget } from '../defineWidget'
type Options = {
x: number
y: number
x2: number
y2: number
color?: DowWidgetColor
}
export const line = defineWidget(({ x, y, x2, y2, color }: Options) => ({
x,
y,
x2,
y2,
t: TYPES.LINE,
co: color,
}))

View File

@ -0,0 +1,22 @@
import { DowWidgetColor, TYPES } from '../constants'
import { defineWidget } from '../defineWidget'
type Options = {
x: number
y: number
width: number
height: number
filled?: boolean
color?: DowWidgetColor
}
export const rectangle = defineWidget(
({ x, y, width, height, filled, color }: Options) => ({
x,
y,
x2: width,
y2: height,
t: filled ? TYPES.RECT_FILL : TYPES.RECT,
co: color,
}),
)

View File

@ -0,0 +1,25 @@
import { DowWidgetAlign, DowWidgetColor, TYPES } from '../constants'
import { defineWidget } from '../defineWidget'
type Options = {
x: number
y: number
text: string
font: number
verticalAlign?: DowWidgetAlign
horizontalAlign?: DowWidgetAlign
color?: DowWidgetColor
}
export const text = defineWidget(
({ x, y, text, font, verticalAlign, horizontalAlign, color }: Options) => ({
x,
y,
t: TYPES.TEXT,
c: text,
va: verticalAlign,
ha: horizontalAlign,
f: font,
co: color,
}),
)

View File

@ -0,0 +1,40 @@
import { ALIGN, DowWidget } from '../constants'
import { defineWidget } from '../defineWidget'
import { line } from '../primitives/line'
import { rectangle } from '../primitives/rectangle'
import { text } from '../primitives/text'
type Options = {
x: number
y: number
width: number
height: number
title: string
titleHeight: number
titleFont: number
}
export const rectangleWithTitle = defineWidget(
({ x, y, width, height, title, titleFont, titleHeight }: Options) => {
const result = [] as DowWidget[]
result.push(rectangle({ x, y, width, height }))
result.push(
line({ x, y: y + titleHeight, x2: x + width, y2: y + titleHeight }),
)
result.push(
text({
x: Math.floor(x + width / 2),
y: y + titleHeight / 2,
horizontalAlign: ALIGN.CENTER,
verticalAlign: ALIGN.CENTER,
text: title,
font: titleFont,
}),
)
return result
},
)