lestnica_dvizh/pkg/app/main.go

139 строки
2,7 КиБ
Go

package app
import (
"my/go-controller/analog"
"my/go-controller/digital"
"my/go-controller/serial"
"my/go-controller/task"
"my/go-controller/timer"
)
// 150 - примерно то же, что и 255 (должно быть те 1.25В где-то там же)
// 100 - средненько
// 50 - приглушённо
const MAX_YARKOST int = 25
const MIN_YARKOST int = 15
const VREMYA_VKL int = 2000
const VREMYA_SVETA uint32 = 10000
const VREMYA_VYKL int = 10000
const DIFF_YARKOST int = MAX_YARKOST - MIN_YARKOST
var loopBlockTime task.TickType = task.MS_TO_TICKS(1000)
var vyklYarkBlockTime task.TickType = task.MS_TO_TICKS(VREMYA_VYKL / DIFF_YARKOST)
var yarkost int
var kogdaVyklyuchit uint32
func initSvet() {
yarkost = MIN_YARKOST
kogdaVyklyuchit = 0
Vyklyuchit()
}
func DvizhEst() {
kogdaVyklyuchit = timer.Millis() + VREMYA_SVETA
if DEBUG {
serial.Println("Vkl svet")
}
PlavnoVklyuchit()
}
func DvizhaNet() {
if kogdaVyklyuchit > timer.Millis() {
return
}
if Vklyucheno() {
if DEBUG {
serial.Println("Vykl")
}
PlavnoVyklyuchit()
}
}
func PlavnoVklyuchit() {
Enable()
for yarkost = yarkost; yarkost <= MAX_YARKOST; yarkost++ {
Yarkost(yarkost)
if DEBUG {
serial.Println(yarkost)
}
timer.Delay(VREMYA_VKL / DIFF_YARKOST)
}
Vklyuchit()
}
func PlavnoVyklyuchit() {
var v int
for yarkost = yarkost; yarkost >= MIN_YARKOST; yarkost-- {
Yarkost(yarkost)
if DEBUG {
serial.Println(yarkost)
}
v = task.TaskNotifyTake(0, vyklYarkBlockTime)
if v > 0 {
DvizhEst()
return
}
}
Vyklyuchit()
}
func Vklyucheno() bool {
return yarkost > MIN_YARKOST
}
func Vklyuchit() {
Yarkost(MAX_YARKOST)
Enable()
}
func Vyklyuchit() {
Yarkost(MIN_YARKOST)
Disable()
}
func Yarkost(i int) {
// логика яркости инвертирована
analog.Write(SVET_YARK_PIN, 255-i)
}
func Enable() {
digital.Write(SVET_ONOFF_PIN, 1)
}
func Disable() {
digital.Write(SVET_ONOFF_PIN, 0)
}
func TaskSveta() {
var v int
PropustitRaz()
for {
/* Block to wait for a notification. Here the RTOS
task notification is being used as a counting semaphore.
The task's notification value is incremented each time
the ISR calls vTaskNotifyGiveFromISR(), and decremented
each time the RTOS task calls ulTaskNotifyTake() - so in
effect holds a count of the number of outstanding interrupts.
The first parameter is set to pdFALSE, so the notification
value is only decremented and not cleared to zero, and one
deferred interrupt event is processed at a time. */
v = task.TaskNotifyTake(0, loopBlockTime)
if v > 0 {
DvizhEst()
} else {
DvizhaNet()
}
}
}
func PropustitRaz() {
task.TaskNotifyTake(0, loopBlockTime)
}