aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2019-10-26 23:45:11 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2019-10-26 23:45:11 +0200
commit641303b337022c737e35d856075038e3186f2a34 (patch)
tree08b8d5d39201c011524eb257ef6c0aa64a29d44b
parent66cdf851a8e945adf702088d9f78f850c8b8932c (diff)
downloadglutte-batteries-641303b337022c737e35d856075038e3186f2a34.tar.gz
glutte-batteries-641303b337022c737e35d856075038e3186f2a34.tar.bz2
glutte-batteries-641303b337022c737e35d856075038e3186f2a34.zip
Print out current_capacity
-rw-r--r--sw/README.rst1
-rw-r--r--sw/main.cpp58
2 files changed, 45 insertions, 14 deletions
diff --git a/sw/README.rst b/sw/README.rst
index f18e668..4446c26 100644
--- a/sw/README.rst
+++ b/sw/README.rst
@@ -22,6 +22,7 @@ secondes, une virgule, un champ de données, et termine par CR LF.
+--------------------+-----------------------------------+
| `TEXT` | Un message informatif |
| `ERROR` | Erreur ou avertissement |
+| `CAPACITY` | Une valeur en mAh |
+--------------------+-----------------------------------+
Par exemple: `TEXT,12,Startup\r\n`
diff --git a/sw/main.cpp b/sw/main.cpp
index 9c9a436..af9d707 100644
--- a/sw/main.cpp
+++ b/sw/main.cpp
@@ -51,31 +51,46 @@ struct timer_t {
uint32_t seconds = 0; /* Timer in seconds */
uint8_t ticks = 0; /* Timer in 100ms steps */
+ timer_t() {}
+ timer_t(uint32_t seconds_, uint8_t ticks_) : seconds(seconds_), ticks(ticks_) {}
+
bool operator>(const timer_t& rhs) {
return (seconds > rhs.seconds) or
(seconds == rhs.seconds and ticks > rhs.ticks);
}
- timer_t operator+(int ticks) {
- timer_t t;
- t.seconds = this->seconds;
- t.ticks = this->ticks + ticks;
- while (t.ticks >= 10) {
- t.seconds++;
- t.ticks -= 10;
+ void normalise() {
+ while (ticks >= 10) {
+ seconds++;
+ ticks -= 10;
}
+ }
+
+ timer_t operator+(const timer_t& rhs) {
+ timer_t t;
+ t.seconds = seconds + rhs.seconds;
+ t.ticks = ticks + rhs.ticks;
+ t.normalise();
return t;
}
- void operator+=(int ticks) {
- this->ticks += ticks;
- while (this->ticks >= 10) {
- seconds++;
- this->ticks -= 10;
- }
+ timer_t operator+(uint8_t ticks) {
+ timer_t t = timer_t(0, ticks);
+ return *this + t;
+ }
+
+ void operator+=(const timer_t& inc) {
+ seconds += inc.seconds;
+ ticks += inc.ticks;
+ normalise();
+ }
+
+ void operator+=(uint8_t ticks) {
+ *this += timer_t(0, ticks);
}
static constexpr int ms_to_ticks(int ms) { return ms / 100; }
+ static timer_t from_seconds(uint32_t s) { return timer_t(s, 0); }
};
/* Storage of battery capacity in mC.
@@ -88,6 +103,7 @@ uint32_t EEMEM stored_capacity3;
uint32_t last_store_time; /* In seconds */
timer_t last_ltc2400_measure;
+timer_t last_ltc2400_print_time;
uint32_t current_capacity;
@@ -171,6 +187,12 @@ static void send_message(const char *message)
uart_puts_P(ENDL);
}
+static void send_capacity(uint32_t capacity)
+{
+ snprintf(timestamp_buf, 15, "CAPACITY,%ld,%ld" ENDL, system_timer.seconds, capacity);
+ uart_puts(timestamp_buf);
+}
+
static void flag_error(const error_type_t e)
{
snprintf(timestamp_buf, 15, "ERROR,%ld,", system_timer.seconds);
@@ -265,12 +287,14 @@ int main()
/* Load capacity stored in EEPROM */
load_capacity_from_eeprom();
+ last_ltc2400_print_time = last_ltc2400_measure = system_timer;
last_store_time = system_timer.seconds;
/* Enable interrupts */
sei();
- double accum = 0.0;
+#warning "Decode if we should accumulate in a double or uint32 */
+ double accum = current_capacity;
/* Put the CPU to sleep */
set_sleep_mode(SLEEP_MODE_IDLE);
@@ -306,6 +330,12 @@ int main()
current_capacity = lrint(accum);
}
}
+
+ const auto ltc2400_print_interval = timer_t::from_seconds(10);
+ if (last_ltc2400_print_time + ltc2400_print_interval > system_timer) {
+ last_ltc2400_print_time += ltc2400_print_interval;
+ send_capacity(current_capacity);
+ }
}
return 0;