Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
circuitpython/shared-bindings/alarm/SleepMemory.c at main · MicroPythonNexus/circuitpython · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
MicroPythonNexus
/
circuitpython
Public
forked from
adafruit/circuitpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
circuitpython
/
shared-bindings
/
alarm
/
SleepMemory.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
161 lines (151 loc) · 6.14 KB
main
Breadcrumbs
circuitpython
/
shared-bindings
/
alarm
/
SleepMemory.c
Copy path
Top
File metadata and controls
Code
Blame
161 lines (151 loc) · 6.14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
// SPDX-FileCopyrightText: Copyright (c) 2020 Dan Halbert for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include
"py/binary.h"
#include
"py/runtime.h"
#include
"py/runtime0.h"
#include
"shared-bindings/alarm/SleepMemory.h"
//| class SleepMemory:
//| """Store raw bytes in RAM that persists during deep sleep.
//| The class acts as a ``bytearray``.
//| If power is lost, the memory contents are lost.
//|
//| Note that this class can't be imported and used directly. The sole
//| instance of :class:`SleepMemory` is available at
//| :attr:`alarm.sleep_memory`.
//|
//| Usage::
//|
//| import alarm
//| alarm.sleep_memory[0] = True
//| alarm.sleep_memory[1] = 12
//| """
//|
//| def __init__(self) -> None:
//| """Not used. Access the sole instance through `alarm.sleep_memory`."""
//| ...
//|
//| def __bool__(self) -> bool:
//| """``sleep_memory`` is ``True`` if its length is greater than zero.
//| This is an easy way to check for its existence.
//| """
//| ...
//|
//| def __len__(self) -> int:
//| """Return the length. This is used by (`len`)"""
//| ...
//|
static
mp_obj_t
alarm_sleep_memory_unary_op
(
mp_unary_op_t
op
,
mp_obj_t
self_in
) {
alarm_sleep_memory_obj_t
*
self
=
MP_OBJ_TO_PTR
(
self_in
);
uint16_t
len
=
common_hal_alarm_sleep_memory_get_length
(
self
);
switch
(
op
) {
case
MP_UNARY_OP_BOOL
:
return
mp_obj_new_bool
(
len
!=
0
);
case
MP_UNARY_OP_LEN
:
return
MP_OBJ_NEW_SMALL_INT
(
len
);
default
:
return
MP_OBJ_NULL
;
// op not supported
}
}
static
const
mp_rom_map_elem_t
alarm_sleep_memory_locals_dict_table
[]
=
{
};
static
MP_DEFINE_CONST_DICT
(
alarm_sleep_memory_locals_dict
,
alarm_sleep_memory_locals_dict_table
);
//| @overload
//| def __getitem__(self, index: slice) -> bytearray: ...
//| @overload
//| def __getitem__(self, index: int) -> int:
//| """Returns the value at the given index."""
//| ...
//|
//| @overload
//| def __setitem__(self, index: slice, value: ReadableBuffer) -> None: ...
//| @overload
//| def __setitem__(self, index: int, value: int) -> None:
//| """Set the value at the given index."""
//| ...
//|
//|
static
mp_obj_t
alarm_sleep_memory_subscr
(
mp_obj_t
self_in
,
mp_obj_t
index_in
,
mp_obj_t
value
) {
if
(
value
==
MP_OBJ_NULL
) {
// delete item
// slice deletion
return
MP_OBJ_NULL
;
// op not supported
}
else
{
alarm_sleep_memory_obj_t
*
self
=
MP_OBJ_TO_PTR
(
self_in
);
if
(
0
) {
#if
MICROPY_PY_BUILTINS_SLICE
}
else
if
(
mp_obj_is_type
(
index_in
,
&
mp_type_slice
)) {
mp_bound_slice_t
slice
;
if
(!
mp_seq_get_fast_slice_indexes
(
common_hal_alarm_sleep_memory_get_length
(
self
),
index_in
,
&
slice
)) {
mp_raise_NotImplementedError
(
MP_ERROR_TEXT
(
"only slices with step=1 (aka None) are supported"
));
}
if
(
value
!=
MP_OBJ_SENTINEL
) {
#if
MICROPY_PY_ARRAY_SLICE_ASSIGN
// Assign
size_t
src_len
=
slice
.
stop
-
slice
.
start
;
uint8_t
*
src_items
;
if
(
mp_obj_is_type
(
value
,
&
mp_type_array
)
||
mp_obj_is_type
(
value
,
&
mp_type_bytearray
)
||
mp_obj_is_type
(
value
,
&
mp_type_memoryview
)
||
mp_obj_is_type
(
value
,
&
mp_type_bytes
)) {
mp_buffer_info_t
bufinfo
;
mp_get_buffer_raise
(
value
,
&
bufinfo
,
MP_BUFFER_READ
);
if
(
bufinfo
.
len
!=
src_len
) {
mp_raise_ValueError
(
MP_ERROR_TEXT
(
"Slice and value different lengths."
));
}
src_len
=
bufinfo
.
len
;
src_items
=
bufinfo
.
buf
;
if
(
1
!=
mp_binary_get_size
(
'@'
,
bufinfo
.
typecode
,
NULL
)) {
mp_raise_ValueError
(
MP_ERROR_TEXT
(
"Array values should be single bytes."
));
}
}
else
{
mp_raise_NotImplementedError
(
MP_ERROR_TEXT
(
"array/bytes required on right side"
));
}
if
(!
common_hal_alarm_sleep_memory_set_bytes
(
self
,
slice
.
start
,
src_items
,
src_len
)) {
mp_raise_RuntimeError
(
MP_ERROR_TEXT
(
"Unable to write to sleep_memory."
));
}
return
mp_const_none
;
#else
return
MP_OBJ_NULL
;
// op not supported
#endif
}
else
{
// Read slice.
size_t
len
=
slice
.
stop
-
slice
.
start
;
uint8_t
*
items
=
m_new
(
uint8_t
,
len
);
common_hal_alarm_sleep_memory_get_bytes
(
self
,
slice
.
start
,
items
,
len
);
return
mp_obj_new_bytearray_by_ref
(
len
,
items
);
}
#endif
}
else
{
// Single index rather than slice.
size_t
index
=
mp_get_index
(
self
->
base
.
type
,
common_hal_alarm_sleep_memory_get_length
(
self
),
index_in
, false);
if
(
value
==
MP_OBJ_SENTINEL
) {
// load
uint8_t
value_out
;
common_hal_alarm_sleep_memory_get_bytes
(
self
,
index
,
&
value_out
,
1
);
return
MP_OBJ_NEW_SMALL_INT
(
value_out
);
}
else
{
// store
mp_int_t
byte_value
=
mp_obj_get_int
(
value
);
mp_arg_validate_int_range
(
byte_value
,
0
,
255
,
MP_QSTR_bytes
);
uint8_t
short_value
=
byte_value
;
if
(!
common_hal_alarm_sleep_memory_set_bytes
(
self
,
index
,
&
short_value
,
1
)) {
mp_raise_RuntimeError
(
MP_ERROR_TEXT
(
"Unable to write to sleep_memory."
));
}
return
mp_const_none
;
}
}
}
}
MP_DEFINE_CONST_OBJ_TYPE
(
alarm_sleep_memory_type
,
MP_QSTR_SleepMemory
,
MP_TYPE_FLAG_NONE
,
locals_dict
,
&
alarm_sleep_memory_locals_dict
,
subscr
,
alarm_sleep_memory_subscr
,
unary_op
,
alarm_sleep_memory_unary_op
);
You can’t perform that action at this time.