Skip to content

Commit ef56749

Browse files
Updated tinypico.py helpers, example and readme to remove the internal temperature functions as Espressif have depreciated the functionality in newer silicon
Reference: espressif/arduino-esp32#2422
1 parent 1fd90d1 commit ef56749

File tree

5 files changed

+199
-140
lines changed

5 files changed

+199
-140
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# TinyPICO MicroPython Helper Library
2+
# 2019 Seon Rozenblum, Matt Trentini
3+
#
4+
# Project home:
5+
# https://github.com/TinyPICO
6+
#
7+
# 2019-Mar-12 - v0.1 - Initial implementation
8+
# 2019-May-20 - v1.0 - Initial Release
9+
# 2019-Oct-23 - v1.1 - Removed temp sensor code, prep for frozen modules
10+
11+
# Import required libraries
12+
from micropython import const
13+
from machine import Pin, SPI, ADC
14+
import machine, time, esp32
15+
16+
# TinyPICO Hardware Pin Assignments
17+
18+
# Battery
19+
BAT_VOLTAGE = const(35)
20+
BAT_CHARGE = const(34)
21+
22+
# APA102 Dotstar pins for production boards
23+
DOTSTAR_CLK = const(12)
24+
DOTSTAR_DATA = const(2)
25+
DOTSTAR_PWR = const(13)
26+
27+
# SPI
28+
SPI_MOSI = const(23)
29+
SPI_CLK = const(18)
30+
SPI_MISO = const(19)
31+
32+
# I2C
33+
I2C_SDA = const(21)
34+
I2C_SCL = const(22)
35+
36+
# DAC
37+
DAC1 = const(25)
38+
DAC2 = const(26)
39+
40+
# Helper functions
41+
42+
# Get a *rough* estimate of the current battery voltage
43+
# If the battery is not present, the charge IC will still report it's trying to charge at X voltage
44+
# so it will still show a voltage.
45+
def get_battery_voltage():
46+
"""
47+
Returns the current battery voltage. If no battery is connected, returns 3.7V
48+
This is an approximation only, but useful to detect of the charge state of the battery is getting low.
49+
"""
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
54+
return measuredvbat
55+
56+
57+
# Return the current charge state of the battery - we need to read the value multiple times
58+
# to eliminate false negatives due to the charge IC not knowing the difference between no battery
59+
# and a full battery not charging - This is why the charge LED flashes
60+
def get_battery_charging():
61+
"""
62+
Returns the current battery charging state.
63+
This can trigger false positives as the charge IC can't tell the difference between a full battery or no battery connected.
64+
"""
65+
measuredVal = 0 # start our reading at 0
66+
io = Pin(BAT_CHARGE, Pin.IN) # Assign the pin to read
67+
68+
for y in range(
69+
0, 10
70+
): # loop through 10 times adding the read values together to ensure no false positives
71+
measuredVal += io.value()
72+
73+
return measuredVal == 0 # return True if the value is 0
74+
75+
76+
# Power to the on-board Dotstar is controlled by a PNP transistor, so low is ON and high is OFF
77+
# We also need to set the Dotstar clock and data pins to be inputs to prevent power leakage when power is off
78+
# This might be improved at a future date
79+
# The reason we have power control for the Dotstar is that it has a quiescent current of around 1mA, so we
80+
# need to be able to cut power to it to minimise power consumption during deep sleep or with general battery powered use
81+
# to minimise unneeded battery drain
82+
def set_dotstar_power(state):
83+
"""Set the power for the on-board Dostar to allow no current draw when not needed."""
84+
# Set the power pin to the inverse of state
85+
if state:
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
88+
else:
89+
Pin(13, Pin.IN, Pin.PULL_HOLD) # Set PULL_HOLD on the pin to allow the 3V3 pull-up to work
90+
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
97+
98+
# A small delay to let the IO change state
99+
time.sleep(0.035)
100+
101+
102+
# Dotstar rainbow colour wheel
103+
def dotstar_color_wheel(wheel_pos):
104+
"""Color wheel to allow for cycling through the rainbow of RGB colors."""
105+
wheel_pos = wheel_pos % 255
106+
107+
if wheel_pos < 85:
108+
return 255 - wheel_pos * 3, 0, wheel_pos * 3
109+
elif wheel_pos < 170:
110+
wheel_pos -= 85
111+
return 0, wheel_pos * 3, 255 - wheel_pos * 3
112+
else:
113+
wheel_pos -= 170
114+
return wheel_pos * 3, 255 - wheel_pos * 3, 0
115+
116+
117+
# Go into deep sleep but shut down the APA first to save power
118+
# Use this if you want lowest deep sleep current
119+
def go_deepsleep(t):
120+
"""Deep sleep helper that also powers down the on-board Dotstar."""
121+
set_dotstar_power(False)
122+
machine.deepsleep(t)

play shield examples/tiny-snake/tinypico.py

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,7 @@
66
#
77
# 2019-Mar-12 - v0.1 - Initial implementation
88
# 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
1910

2011
# Import required libraries
2112
from micropython import const
@@ -38,11 +29,11 @@
3829
SPI_CLK = const(18)
3930
SPI_MISO = const(19)
4031

41-
#I2C
32+
# I2C
4233
I2C_SDA = const(21)
4334
I2C_SCL = const(22)
4435

45-
#DAC
36+
# DAC
4637
DAC1 = const(25)
4738
DAC2 = const(26)
4839

