131 lines
5.0 KiB
Markdown
Raw Normal View History

2021-01-29 12:31:58 +01:00
[![Arduino CI](https://github.com/RobTillaart/GAMMA/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
2021-11-02 15:15:11 +01:00
[![Arduino-lint](https://github.com/RobTillaart/GAMMA/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/GAMMA/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/GAMMA/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/GAMMA/actions/workflows/jsoncheck.yml)
2023-10-29 12:03:26 +01:00
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/GAMMA.svg)](https://github.com/RobTillaart/GAMMA/issues)
2021-01-29 12:31:58 +01:00
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/GAMMA/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/GAMMA.svg?maxAge=3600)](https://github.com/RobTillaart/GAMMA/releases)
2023-10-29 12:03:26 +01:00
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/GAMMA.svg)](https://registry.platformio.org/libraries/robtillaart/GAMMA)
2021-01-29 12:31:58 +01:00
2021-11-02 15:15:11 +01:00
2021-01-29 12:31:58 +01:00
# GAMMA
Arduino Library for the GAMMA function to adjust brightness of LED's etc.
2021-11-02 15:15:11 +01:00
2021-01-29 12:31:58 +01:00
## Description
2023-10-29 12:03:26 +01:00
This library provides a GAMMA lookup class. It is typical used to
2021-01-29 12:31:58 +01:00
change the intensity / brightness of a LED to match the human eye.
When a LED is given 50% PWM it looks far brighter for the eye.
This lib provides a balance between an expensive math function and the speed
2023-01-26 19:56:10 +01:00
of a lookup table.
2021-01-29 12:31:58 +01:00
The accuracy of the library depends on the size of the internal array.
2023-01-26 19:56:10 +01:00
A size of 256 is the reference. Smaller arrays use interpolation and
these interpolated values are good (< 1%) down to internal array size 16.
2021-01-29 12:31:58 +01:00
The size can be as small as 2 which is pretty inaccurate.
In this latter case the curve is approximated by only two linear interpolations.
In short, choose the size that fits your application.
2023-01-26 19:56:10 +01:00
The library has a **setGamma(float gamma)** function that allows an application
to change the gamma value runtime.
2022-07-26 13:16:03 +02:00
This allows adjustments that are not possible with a fixed table.
2021-01-29 12:31:58 +01:00
2023-01-26 19:56:10 +01:00
The class provides **dump()** to create a table e.g. to place in PROGMEM.
2022-07-25 15:25:14 +02:00
Since 0.2.2 the library also has **dumpArray()** to generate a C-style array.
2021-01-29 12:31:58 +01:00
2021-11-02 15:15:11 +01:00
Note: tested on UNO and ESP32 only.
2023-10-29 12:03:26 +01:00
#### Related
- https://en.wikipedia.org/wiki/Gamma_correction
- https://github.com/RobTillaart/map2colour
2021-01-29 12:31:58 +01:00
## Interface
2023-01-26 19:56:10 +01:00
```cpp
#include "gamma.h"
```
2021-01-29 12:31:58 +01:00
### Core functions
2021-11-02 15:15:11 +01:00
- **GAMMA(uint16_t size = 32)** constructor, gets the size of the internal
array as parameter. The default for size = 32 as this is a good balance between performance
2023-01-26 19:56:10 +01:00
and size of the internal array.
2022-07-25 15:25:14 +02:00
The size parameter must be in {2, 4, 8, 16, 32, 64, 128, 256 }.
- **~GAMMA()** destructor.
2022-07-26 13:16:03 +02:00
- **bool begin()** The internal array is allocated and initialized with a gamma == 2.8.
2022-07-25 15:25:14 +02:00
This is an often used value to adjust light to human eye responses.
Note that **begin()** must be called before any other function.
2022-07-26 13:16:03 +02:00
Returns false if allocation fails.
2022-07-25 15:25:14 +02:00
- **void setGamma(float gamma)** calculates and fills the array with new values.
2023-01-26 19:56:10 +01:00
This can be done runtime so runtime adjustment of gamma mapping is possible.
2022-07-25 15:25:14 +02:00
This calculation are relative expensive and takes quite some time (depending on size).
If the array already is calculated for gamma, the calculation will be skipped.
2021-11-02 15:15:11 +01:00
The parameter **gamma** must be > 0. The value 1 gives an 1:1 mapping.
2023-10-29 12:03:26 +01:00
Returns false if gamma <= 0 or if no array is allocated.
2022-07-25 15:25:14 +02:00
- **float getGamma()** returns the set gamma value.
2022-07-26 13:16:03 +02:00
- **uint8_t operator \[uint8_t index\]** allows the GAMMA object to be accessed as an array.
2023-01-26 19:56:10 +01:00
like ```x = G[40];``` Makes it easy to switch with a real array.
2022-07-26 13:16:03 +02:00
The value returned is in the range 0 .. 255, so the user may need to scale it e.g. to 0.0 - 1.0.
2023-10-29 12:03:26 +01:00
Note: if internal array not allocated the function returns 0.
2022-07-26 13:16:03 +02:00
As this is a legitimate value the user should take care.
2021-01-29 12:31:58 +01:00
2021-11-02 15:15:11 +01:00
2021-01-29 12:31:58 +01:00
### Development functions
2023-01-26 19:56:10 +01:00
- **uint16_t size()** returns the size of the internal array.
2022-07-25 15:25:14 +02:00
This is always a power of 2.
2023-10-29 12:03:26 +01:00
- **uint16_t distinct()** returns the number of distinct values in the internal array.
Especially with larger internal arrays there will be more duplicate numbers.
- **bool dump(Stream \*str = &Serial)** dumps the internal array to a stream, default Serial.
2022-07-25 15:25:14 +02:00
Useful to create an array in RAM, PROGMEM, EEPROM, in a file or wherever.
2023-10-29 12:03:26 +01:00
Returns false if no array is allocated.
- **void dumpArray(Stream \*str = &Serial)** dumps the internal array to a stream, default Serial, as a C-style array. See example.
Returns false if no array is allocated.
2021-11-02 15:15:11 +01:00
## Operation
2022-07-25 15:25:14 +02:00
See example.
2021-11-02 15:15:11 +01:00
2021-01-29 12:31:58 +01:00
## Future ideas
2023-01-26 19:56:10 +01:00
#### Must
2021-11-02 15:15:11 +01:00
- improve documentation
2022-11-08 17:03:54 +01:00
2023-01-26 19:56:10 +01:00
#### Should
2021-12-18 17:16:37 +01:00
- test other platforms
2022-11-08 17:03:54 +01:00
2023-01-26 19:56:10 +01:00
#### Could
2021-01-29 12:31:58 +01:00
- uint16 version?
2023-01-26 19:56:10 +01:00
- GAMMA16,
- GAMMA32,
2022-07-26 13:16:03 +02:00
- GAMMA_RGB ?
2022-11-08 17:03:54 +01:00
2023-01-26 19:56:10 +01:00
#### Wont
- look for optimizations (done in 0.4.0)
- getter \[\]
- setGamma -> pow() is expensive
- setGamma(gamma) gamma = 1.0 is linear, less math (too specific?)
2021-12-18 17:16:37 +01:00
2023-10-29 12:03:26 +01:00
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,