6
6
#
7
7
# 2019-Mar-12 - v0.1 - Initial implementation
8
8
# 2019-May-20 - v1.0 - Initial Release
9
-
10
- """
11
- `tinypico` - MicroPython TinyPICO Helper Library
12
- ====================================================
13
-
14
- * Author(s): Seon Rozenblum, Matt Trentini
15
- """
16
-
17
- __version__ = "0.0.0-auto.0"
18
- __repo__ = "https://github.com/TinyPICO/tinypico-micropython"
9
+ # 2019-Oct-23 - v1.1 - Removed temp sensor code, prep for frozen modules
19
10
20
11
# Import required libraries
21
12
from micropython import const
38
29
SPI_CLK = const (18 )
39
30
SPI_MISO = const (19 )
40
31
41
- #I2C
32
+ # I2C
42
33
I2C_SDA = const (21 )
43
34
I2C_SCL = const (22 )
44
35
45
- #DAC
36
+ # DAC
46
37
DAC1 = const (25 )
47
38
DAC2 = const (26 )
48
39
@@ -56,12 +47,13 @@ def get_battery_voltage():
56
47
Returns the current battery voltage. If no battery is connected, returns 3.7V
57
48
This is an approximation only, but useful to detect of the charge state of the battery is getting low.
58
49
"""
59
- adc = ADC ( Pin ( BAT_VOLTAGE ) ) # Assign the ADC pin to read
60
- measuredvbat = adc .read () # Read the value
61
- measuredvbat /= 4095 # divide by 4095 as we are using the default ADC voltage range of 0-1V
62
- measuredvbat *= 3.7 # Multiply by 3.7V, our reference voltage
50
+ adc = ADC (Pin (BAT_VOLTAGE )) # Assign the ADC pin to read
51
+ measuredvbat = adc .read () # Read the value
52
+ measuredvbat /= 4095 # divide by 4095 as we are using the default ADC voltage range of 0-1V
53
+ measuredvbat *= 3.7 # Multiply by 3.7V, our reference voltage
63
54
return measuredvbat
64
55
56
+
65
57
# Return the current charge state of the battery - we need to read the value multiple times
66
58
# to eliminate false negatives due to the charge IC not knowing the difference between no battery
67
59
# and a full battery not charging - This is why the charge LED flashes
@@ -70,66 +62,61 @@ def get_battery_charging():
70
62
Returns the current battery charging state.
71
63
This can trigger false positives as the charge IC can't tell the difference between a full battery or no battery connected.
72
64
"""
73
- measuredVal = 0 # start our reading at 0
74
- io = Pin ( BAT_CHARGE , Pin .IN ) # Assign the pin to read
65
+ measuredVal = 0 # start our reading at 0
66
+ io = Pin (BAT_CHARGE , Pin .IN ) # Assign the pin to read
75
67
76
- for y in range (0 , 10 ): # loop through 10 times adding the read values together to ensure no false positives
68
+ for y in range (
69
+ 0 , 10
70
+ ): # loop through 10 times adding the read values together to ensure no false positives
77
71
measuredVal += io .value ()
78
72
79
- return ( measuredVal == 0 ) # return True if the value is 0
80
-
81
- # Return the internal PICO-D4 temperature in Fahrenheit
82
- def get_internal_temp_F ():
83
- """Return the internal PICO-D4 temperature in Fahrenheit."""
84
- return esp32 .raw_temperature ()
73
+ return measuredVal == 0 # return True if the value is 0
85
74
86
- # Return the internal PICO-D4 temperature in Celsius
87
- def get_internal_temp_C ():
88
- """Return the internal PICO-D4 temperature in Celsius."""
89
- return ( esp32 .raw_temperature () - 32 ) / 1.8
90
75
91
76
# Power to the on-board Dotstar is controlled by a PNP transistor, so low is ON and high is OFF
92
77
# We also need to set the Dotstar clock and data pins to be inputs to prevent power leakage when power is off
93
78
# This might be improved at a future date
94
79
# The reason we have power control for the Dotstar is that it has a quiescent current of around 1mA, so we
95
80
# need to be able to cut power to it to minimise power consumption during deep sleep or with general battery powered use
96
81
# to minimise unneeded battery drain
97
- def set_dotstar_power ( state ):
82
+ def set_dotstar_power (state ):
98
83
"""Set the power for the on-board Dostar to allow no current draw when not needed."""
99
84
# Set the power pin to the inverse of state
100
85
if state :
101
- Pin ( DOTSTAR_PWR , Pin .OUT , None ) # Break the PULL_HOLD on the pin
102
- Pin ( DOTSTAR_PWR ).value ( False ) # Set the pin to LOW to enable the Transistor
86
+ Pin (DOTSTAR_PWR , Pin .OUT , None ) # Break the PULL_HOLD on the pin
87
+ Pin (DOTSTAR_PWR ).value (False ) # Set the pin to LOW to enable the Transistor
103
88
else :
104
- Pin (13 , Pin .IN , Pin .PULL_HOLD ) # Set PULL_HOLD on the pin to allow the 3V3 pull-up to work
89
+ Pin (13 , Pin .IN , Pin .PULL_HOLD ) # Set PULL_HOLD on the pin to allow the 3V3 pull-up to work
105
90
106
- Pin (DOTSTAR_CLK , Pin .OUT if state else Pin .IN ) # If power is on, set CLK to be output, otherwise input
107
- Pin (DOTSTAR_DATA , Pin .OUT if state else Pin .IN ) # If power is on, set DATA to be output, otherwise input
91
+ Pin (
92
+ DOTSTAR_CLK , Pin .OUT if state else Pin .IN
93
+ ) # If power is on, set CLK to be output, otherwise input
94
+ Pin (
95
+ DOTSTAR_DATA , Pin .OUT if state else Pin .IN
96
+ ) # If power is on, set DATA to be output, otherwise input
108
97
109
98
# A small delay to let the IO change state
110
- time .sleep (.035 )
99
+ time .sleep (0.035 )
100
+
111
101
112
102
# Dotstar rainbow colour wheel
113
- def dotstar_color_wheel ( wheel_pos ):
103
+ def dotstar_color_wheel (wheel_pos ):
114
104
"""Color wheel to allow for cycling through the rainbow of RGB colors."""
115
105
wheel_pos = wheel_pos % 255
116
106
117
107
if wheel_pos < 85 :
118
- return ( 255 - wheel_pos * 3 , 0 , wheel_pos * 3 )
108
+ return 255 - wheel_pos * 3 , 0 , wheel_pos * 3
119
109
elif wheel_pos < 170 :
120
110
wheel_pos -= 85
121
- return ( 0 , wheel_pos * 3 , 255 - wheel_pos * 3 )
111
+ return 0 , wheel_pos * 3 , 255 - wheel_pos * 3
122
112
else :
123
113
wheel_pos -= 170
124
- return ( wheel_pos * 3 , 255 - wheel_pos * 3 , 0 )
114
+ return wheel_pos * 3 , 255 - wheel_pos * 3 , 0
115
+
125
116
126
117
# Go into deep sleep but shut down the APA first to save power
127
118
# Use this if you want lowest deep sleep current
128
- def go_deepsleep ( t ):
119
+ def go_deepsleep (t ):
129
120
"""Deep sleep helper that also powers down the on-board Dotstar."""
130
- set_dotstar_power ( False )
131
- machine .deepsleep ( t )
132
-
133
-
134
-
135
-
121
+ set_dotstar_power (False )
122
+ machine .deepsleep (t )
0 commit comments