Skip to content

Commit

Permalink
Add test code to measure when debugging. Remove unnecessary spaces at…
Browse files Browse the repository at this point in the history
… line endings.
  • Loading branch information
breaker27 committed Sep 25, 2022
1 parent ecb67de commit 23a04e0
Showing 1 changed file with 44 additions and 29 deletions.
73 changes: 44 additions & 29 deletions firmware/shc_soil_moisture_meter/shc_soil_moisture_meter.c
Expand Up @@ -89,21 +89,21 @@ uint32_t power(uint32_t x, uint32_t y)
// return the value (in seconds).
uint16_t init_wakeup(void)
{
uint16_t interval = init_mode ?
uint16_t interval = init_mode ?
e2p_soilmoisturemeter_get_wakeupintervalinit() :
e2p_soilmoisturemeter_get_wakeupinterval();

if (interval == 0) // misconficuration in E2P
{
interval = WAKEUPINTERVAL_1H;
}

rfm12_set_wakeup_timer(interval);

// Calculate wake-up time in seconds according RFM12B datasheet and round the value to seconds.
uint16_t sec = (uint16_t)(((interval & 0xff) * power(2, (interval >> 8) & 0b11111) + 500) / 1000);
UART_PUTF("Wake-up interval: %us\r\n", sec);

return sec;
}

Expand Down Expand Up @@ -151,7 +151,7 @@ void prepare_battery_status(void)

// Prepare message for humidity
void prepare_humidity_status(uint16_t hum)
{
{
UART_PUTF2("Sending humidity: %u.%u%%\r\n", hum / 10, hum % 10);

// Set packet content
Expand All @@ -161,7 +161,7 @@ void prepare_humidity_status(uint16_t hum)

// Prepare message for humidity and raw value as temperature (only for debugging!)
void prepare_humidity_status_RAW_DBG(uint16_t hum, int16_t raw)
{
{
UART_PUTF2("Sending humidity: %u.%u%%\r\n", hum / 10, hum % 10);
UART_PUTF ("Sending temperature: %d\r\n", raw);

Expand All @@ -177,7 +177,7 @@ bool measure_humidity(void)
{
bool res = false;
uint16_t cnt;

switch_schmitt_trigger(true);
_delay_ms(10);

Expand All @@ -198,19 +198,19 @@ bool measure_humidity(void)
cnt = TCNT1;

TCCR1B = 0x00; // turn counter off

switch_schmitt_trigger(false);

counter_meas += cnt;
wupCnt++;

UART_PUTF4("Init mode %u, Measurement %u/%u, Counter %u\r\n",
init_mode, wupCnt, init_mode ? avgIntInit : avgInt , cnt);

if ((init_mode && (wupCnt == avgIntInit)) || (!init_mode && (wupCnt == avgInt)))
{
uint32_t avg = init_mode ? counter_meas / avgIntInit : counter_meas / avgInt;

if (init_mode)
{
UART_PUTF("Init: Save avg %u as dry threshold.\r\n", avg);
Expand All @@ -223,13 +223,13 @@ bool measure_humidity(void)
else
{
int32_t result;

if (avg < counter_min)
{
counter_min = avg;
UART_PUTF("New min: %lu, ", counter_min);
}

if (avg > dry_thr)
{
result = 0;
Expand All @@ -238,10 +238,10 @@ bool measure_humidity(void)
{
result = (dry_thr - avg) * 1000 / (dry_thr - counter_min);
}

UART_PUTF("Avg: %u, ", avg);
UART_PUTF("Result: %lu permill\r\n", result);

// Don't change reported value if it changes within a window of
// some percent.
if (reported_result == 0)
Expand Down Expand Up @@ -275,7 +275,7 @@ bool measure_humidity(void)
}
}
}

prepare_humidity_status((uint16_t)reported_result);
//prepare_humidity_status_RAW_DBG((uint16_t)reported_result, (int16_t) MIN((int32_t)avg, 30000)); // for debugging only
res = true;
Expand All @@ -284,18 +284,18 @@ bool measure_humidity(void)
wupCnt = 0;
counter_meas = 0;
}

_delay_ms(10);
return res;
}

void send_prepared_message(void)
{
inc_packetcounter();

pkg_header_set_senderid(device_id);
pkg_header_set_packetcounter(packetcounter);

rfm12_send_bufx();
rfm12_tick(); // send packet, and then WAIT SOME TIME BEFORE GOING TO SLEEP (otherwise packet would not be sent)

Expand All @@ -317,9 +317,9 @@ int main(void)
_delay_ms(1000);

util_init();

check_eeprom_compatibility(DEVICETYPE_SOILMOISTUREMETER);

// configure power pin for 74HC14D as output
sbi(TRIGGERPWR_DDR, TRIGGERPWR_PIN);

Expand Down Expand Up @@ -374,7 +374,22 @@ int main(void)
// set DIDR for all ADC channels and AINs, switch off digital input buffers to reduce ADC noise and to save power
DIDR0 = 63;
DIDR1 = 3;


// test code
/*while (1) {
adc_on(true);
_delay_ms(10);
uint16_t b = read_battery(); // 1.1V * 2 cells = 2.2V = min. voltage for RFM12B
adc_on(false);
uint16_t percentage = bat_percentage(b / 2, 1100); // 1.1V * 2 cells = 2.2V = min. voltage for RFM12B
UART_PUTF2("%u %u ", b, percentage);
measure_humidity();
_delay_ms(1000);
led_blink(50, 50, 20);
}*/

// If button pressed at start up, go to sleep for idle power consumption test.
// Don't communicate with RFM12, which may not have been connected yet.
if (BUTTON)
Expand Down Expand Up @@ -407,19 +422,19 @@ int main(void)
{
led_blink(100, 0, 1);
UART_PUTS("Button pressed!\r\n");

uint8_t cnt = 0;

while (BUTTON && (cnt < 250))
{
_delay_ms(10);
cnt++;
}

if (cnt == 250)
{
UART_PUTS("Long press -> initiate measure mode!\r\n");

while (BUTTON)
{
led_blink(100, 100, 1);
Expand All @@ -429,7 +444,7 @@ int main(void)
wupCnt = 0;
counter_meas = 0;
init_wakeup(); // to usually shorter value

UART_PUTS("Button released!\r\n");
_delay_ms(10);
}
Expand Down Expand Up @@ -469,10 +484,10 @@ int main(void)
send_prepared_message();
}
}

power_down(true);
}

// never called
// aes256_done(&aes_ctx);
}

0 comments on commit 23a04e0

Please sign in to comment.