-
Notifications
You must be signed in to change notification settings - Fork 4
/
ulp.py
116 lines (97 loc) · 3.18 KB
/
ulp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""
Module for including ultra low power functions of picocell module.
This module uses ultra lower power load switch and timer hardwares.
"""
import os
from machine import Pin
from core.temp import config, debug
from core.utils.helpers import read_json_file, write_json_file
from core.utils.status import Status
CONST_TIMER = 1 # minutes
DEFAULT_PERIOD = 60 # minutes
ULP_FILE_PATH = "ulp.json"
class ULP:
"""
Class for including ultra low power functions of picocell module.
"""
timer_done_pin = Pin(11, Pin.OUT)
def __init__(self):
"""
Constructor for ULP class
"""
config["ulp"] = {}
file_existence = str(os.listdir()).find(ULP_FILE_PATH)
if file_existence != -1:
ulp = read_json_file(ULP_FILE_PATH)
config["ulp"]["status"] = ulp.get("status", False)
config["ulp"]["timer_count"] = ulp.get("timer_count", 0)
config["ulp"]["desired_timer_count"] = ulp.get("desired_timer_count", DEFAULT_PERIOD)
else:
config["ulp"] = {}
config["ulp"]["status"] = False
config["ulp"]["timer_count"] = 0
config["ulp"]["desired_timer_count"] = DEFAULT_PERIOD
self.save_ulp_config()
def save_ulp_config(self):
"""
Function for saving ultra low power configuration
"""
try:
write_json_file(ULP_FILE_PATH, config["ulp"])
except Exception as error:
print(error)
return {"response" : str(error), "status" : Status.ERROR}
def enable(self):
"""
Function for enabling ultra low power shutdown
"""
config["ulp"]["status"] = True
self.save_ulp_config()
def disable(self):
"""
Function for disabling ultra low power shutdown
"""
config["ulp"]["status"] = False
self.save_ulp_config()
def set_deep_sleep_period(self, period=60):
"""
Function for setting timer period.
Parameters
----------
period : int
Timer period in minutes. Must be power of CONTS_TIMER. (default = 60)(1 hour)
"""
config["ulp"]["desired_timer_count"] = int(period / CONST_TIMER)
self.save_ulp_config()
def deep_sleep(self):
"""
Function for shutting down picocell module
"""
self.timer_done_pin.value(1)
def increase_counter(self):
"""
Function for increasing timer counter
"""
config["ulp"]["timer_count"] += 1
self.save_ulp_config()
def reset_counter(self):
"""
Function for resetting timer counter
"""
config["ulp"]["timer_count"] = 0
self.save_ulp_config()
def check(self):
"""
Function for checking timer done
"""
debug.print("ULP: Check")
if not config["ulp"].get("status", False):
return
count = config["ulp"].get("timer_count")
desired_count = config["ulp"].get("desired_timer_count")
if count < desired_count - 1 :
self.increase_counter()
self.deep_sleep()
else:
self.reset_counter()
# do the job