Browse Source

Use power down mode to save power

master
Macoy Madson 7 months ago
parent
commit
a0b14d0e66
  1. 22
      .clang-format
  2. 3
      Build.sh
  3. 6
      ReadMe.org
  4. 11
      src/DrinkCounter.cake
  5. 58
      src/LowPower.c
  6. 17
      src/LowPower.h

22
.clang-format

@ -0,0 +1,22 @@
# http://releases.llvm.org/6.0.0/tools/clang/docs/ClangFormatStyleOptions.html
BasedOnStyle: Google
AccessModifierOffset: -4
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
BreakBeforeBraces: Allman
BraceWrapping:
AfterNamespace: false
BreakBeforeTernaryOperators: false
ColumnLimit: 100
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
IndentWidth: 4
Standard: Cpp11
TabWidth: 4
UseTab: ForIndentation
DerivePointerAlignment: false
PointerAlignment: Left
NamespaceIndentation: None
IndentCaseLabels: true

3
Build.sh

@ -21,6 +21,9 @@ $CAKELISP --verbose-processes --skip-build \
# Arduino build expects things to have specific names and extensions
mv cakelisp_cache/DrinkCounter/DrinkCounter.cake.c cakelisp_cache/DrinkCounter/DrinkCounter.ino || exit $?
cp src/LowPower.* cakelisp_cache/DrinkCounter/ || exit $?
mv cakelisp_cache/DrinkCounter/LowPower.c cakelisp_cache/DrinkCounter/LowPower.ino || exit $?
/media/macoy/Preserve/Programs/arduino-cli_0.29.0-rc.1_Linux_64bit/arduino-cli --no-color \
compile -b arduino:avr:uno ./cakelisp_cache/DrinkCounter/DrinkCounter.ino || exit $?

6
ReadMe.org

@ -6,3 +6,9 @@ Currently an Arduino project for helping me count my drinks.
- Download the [[https://github.com/arduino/arduino-cli/releases][Arduino CLI]]. There may be more steps because I had installed the IDE first before I found the CLI.
- Update ~Build.sh~ to point to the Arduino CLI install.
* Notes
- [[https://arduino.github.io/arduino-cli/0.29/sketch-build-process/][Arduino build process]]
- [[https://arduino.github.io/arduino-cli/0.29/platform-specification/][Arduino platform specification]]
- [[https://blog.duk.io/sleeping-atmega328-on-1ua-with-timer-wakeup/][Low power sleep with timer]]
- [[https://www.tutorialspoint.com/deep-sleep-in-arduino][Deep sleep in Arduino]]

11
src/DrinkCounter.cake

@ -9,14 +9,21 @@
(import "CHelpers.cake")
(c-import "LowPower.h")
(defstruct-local drink-settings
max-drinks-per-week char
max-drinks-in-one-day char)
(defun setup ()
(pinMode LED_BUILTIN OUTPUT))
(defun loop ()
;; turn the LED on (HIGH is the voltage level)
(digitalWrite LED_BUILTIN HIGH)
;; wait for a second
(delay 10000)
;; wait for a bit in low power mode
;; TODO: Use an external clock instead of watchdog to save power
(startLowPowerSleep SLEEP_8S)
;; turn the LED off by making the voltage LOW
(digitalWrite LED_BUILTIN LOW)
;; wait for a second

58
src/LowPower.c

@ -0,0 +1,58 @@
// Partially copied from https://github.com/rocketscream/Low-Power
#include "LowPower.h"
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
// Only Pico Power devices can change BOD settings through software
#define sleep_bod_disable() \
do \
{ \
unsigned char tempreg; \
__asm__ __volatile__( \
"in %[tempreg], %[mcucr]" \
"\n\t" \
"ori %[tempreg], %[bods_bodse]" \
"\n\t" \
"out %[mcucr], %[tempreg]" \
"\n\t" \
"andi %[tempreg], %[not_bodse]" \
"\n\t" \
"out %[mcucr], %[tempreg]" \
: [ tempreg ] "=&d"(tempreg) \
: [ mcucr ] "I" _SFR_IO_ADDR(MCUCR), [ bods_bodse ] "i"(_BV(BODS) | _BV(BODSE)), \
[ not_bodse ] "i"(~_BV(BODSE))); \
} while (0)
// Only Pico Power devices can change BOD settings through software
#define lowPowerBodOff(mode) \
do \
{ \
set_sleep_mode(mode); \
cli(); \
sleep_enable(); \
sleep_bod_disable(); \
sei(); \
sleep_cpu(); \
sleep_disable(); \
sei(); \
} while (0);
ISR(WDT_vect)
{
// WDIE & WDIF is cleared in hardware upon entering this ISR
wdt_disable();
}
// the loop function runs over and over again forever
void startLowPowerSleep(period_t period)
{
// These are disabled from here on out!
ADCSRA &= ~(1 << ADEN);
wdt_enable(period);
WDTCSR |= (1 << WDIE);
lowPowerBodOff(SLEEP_MODE_PWR_DOWN);
// end low sleep here
}

17
src/LowPower.h

@ -0,0 +1,17 @@
#ifndef LOWPOWER_H
#define LOWPOWER_H
typedef enum period_t {
SLEEP_15MS,
SLEEP_30MS,
SLEEP_60MS,
SLEEP_120MS,
SLEEP_250MS,
SLEEP_500MS,
SLEEP_1S,
SLEEP_2S,
SLEEP_4S,
SLEEP_8S,
SLEEP_FOREVER
} period_t;
void startLowPowerSleep(period_t period);
#endif
Loading…
Cancel
Save