# eLight flashlight
## Function-
Support spotlight and floodlight to be turned on and dimmed (10%~100%) separately, without flicker;
- Support any light to flash and SOS mode, with adjustable flashing frequency;
- Support automatic reminder and shutdown of low battery, support manual check of battery level;
- Support power bank function, Type-C interface, 5V/2A input and output;
- Support lock button;
- Support forced reset;
- Support ISP upgrade.
## Hardware-
The shell is based on the modification of the Smiling Shark 867A flashlight, and the charging port is changed to Type-C USB;
- Use 18650 lithium battery above 3000mAh to meet the charge and discharge rate;
- Spotlight single 1w LED (enhanced heat dissipation), floodlight 3 0.5w 5730.
![PCB front]

![PCB back]

## MCU
- STC8G1K08A, SOP8, 10-bit ADC, PWM output, wide voltage range, wake-up from power-down mode;
- MCU main frequency 4M, output 8-bit PWM, PWM frequency about 15.6k, 32 levels of brightness after gamma correction.
## Key operation
- Off state: short press to turn on the spotlight, double-click to turn on the floodlight, long press to check the remaining power;
- Spotlight and floodlight state: short press to turn off, double-click to adjust to maximum brightness, 3 clicks to turn on the corresponding light to flash, long press to adjust brightness continuously, each long press switches the direction of bright and dark adjustment;
- Flashing state: short press to switch SOS, short press again to turn off, flashing and SOS are fixed to the maximum brightness, long press to switch frequency or interval;
- Lock and unlock: 4 consecutive clicks, the key floodlight flashes quickly once when locked;
- Super long press in any state to force reset.
## Button definition
- Short press: 50-500ms, Long press: >500ms (continuous press every 50ms afterwards), Super long press: >8s, Continuous press (2-4 consecutive presses): the interval between two adjacent short presses is <250ms;
- Each short press is processed immediately to improve the response speed. The problem of repeated processing of continuous presses relies on the state machine design to ensure the correct final state:
| State| Single click| Double click| 3 clicks| 4 clicks| Long press| Timeout|
| :---: | :---: | :---: | :---: | :---: | :---: | :---: | |
off | on,Spot| on,Highlight| | | bat | |
| on | off | on,Flood| flash | | Dimming| |
| flash | sos | | | lock | FM| |
| sos | off | on,Highlight| | | FM| |
| lock | | | | off | | |
bat | | | | | | off |
## State memory, reset and firmware upgrade
- The adjusted value is automatically memorized to RAM. Long press to force reset the MCU, and the state will be automatically cleared;
- After reset, it will first go through the ISP mode. You can open the STC ISP software first and wait for the upgrade.
## Power reminder and protection
- When you long press to view, the remaining battery power is estimated according to the voltage. The floodlight is lit at low brightness for 0.01~1.5s and then turned off;
- If the voltage is <3.3v, it is low power. In normal mode, it flashes twice every 10s to remind. There is no prompt in flash and SOS mode;
- If the voltage is <2.9v, it will automatically shut down. If it is turned on again, it will only flash once quickly;
- There is a hysteresis of 0.1v for voltage judgment, that is, the voltage needs to reach 3.4v or 3v when it rises.
## Low power consumption
- The MCU enters the power-down mode after a delay of 2s in the off state, and wakes up with a key interrupt;
- The PWM clock cannot be stopped in the on state, and it enters the idle mode and measures the voltage every 0.5s.
## LED driver
- PAM2804/SY8006/LN2401 and other switch step-down constant current ICs, efficiency>90%, hardware design can realize DC dimming and PWM dimming; (actual software realizes DC dimming)
- DC dimming is controlled by PWM filtering to DC and then entering FB. The error is large at low brightness, but the current is small and the light efficiency is higher;
+ PWM is filtered by 1st level RC (10k-1uf), and then passed through 200k-10k and superimposed with current sampling into FB to achieve 10%~100% constant current;
+ The flash mode can achieve higher brightness with >100% current. Since it is intermittently turned on, the heat dissipation can be satisfied; (not implemented)
+ The PWM duty cycle needs to be adjusted in real time according to the power supply voltage to achieve constant DC voltage after filtering;
- PWM dimming can achieve lower brightness. When constant current, the duty cycle does not need to be adjusted according to the voltage. Switch EN or MOS can be selected; (not implemented)
+ The slow start time of switch EN is too long, resulting in a low frequency (<1kHz);
+ When the switch MOS is turned on, there is a pulse current, and a us-level slow start needs to be added, such as using open drain + pull-up drive.
[Code warehouse (github)](https://github.com/eleqian/eLight)