platformio library faff

This commit is contained in:
Seth Tunstall 2022-10-26 17:39:48 +01:00
parent ea2b3443c4
commit 0ba7c21f35
27 changed files with 2560 additions and 5 deletions

View file

@ -0,0 +1 @@
{"type": "library", "name": "TinyPICO Helper Library", "version": "1.4.0", "spec": {"owner": "tinypico", "id": 6813, "name": "TinyPICO Helper Library", "requirements": null, "uri": null}}

View file

@ -0,0 +1,97 @@
TinyPICO Arduino Helper
=======================
This library adds some helper functions and useful pin assignments to make coding with TinyPICO & Arduino easier
We will be adding this library to the Arduino IDE library manager once we get closer to shipping the TinyPICOs.
TinyPICO Hardare Pin Assingments
--------------------------------
.. code-block:: c++
// APA102 Dotstar
#define DOTSTAR_PWR 13
#define DOTSTAR_DATA 2
#define DOTSTAR_CLK 12
// Battery
#define BAT_CHARGE 34
#define BAT_VOLTAGE 35
..
Helper functions
----------------
.. code-block:: c++
// Class constructor
TinyPICO();
// Get a *rough* estimate of the current battery voltage
// If the battery is not present, the charge IC will still report it's trying to charge at X voltage
// so it will still show a voltage.
float GetBatteryVoltage();
// Return the current charge state of the battery - we need to read the value multiple times
// to eliminate false negatives due to the charge IC not knowing the difference between no battery
// and a full battery not charging - This is why the charge LED flashes
bool IsChargingBattery();
// Power to the on-oard Dotstar is controlled by a PNP transistor, so low is ON and high is OFF
// We also need to set the Dotstar clock and data pins to be inputs to prevent power leakage when power is off
// The reason we have power control for the Dotstar is that it has a quiescent current of around 1mA, so we
// need to be able to cut power to it to minimise power consumption during deep sleep or with general battery powered use
// to minimse un-needed battery drain
void DotStar_SetPower( bool state );
// On-board Dotstar control
void DotStar_Clear();
void DotStar_SetBrightness( uint8_t );
void DotStar_SetPixelColor( uint32_t c );
void DotStar_SetPixelColor( uint8_t r, uint8_t g, uint8_t b );
void DotStar_Show( void );
void DotStar_CycleColor();
void DotStar_CycleColor( unsigned long wait );
void DotStar_CycleColor();
void DotStar_CycleColor( unsigned long wait );
// Convert R,G,B values to uint32_t
uint32_t Color( uint8_t r, uint8_t g, uint8_t b );
..
Example Usage
-------------
.. code-block:: c++
#include <TinyPICO.h>
// Interval between internal temperature reads
unsigned long next_temp_read = 0; // Next time step in milliseconds
uint8_t temp_read_interval = 1000; // This is in milliseconds
// Initialise the TinyPICO library
TinyPICO tp = TinyPICO();
void setup()
{
// Not used
}
void loop()
{
// Cycle the DotStar colour every 25 miliseconds
tp.DotStar_CycleColor(25);
// You can set the DotStar colour directly using r,g,b values
// tp.DotStar_SetPixelColor( 255, 128, 0 );
// You can set the DotStar colour directly using a uint32_t value
// tp.DotStar_SetPixelColor( 0xFFC900 );
// You can aclear the DotStar too
// tp.DotStar_Clear();
// To power down the DotStar for deep sleep you call this
// tp.DotStar_SetPower( false );
}
..

View file

@ -0,0 +1,27 @@
#include <TinyPICO.h>
// Initialise the TinyPICO library
TinyPICO tp = TinyPICO();
void setup()
{
// Not used
}
void loop()
{
// Cycle the DotStar colour every 25 milliseconds
tp.DotStar_CycleColor(25);
// You can set the DotStar colour directly using r,g,b values
// tp.DotStar_SetPixelColor( 255, 128, 0 );
// You can set the DotStar colour directly using a uint32_t value
// tp.DotStar_SetPixelColor( 0xFFC900 );
// You can clear the DotStar too
// tp.DotStar_Clear();
// To power down the DotStar for deep sleep you call this
// tp.DotStar_SetPower( false );
}

