149 строки
3 КиБ
Go
149 строки
3 КиБ
Go
package app
|
|
|
|
import (
|
|
"my/go-controller/analog"
|
|
"my/go-controller/digital"
|
|
"my/go-controller/serial"
|
|
"my/go-controller/task"
|
|
"my/go-controller/timer"
|
|
)
|
|
|
|
const MAX_YARKOST uint32 = 2000
|
|
|
|
const MIN_YARKOST uint32 = 500
|
|
|
|
const VREMYA_VKL int = 2000
|
|
const VREMYA_SVETA uint32 = 30000
|
|
const VREMYA_VYKL int = 10000
|
|
|
|
const LEDC_CHANNEL_0 byte = 0
|
|
const LEDC_RESOLUTION_BITS int = 12
|
|
const MAX_CHANNEL_VALUE uint32 = 0xfff - 1
|
|
const LEDC_BASE_FREQ int = 5000
|
|
|
|
const DIFF_YARKOST int = 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 uint32
|
|
var kogdaVyklyuchit uint32
|
|
|
|
func initSvet() {
|
|
yarkost = MIN_YARKOST
|
|
kogdaVyklyuchit = 0
|
|
|
|
initLED()
|
|
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 initLED() {
|
|
analog.LEDcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_RESOLUTION_BITS)
|
|
analog.LEDcAttachPin(SVET_YARK_PIN, LEDC_CHANNEL_0)
|
|
}
|
|
func Yarkost(i uint32) {
|
|
// логика яркости инвертирована
|
|
// analog.Write(SVET_YARK_PIN)
|
|
analog.LEDcWrite(LEDC_CHANNEL_0, MAX_CHANNEL_VALUE-i)
|
|
}
|
|
func Enable() {
|
|
digital.Write(SVET_ONOFF_PIN, 1)
|
|
digital.PinMode(SVET_ONOFF_PIN, digital.ModeOutput)
|
|
}
|
|
func Disable() {
|
|
digital.PinMode(SVET_ONOFF_PIN, digital.ModeInput)
|
|
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)
|
|
}
|