@@ -56,12 +47,13 @@ def get_battery_voltage():
5647
Returns the current battery voltage. If no battery is connected, returns 3.7V
5748
This is an approximation only, but useful to detect of the charge state of the battery is getting low.
5849
"""
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
6354
return measuredvbat
6455

56+
6557
# Return the current charge state of the battery - we need to read the value multiple times
6658
# to eliminate false negatives due to the charge IC not knowing the difference between no battery
6759
# and a full battery not charging - This is why the charge LED flashes
@@ -70,66 +62,61 @@ def get_battery_charging():
7062
Returns the current battery charging state.
7163
This can trigger false positives as the charge IC can't tell the difference between a full battery or no battery connected.
7264
"""
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
7567

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
7771
measuredVal += io.value()
7872

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
8574

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
9075

9176
# Power to the on-board Dotstar is controlled by a PNP transistor, so low is ON and high is OFF
9277
# We also need to set the Dotstar clock and data pins to be inputs to prevent power leakage when power is off
9378
# This might be improved at a future date
9479
# The reason we have power control for the Dotstar is that it has a quiescent current of around 1mA, so we
9580
# need to be able to cut power to it to minimise power consumption during deep sleep or with general battery powered use
9681
# to minimise unneeded battery drain
97-
def set_dotstar_power( state ):
82+
def set_dotstar_power(state):
9883
"""Set the power for the on-board Dostar to allow no current draw when not needed."""
9984
# Set the power pin to the inverse of state
10085
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
10388
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
10590

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
10897

10998
# A small delay to let the IO change state
110-
time.sleep(.035)
99+
time.sleep(0.035)
100+
111101

112102
# Dotstar rainbow colour wheel
113-
def dotstar_color_wheel( wheel_pos ):
103+
def dotstar_color_wheel(wheel_pos):
114104
"""Color wheel to allow for cycling through the rainbow of RGB colors."""
115105
wheel_pos = wheel_pos % 255
116106

117107
if wheel_pos < 85:
118-
return (255 - wheel_pos * 3, 0, wheel_pos * 3)
108+
return 255 - wheel_pos * 3, 0, wheel_pos * 3
119109
elif wheel_pos < 170:
120110
wheel_pos -= 85
121-
return (0, wheel_pos * 3, 255 - wheel_pos * 3)
111+
return 0, wheel_pos * 3, 255 - wheel_pos * 3
122112
else:
123113
wheel_pos -= 170
124-
return ( wheel_pos * 3, 255 - wheel_pos * 3, 0)
114+
return wheel_pos * 3, 255 - wheel_pos * 3, 0
115+
125116

126117
# Go into deep sleep but shut down the APA first to save power
127118
# Use this if you want lowest deep sleep current
128-
def go_deepsleep( t ):
119+
def go_deepsleep(t):
129120
"""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)

tinypico-helper/README.rst

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ Helper functions
4444
# and a full battery not charging - This is why the charge LED flashes
4545
def get_battery_charging()
4646
47-
# Return the internal PICO-D4 temperature in Fahrenheit
48-
def get_internal_temp_F()
49-
50-
# Return the internal PICO-D4 temperature in Celsius
51-
def get_internal_temp_C()
52-
47+
# Internal temperature functionality has been depreciated by Espressif
48+
#
49+
# #Return the internal PICO-D4 temperature in Fahrenheit
50+
# #def get_internal_temp_F()
51+
# #Return the internal PICO-D4 temperature in Celsius
52+
# #def get_internal_temp_C()
53+
#
54+
5355
# Power to the on-board Dotstar is controlled by a PNP transistor, so low is ON and high is OFF
5456
# We also need to set the Dotstar clock and data pins to be inputs to prevent power leakage when power is off
5557
# This might be improved at a future date
@@ -96,23 +98,6 @@ Example Usage
9698
print("------------------------------------")
9799
micropython.mem_info()
98100
99-
# Read the data every 15 seconds
100-
update_interval = 5
101-
# Make sure it fires immediately by starting it in the past
102-
update_temp_time = time.time() - 10
103-
104-
def print_temp():
105-
global update_interval
106-
global update_temp_time
107-
108-
# We only run the contents of this function every 5 seconds
109-
if update_temp_time < time.time():
110-
update_temp_time = time.time() + update_interval
111-
112-
# Grab the temperatures and print them
113-
print("\nInternal PICO-D4 Temp: {}°F {:.2f}°C".format( TinyPICO.get_internal_temp_F(), TinyPICO.get_internal_temp_C() ) )
114-
115-
116101
# Create a colour wheel index int
117102
color_index = 0
118103
@@ -126,7 +111,4 @@ Example Usage
126111
color_index += 1
127112
# Sleep for 20ms so the colour cycle isn't too fast
128113
time.sleep_ms(20)
129-
130-
# Print the internal PICO-D4 temperature in F and C
131-
print_temp()
132114
..

tinypico-helper/example.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,6 @@
2727
print("------------------------------------")
2828
micropython.mem_info()
2929

30-
# Read the data every 15 seconds
31-
update_interval = 5
32-
# Make sure it fires immediately by starting it in the past
33-
update_temp_time = time.time() - 10
34-
35-
def print_temp():
36-
global update_interval
37-
global update_temp_time
38-
39-
# We only run the contents of this function every 5 seconds
40-
if update_temp_time < time.time():
41-
update_temp_time = time.time() + update_interval
42-
43-
# Grab the temperatures and print them
44-
print("\nInternal PICO-D4 Temp: {}°F {:.2f}°C".format( TinyPICO.get_internal_temp_F(), TinyPICO.get_internal_temp_C() ) )
45-
46-
4730
# Create a colour wheel index int
4831
color_index = 0
4932

@@ -57,6 +40,4 @@ def print_temp():
5740
color_index += 1
5841
# Sleep for 20ms so the colour cycle isn't too fast
5942
time.sleep_ms(20)
60-
61-
# Print the internal PICO-D4 temperature in F and C
62-
print_temp()
43+

0 commit comments

Comments
 (0)