View file

@ -0,0 +1,17 @@
###################################
# Syntax Coloring Map For Laser7Segment
###################################
###################################
# Datatypes (KEYWORD1)
###################################
###################################
# Methods and Functions (KEYWORD2)
###################################
Laser7Segment KEYWORD2
###################################
# Constants (LITERAL1)
###################################

View file

@ -0,0 +1,9 @@
name=TinyPICO Helper Library
version=1.4.0
author=UnexpectedMaker
maintainer=UnexpectedMaker
sentence=A TinyPICO Helper Library
paragraph=A TinyPICO Helper Library
category=Uncategorized
url=https://github.com/inyPICO
architectures=*

View file

@ -0,0 +1,254 @@
// ---------------------------------------------------------------------------
// TinyPICO Helper Library - v1.4 - 18/10/2019
//
// Created by Seon Rozenblum - seon@unexpectedmaker.com
// Copyright 2019 License: MIT https://github.com/tinypico/tinypico-arduino/blob/master/LICENSE
//
// See "TinyPICO.h" for purpose, syntax, version history, links, and more.
//
// v1.4 - Support for esp32 calibrated battery voltage conversion ( @joey232 )
// - Removed temperature senser functions - This has been depreciated by Espressif
// - See https://github.com/espressif/esp-idf/issues/146
// v1.3 - Code cleanup for SWSPI bit-banging and fixed single set color not working the first time
// v1.2 - Fixed incorrect attenuation calc in the battery voltage method
// v1.1 - Fixed folder structure to be compliant with the Arduino Library Manager requirements
// v1.0 - Initial Release
// ---------------------------------------------------------------------------
#include "TinyPICO.h"
#include <SPI.h>
#include "driver/adc.h"
#include "esp_adc_cal.h"
// Battery divider resistor values
#define UPPER_DIVIDER 442
#define LOWER_DIVIDER 160
#define DEFAULT_VREF 1100 // Default referance voltage in mv
#define BATT_CHANNEL ADC1_CHANNEL_7 // Battery voltage ADC input
TinyPICO::TinyPICO()
{
pinMode( DOTSTAR_PWR, OUTPUT );
pinMode( BAT_CHARGE, INPUT );
pinMode( BAT_VOLTAGE, INPUT );
DotStar_SetPower( false );
nextVoltage = millis();
for (int i = 0; i < 3; i++ )
pixel[i] = 0;
isInit = false;
brightness = 128;
colorRotation = 0;
nextRotation = 0;
}
TinyPICO::~TinyPICO()
{
isInit = false;
DotStar_SetPower( false );
}
void TinyPICO::DotStar_SetBrightness(uint8_t b)
{
// Stored brightness value is different than what's passed. This
// optimizes the actual scaling math later, allowing a fast 8x8-bit
// multiply and taking the MSB. 'brightness' is a uint8_t, adding 1
// here may (intentionally) roll over...so 0 = max brightness (color
// values are interpreted literally; no scaling), 1 = min brightness
// (off), 255 = just below max brightness.
brightness = b + 1;
}
// Convert separate R,G,B to packed value
uint32_t TinyPICO::Color(uint8_t r, uint8_t g, uint8_t b)
{
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
void TinyPICO::DotStar_Show(void)
{
if ( !isInit )
{
isInit = true;
swspi_init();
delay(10);
}
uint16_t b16 = (uint16_t)brightness; // Type-convert for fixed-point math
// Start-frame marker
for( int i=0; i<4; i++) swspi_out(0x00);
// Pixel start
swspi_out(0xFF);
for( int i=0; i<3; i++)
{
if( brightness > 0)
swspi_out((pixel[i] * b16) >> 8); // Scale, write - Scaling pixel brightness on output
else
swspi_out(pixel[i]); // R,G,B @Full brightness (no scaling)
}
// // End frame marker
swspi_out(0xFF);
}
void TinyPICO::swspi_out(uint8_t n)
{
for(uint8_t i=8; i--; n <<= 1)
{
if (n & 0x80)
digitalWrite(DOTSTAR_DATA, HIGH);
else
digitalWrite(DOTSTAR_DATA, LOW);
digitalWrite(DOTSTAR_CLK, HIGH);
digitalWrite(DOTSTAR_CLK, LOW);
}
delay(1);
}
void TinyPICO::DotStar_Clear() { // Write 0s (off) to full pixel buffer
for (int i = 0; i < 3; i++ )
pixel[i] = 0;
DotStar_Show();
}
// Set pixel color, separate R,G,B values (0-255 ea.)
void TinyPICO::DotStar_SetPixelColor(uint8_t r, uint8_t g, uint8_t b)
{
pixel[0] = b;
pixel[1] = g;
pixel[2] = r;
DotStar_Show();
}
// Set pixel color, 'packed' RGB value (0x000000 - 0xFFFFFF)
void TinyPICO::DotStar_SetPixelColor(uint32_t c)
{
pixel[0] = (uint8_t)c;
pixel[1] = (uint8_t)(c >> 8);
pixel[2] = (uint8_t)(c >> 16);
DotStar_Show();
}
void TinyPICO::swspi_init(void)
{
DotStar_SetPower( true );
digitalWrite(DOTSTAR_DATA , LOW);
digitalWrite(DOTSTAR_CLK, LOW);
}
void TinyPICO::swspi_end()
{
DotStar_SetPower( false );
}
// Switch the DotStar power
void TinyPICO::DotStar_SetPower( bool state )
{
digitalWrite( DOTSTAR_PWR, !state );
pinMode( DOTSTAR_DATA, state ? OUTPUT : INPUT_PULLDOWN );
pinMode( DOTSTAR_CLK, state ? OUTPUT : INPUT_PULLDOWN );
}
void TinyPICO::DotStar_CycleColor()
{
DotStar_CycleColor(0);
}
void TinyPICO::DotStar_CycleColor( unsigned long wait = 0 )
{
if ( millis() > nextRotation + wait )
{
nextRotation = millis();
colorRotation++;
byte WheelPos = 255 - colorRotation;
if(WheelPos < 85)
{
DotStar_SetPixelColor(255 - WheelPos * 3, 0, WheelPos * 3);
}
else if(WheelPos < 170)
{
WheelPos -= 85;
DotStar_SetPixelColor(0, WheelPos * 3, 255 - WheelPos * 3);
}
else
{
WheelPos -= 170;
DotStar_SetPixelColor(WheelPos * 3, 255 - WheelPos * 3, 0);
}
DotStar_Show();
}
}
// Return the current charge state of the battery
bool TinyPICO::IsChargingBattery()
{
int measuredVal = 0;
for ( int i = 0; i < 10; i++ )
{
int v = digitalRead( BAT_CHARGE );
measuredVal += v;
}
return ( measuredVal == 0);
}
// Return a *rough* estimate of the current battery voltage
float TinyPICO::GetBatteryVoltage()
{
uint32_t raw, mv;
esp_adc_cal_characteristics_t chars;
// only check voltage every 1 second
if ( nextVoltage - millis() > 0 )
{
nextVoltage = millis() + 1000;
// grab latest voltage
analogRead(BAT_VOLTAGE); // Just to get the ADC setup
raw = adc1_get_raw(BATT_CHANNEL); // Read of raw ADC value
// Get ADC calibration values
esp_adc_cal_characterize(ADC_UNIT_1,ADC_ATTEN_11db ,ADC_WIDTH_BIT_12,DEFAULT_VREF,&chars);
// Convert to calibrated mv then volts
mv = esp_adc_cal_raw_to_voltage(raw, &chars) * (LOWER_DIVIDER+UPPER_DIVIDER) / LOWER_DIVIDER;
lastMeasuredVoltage = (float)mv / 1000.0;
}
return ( lastMeasuredVoltage );
}
// Tone - Sound wrapper
void TinyPICO::Tone( uint8_t pin, uint32_t freq )
{
if ( !isToneInit )
{
pinMode( pin, OUTPUT);
ledcSetup(0, freq, 8); // Channel 0, resolution 8
ledcAttachPin( pin , 0 );
isToneInit = true;
}
ledcWriteTone( 0, freq );
}
void TinyPICO::NoTone( uint8_t pin )
{
if ( isToneInit )
{
ledcWriteTone(0, 0);
pinMode( pin, INPUT_PULLDOWN);
isToneInit = false;
}
}

View file

@ -0,0 +1,97 @@
// ---------------------------------------------------------------------------
// TinyPICO Helper Library - v1.4 - 18/10/2019
//
// AUTHOR/LICENSE:
// Created by Seon Rozenblum - seon@unexpectedmaker.com
// Copyright 2016 License: GNU GPL v3 http://www.gnu.org/licenses/gpl-3.0.html
//
// LINKS:
// Project home: http://tinypico.com
// Blog: http://tinypico.com
//
// DISCLAIMER:
// This software is furnished "as is", without technical support, and with no
// warranty, express or implied, as to its usefulness for any purpose.
//
// PURPOSE:
// Helper Library for the TinyPICO http://tinypico.com
//
// HISTORY:
//
// v1.4 - Support for esp32 calibrated battery voltage conversion ( @joey232 )
// - Removed temperature senser functions - This has been depreciated by Espressif
// - See https://github.com/espressif/esp-idf/issues/146
// v1.3 - Code cleanup for SWSPI bit-banging and fixed single set color not working the first time
// v1.2 - Fixed incorrect attenuation calc in the battery voltage method
// v1.1 - Fixed folder structure to be compliant with the Arduino Library Manager requirements
// v1.0 - Initial Release
//
// ---------------------------------------------------------------------------
#ifndef TinyPICO_h
#define TinyPICO_h
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#include <pins_arduino.h>
#endif
#include <SPI.h>
#define DOTSTAR_PWR 13
#define DOTSTAR_DATA 2
#define DOTSTAR_CLK 12
#define BAT_CHARGE 34
#define BAT_VOLTAGE 35
class TinyPICO
{
public:
TinyPICO();
~TinyPICO();
// TinyPICO Features
void DotStar_SetPower( bool state );
float GetBatteryVoltage();
bool IsChargingBattery();
// Dotstar
void DotStar_Clear(); // Set all pixel data to zero
void DotStar_SetBrightness( uint8_t ); // Set global brightness 0-255
void DotStar_SetPixelColor( uint32_t c );
void DotStar_SetPixelColor( uint8_t r, uint8_t g, uint8_t b );
void DotStar_Show( void ); // Issue color data to strip
void DotStar_CycleColor();
void DotStar_CycleColor( unsigned long wait );
uint32_t Color( uint8_t r, uint8_t g, uint8_t b ); // R,G,B to 32-bit color
// Tone for making sound on any ESP32 - just using channel 0
void Tone( uint8_t, uint32_t );
void NoTone( uint8_t );
protected:
void swspi_init(void); // Start bitbang SPI
void swspi_out(uint8_t n); // Bitbang SPI write
void swspi_end(void); // Stop bitbang SPI
private:
unsigned long nextVoltage;
float lastMeasuredVoltage;
byte colorRotation;
unsigned long nextRotation;
uint8_t brightness; // Global brightness setting
uint8_t pixel[ 3 ]; // LED RGB values (3 bytes ea.)
bool isInit;
bool isToneInit;
};
#endif