Shane Lobo
/
nRF52_Pulse_Threads
Pulse consistency test using wait_us in a thread
Diff: main.cpp
- Revision:
- 0:18409537564c
- Child:
- 1:4dec185d33e9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Mar 30 06:37:35 2019 +0000 @@ -0,0 +1,224 @@ +/* mbed Microcontroller Library + * Copyright (c) 2018 ARM Limited + * SPDX-License-Identifier: Apache-2.0 + */ + +#include <mbed.h> +#include "SoftPWM.h" + +#define DUTY_CYCLE 0.55f + +#define MAX_INTENSITY 70 + +SoftPWM LEDR(p2); +SoftPWM LEDG(p4); +SoftPWM LEDB(p3); + +DigitalOut VEN_3V3B(p25); + +DigitalOut BOOST_EN(p14); +PwmOut BOOST_PWM(p16); + +DigitalOut HB_CH1(p6); +DigitalOut HB_CH2(p7); + +InterruptIn SW_ONOFF(p15); +InterruptIn SW_MODE(p17); +InterruptIn SW_PLUS(p13); +InterruptIn SW_MINUS(p19); + +Ticker stimTicker; + +volatile bool flip_power = false; +volatile bool low_power = false; + +int intensity = 0; + +volatile bool stim_mode = false; +volatile bool stim_active = false; +volatile bool stim_progress = true; +short stim_count = 0; + +void flip_ONOFF() { + flip_power = true; +} + +void flip_MODE() { + if (!low_power && !stim_mode) { + stim_mode = true; + } +} + +void flip_PLUS() { + if (!low_power && !stim_mode) { + if (intensity < MAX_INTENSITY) intensity++; + } +} + +void flip_MINUS() { + if (!low_power && !stim_mode) { + if (intensity > 0) intensity--; + } +} + +void stimProgress() { + stim_progress = true; +} + +void stimulate() { + // DAC = intensity + HB_CH1 = 1; + HB_CH2 = 0; + + wait_us(100); + + HB_CH1 = 0; + HB_CH2 = 1; + + wait_us(100); + + HB_CH1 = 0; + HB_CH2 = 0; + // DAC = 0 +} + +void startStimProcess() { + stim_active = true; + stim_count = 0; + BOOST_EN = 1; + BOOST_PWM.write(DUTY_CYCLE); + stim_progress = false; + stimTicker.attach(&stimProgress, 1); +} + +void stopStimProcess() { + if (!stim_mode) return; + + stimTicker.detach(); + + stim_count = 0; + stim_active = false; + stim_mode = false; + stim_progress = false; + + HB_CH1 = 0; + HB_CH2 = 0; + + BOOST_EN = 0; + BOOST_PWM.write(0); + + // DAC = 0 +} + +int main() +{ + SW_ONOFF.mode(PullUp); + SW_MODE.mode(PullUp); + SW_PLUS.mode(PullUp); + SW_MINUS.mode(PullUp); + + SW_ONOFF.fall(&flip_ONOFF); + SW_MODE.fall(&flip_MODE); + SW_PLUS.fall(&flip_PLUS); + SW_MINUS.fall(&flip_MINUS); + + HB_CH1 = 0; + HB_CH2 = 0; + BOOST_EN = 0; + VEN_3V3B = 0; + + BOOST_PWM.period_us(100); + + LEDR.period_us(500); + LEDG.period_us(500); + LEDB.period_us(500); + + while(1) + { + if (!low_power) { + SW_MODE.enable_irq(); + SW_PLUS.enable_irq(); + SW_MINUS.enable_irq(); + + VEN_3V3B = 1; + + intensity = 0; + + while (!low_power) { + if (stim_mode && !stim_active) { + startStimProcess(); + } + + if (stim_active) { + LEDR.write(0.5f); + LEDG.write(0.25f); + LEDB.write(0); + + if (stim_progress) { + stim_progress = false; + + LEDR.write(0.5f); + LEDG.write(0); + LEDB.write(0.5f); + + stimulate(); + + if (++stim_count == 4) { + stopStimProcess(); + } + + wait(0.2f); + } + } + else { + LEDR.write(0); + LEDG.write(0.5f); + LEDB.write(0); + } + + if (flip_power) { + flip_power = false; + wait(0.2f); + low_power = true; + } + + __WFE(); + __SEV(); + __WFE(); + } + } + else { + SW_ONOFF.disable_irq(); + SW_MODE.disable_irq(); + SW_PLUS.disable_irq(); + SW_MINUS.disable_irq(); + + stopStimProcess(); + + VEN_3V3B = 0; + + LEDR.write(0); + LEDG.write(0); + LEDB.write(0); + + wait(0.2f); + + SW_ONOFF.enable_irq(); + + nrf_gpio_cfg_sense_input(15, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); + NRF_POWER->SYSTEMOFF = 1; + + if (flip_power) { + flip_power = false; + wait(0.2f); + low_power = false; + } + } + + __WFE(); + __SEV(); + __WFE(); + } + + return 0; +}