- Definition of constants (e.g., pin numbers)
- Hardware initialization
- Handling sensors and similar peripherals
This is the multi-page printable view of this section. Click here to print...
Board Behavior (BRD)
- 1: <BRD_APPTWELITE>
- 2: PAL Common Definitions
- 2.1: <PAL_NOTICE>
- 2.2: <PAL_AMB>
- 2.3: <PAL_MAG>
- 2.4: <PAL_MOT>
- 3: <ARIA>
- 4: <CUE>
- 5: <MONOSTICK>
1 - <BRD_APPTWELITE>
This is a board behavior assuming the same wiring as the “Extremely Simple! Standard App (App_Twelite).” It defines constants and provides functions for reading M1–M3 and BPS pins.
Constants
The following constants are defined:
static const uint8_t PIN_DI1 = mwx::PIN_DIGITAL::DIO12;
static const uint8_t PIN_DI2 = mwx::PIN_DIGITAL::DIO13;
static const uint8_t PIN_DI3 = mwx::PIN_DIGITAL::DIO11;
static const uint8_t PIN_DI4 = mwx::PIN_DIGITAL::DIO16;
static const uint8_t PIN_DO1 = mwx::PIN_DIGITAL::DIO18;
static const uint8_t PIN_DO2 = mwx::PIN_DIGITAL::DIO19;
static const uint8_t PIN_DO3 = mwx::PIN_DIGITAL::DIO4;
static const uint8_t PIN_DO4 = mwx::PIN_DIGITAL::DIO9;
static const uint8_t PIN_M1 = mwx::PIN_DIGITAL::DIO10;
static const uint8_t PIN_M2 = mwx::PIN_DIGITAL::DIO2;
static const uint8_t PIN_M3 = mwx::PIN_DIGITAL::DIO3;
static const uint8_t PIN_BPS = mwx::PIN_DIGITAL::DIO17;
static const uint8_t PIN_AI1 = mwx::PIN_ANALOGUE::A1;
static const uint8_t PIN_AI2 = mwx::PIN_ANALOGUE::A3;
static const uint8_t PIN_AI3 = mwx::PIN_ANALOGUE::A2;
static const uint8_t PIN_AI4 = mwx::PIN_ANALOGUE::A4;
You can access them like BRD_APPTWELITE::PIN_DI1
.
Methods
Methods are provided to read the values of DIP SW (M1, M2, M3, BPS) pins.
inline uint8_t get_M1()
inline uint8_t get_M2()
inline uint8_t get_M3()
inline uint8_t get_BPS()
inline uint8_t get_DIPSW_BM()
The return value is not HIGH or LOW: 0
means the switch is not set (HIGH side), 1
means it is set (LOW side).
get_DIPSW_BM()
returns the values of M1, M2, M3, and BPS pins in order from bit 0.
2 - PAL Common Definitions
Constants
The following constants are defined:
static const uint8_t PIN_BTN = 12; // button (as SET)
static const uint8_t PIN_LED = 5; // LED
static const uint8_t PIN_WDT = 13; // WDT (shall tick every 60sec)
static const uint8_t PIN_D1 = 1; // DIP SW1
static const uint8_t PIN_D2 = 2; // DIP SW2
static const uint8_t PIN_D3 = 3; // DIP SW3
static const uint8_t PIN_D4 = 4; // DIP SW4
static const uint8_t PIN_SNS_EN = 16;
static const uint8_t PIN_SNS_INT = 17;
These can be accessed like PAL_AMB::PIN_BTN
.
Hardware Initialization
pinMode(PIN_BTN, INPUT_PULLUP);
pinMode(PIN_LED, OUTPUT_INIT_HIGH);
pinMode(PIN_WDT, OUTPUT_INIT_HIGH);
pinMode(PIN_D1, INPUT_PULLUP);
pinMode(PIN_D2, INPUT_PULLUP);
pinMode(PIN_D3, INPUT_PULLUP);
pinMode(PIN_D4, INPUT_PULLUP);
Pins are initialized as shown in the code above.
Watchdog Timer
The external watchdog timer is reset at startup, after waking from sleep, and after a certain amount of time has passed since startup.
Methods
set_led()
void set_led(uint8_t mode, uint16_t tick)
Controls LED (D1).
mode
takes the following parameters. tick
specifies the ON duration [ms], but see the explanation of mode
for details.
Mode | Description |
---|---|
LED_TIMER::BLINK | Blinks the LED. Toggles ON/OFF every tick ms. After waking from sleep, it resets and starts ON. |
LED_TIMER::ON_RX | Turns on the LED for tick ms when a packet is received. |
LED_TIMER::ON_TX_COMP | Turns on the LED for tick ms when transmission completes. |
led_one_shot()
void led_one_shot(uint16_t tick)
Turns on the LED for a specified duration. Cannot be used simultaneously with set_led()
.
get_D1() .. D4(), get_DIPSW_BM()
inline uint8_t get_D1()
inline uint8_t get_D2()
inline uint8_t get_D3()
inline uint8_t get_D4()
inline uint8_t get_DIPSW_BM()
get_D1()
through get_D4()
return 0
when the DIP switch is in the HIGH (up) position, and 1
when in the LOW (down) position.
get_DIPSW_BM()
returns the DIP switch setting as a value from 0
to 15
, calculated as: SW1==LOW
= 1, SW2==LOW
= 2, SW3==LOW
= 4, and SW4==LOW
= 8.
The logic here is inverted from the actual HIGH(1)/LOW(0) levels of D1–D4.
This is because a DIP switch in the LOW(0) position is considered “set” and assigned a value of 1
.
2.1 - <PAL_NOTICE>
This board behavior is for the NOTICE PAL.
In addition to the common definitions, this behavior enables access to onboard sensors.
- LED driver PCA9632
- Accelerometer MC3630
void setup() {
auto&& brd = the_twelite.board.use<PAL_NOTICE>();
}
Member Objects
sns_PCA9632
Object for the PCA9632 device. The board definition handles Wire initialization and device setup. Use the control methods described later to interact with the device.
If using a coin battery, pulsed lighting (e.g., flashing briefly every second) can reduce peak current draw.
For example, turning on the LED for about 20ms every 1 second can still appear bright enough while minimizing the current (e.g., 5mA * 20ms per second = 0.1mA average).
sns_MC3630
Object for the MC3630 sensor. Handles SPI setup, device initialization, and interrupt processing. Use the methods provided in sns_MC3630
.
PCA9632 Definitions
static const uint8_t LED_OFF = SnsPCA9632::LED_OFF;
static const uint8_t LED_ON = SnsPCA9632::LED_PWM;
static const uint8_t LED_BLINK = SnsPCA9632::LED_BLINK;
static const uint8_t LED_NOP = SnsPCA9632::LED_NOP;
static const uint8_t LED_R = SnsPCA9632::LED1;
static const uint8_t LED_G = SnsPCA9632::LED2;
static const uint8_t LED_B = SnsPCA9632::LED3;
static const uint8_t LED_W = SnsPCA9632::LED4;
static const uint8_t LED_REG_MAX_PWM = 127;
static const uint8_t LED_REG_BOOST_PWM = 255;
LED States
Definition | Description |
---|---|
PAL_NOTICE::LED_OFF | Off |
PAL_NOTICE::LED_ON | On (PWM brightness control) |
PAL_NOTICE::LED_BLINK | Blinking |
PAL_NOTICE::LED_NOP | No change |
LED Identifiers
Definition | Description |
---|---|
PAL_NOTICE::LED_R | Red LED |
PAL_NOTICE::LED_G | Green LED |
PAL_NOTICE::LED_B | Blue LED |
PAL_NOTICE::LED_W | White LED |
Register Brightness Settings
Definition | Description |
---|---|
PAL_NOTICE::LED_REG_MAX_PWM | Standard brightness PWM register value (approx. 50%) |
PAL_NOTICE::LED_REG_BOOST_PWM | Boost brightness PWM register value |
PCA9632 Control Methods
Master Switch
void set_led_master_sw_on() { digitalWrite(PIN_SNS_EN, LOW); }
void set_led_master_sw_off() { digitalWrite(PIN_SNS_EN, HIGH); }
NOTICE PAL includes a FET switch after the PCA9632 output. LEDs will not light unless this switch is turned ON.
LED State Control
void set_led_r_blink()
void set_led_r_on()
void set_led_r_off()
void set_led_g_on()
void set_led_g_blink()
void set_led_g_off()
void set_led_b_on()
void set_led_b_blink()
void set_led_b_off()
void set_led_w_on()
void set_led_w_blink()
void set_led_w_off()
These functions set individual LEDs to ON, OFF, or BLINK state.
void set_leds(uint8_t r, uint8_t g, uint8_t b, uint8_t w)
void set_leds_off()
set_leds()
controls the state of all LEDs. Each parameter must be one of: PAL_NOTICE::LED_OFF
, PAL_NOTICE::LED_ON
, PAL_NOTICE::LED_BLINK
, or PAL_NOTICE::LED_NOP
.
LED Brightness Control
void set_led_brightness_r_reg(uint8_t duty)
void set_led_brightness_g_reg(uint8_t duty)
void set_led_brightness_b_reg(uint8_t duty)
void set_led_brightness_w_reg(uint8_t duty)
void set_leds_brightness_reg(uint8_t r, uint8_t g, uint8_t b, uint8_t w)
void set_led_brightness_r1000(uint16_t duty, bool boost = false)
void set_led_brightness_g1000(uint16_t duty, bool boost = false)
void set_led_brightness_b1000(uint16_t duty, bool boost = false)
void set_led_brightness_w1000(uint16_t duty, bool boost = false)
void set_leds_brightness1000(
uint16_t r, uint16_t g, uint16_t b, uint16_t w, bool boost = false)
Controls PWM duty cycle (brightness) of LEDs.
set_led_brightness_?_reg()
and set_leds_brightness_reg()
directly specify 0–255 register values, where brightness is duty/256
.
set_led_brightness_?1000()
and set_leds_brightness1000()
accept values from 0 to 1000. 0 means off, higher values increase brightness. When boost=false
, a value of 1000 maps to register value 127; when boost=true
, it maps to 255.
Blink Control
void set_blink_cycle_ms(uint16_t x)
void set_blink_duty1000(uint16_t x)
LEDs set to PAL_NOTICE::LED_BLINK
blink based on the specified cycle and duty.
- Per-LED blinking patterns are not supported.
- Brightness during blink is set by the current PWM duty configuration.
set_blink_cycle_ms()
sets blink cycle in milliseconds.
set_blink_duty1000()
sets ON duration as cycle * x / 1000
.
LED Test
void test_led()
Briefly lights up all four LEDs. After this, the master switch is left ON (set_led_master_sw_on()
).
2.2 - <PAL_AMB>
In addition to the common definitions, this behavior enables access to onboard sensors.
- Temperature and Humidity Sensor SHTC3
- Ambient Light Sensor LTR308ALS
void setup() {
auto&& brd = the_twelite.board.use<PAL_AMB>();
}
Member Objects
sns_SHTC3
Object for the SHTC3 sensor.
sns_LTR308ALS
Object for the LTR-308ALS sensor.
2.3 - <PAL_MAG>
This board behavior is for the Open-Close Sensor PAL.
void setup() {
auto&& brd = the_twelite.board.use<PAL_MAG>();
}
The sensor on the Open-Close Sensor PAL is a magnetic sensor with two interrupt input lines only.
const uint8_t PAL_MAG::PIN_SNS_NORTH = 16;
const uint8_t PAL_MAG::PIN_SNS_OUT1 = 16;
const uint8_t PAL_MAG::PIN_SNS_SOUTH = 17;
const uint8_t PAL_MAG::PIN_SNS_OUT2 = 17;
PAL_MAG::PIN_SNS_NORTH
triggers an interrupt when the sensor detects the north pole; PAL_MAG::PIN_SNS_SOUTH
also triggers an interrupt upon detecting the north pole.
Configure the following before entering sleep:
pinMode(PAL_MAG::PIN_SNS_OUT1, PIN_MODE::WAKE_FALLING);
pinMode(PAL_MAG::PIN_SNS_OUT2, PIN_MODE::WAKE_FALLING);
On wake-up, check the GPIO that triggered the wake-up:
uint8_t b_north =
the_twelite.is_wokeup_by_dio(PAL_MAG::PIN_SNS_NORTH);
uint8_t b_south =
the_twelite.is_wokeup_by_dio(PAL_MAG::PIN_SNS_SOUTH);
2.4 - <PAL_MOT>
This board behavior is for the Motion Sensor PAL.
In addition to the common definitions, this behavior enables access to onboard sensors.
- Accelerometer MC3630
void setup() {
auto&& brd = the_twelite.board.use<PAL_MOT>();
}
Member Objects
sns_MC3630
Object for the MC3630 sensor.
3 - <ARIA>
Enables access to the onboard accelerometer, magnetic sensor, and LED.
- Temperature/Humidity Sensor
- Magnetic Sensor
- LED
void setup() {
auto&& brd = the_twelite.board.use<ARIA>();
}
Temperature/Humidity Sensor
A member object for the SHT4x sensor (sns_SHT4x
) is defined.
Magnetic Sensor
The sensor used in the Door Sensor PAL is a magnetic sensor that only has two interrupt input lines.
const uint8_t CUE::PIN_SNS_NORTH = 16;
const uint8_t CUE::PIN_SNS_OUT1 = 16;
const uint8_t CUE::PIN_SNS_SOUTH = 8;
const uint8_t CUE::PIN_SNS_OUT2 = 8;
ARIA::PIN_SNS_NORTH
triggers an interrupt when the sensor detects the north pole.
ARIA::PIN_SNS_SOUTH
also triggers an interrupt when the sensor detects the north pole.
Before entering sleep, configure the pins as follows:
pinMode(CUE::PIN_SNS_OUT1, PIN_MODE::WAKE_FALLING);
pinMode(CUE::PIN_SNS_OUT2, PIN_MODE::WAKE_FALLING);
On wake-up, check the cause using:
uint8_t b_north =
the_twelite.is_wokeup_by_dio(CUE::PIN_SNS_NORTH);
uint8_t b_south =
the_twelite.is_wokeup_by_dio(CUE::PIN_SNS_SOUTH);
LED
set_led()
void set_led(uint8_t mode, uint16_t tick)
Controls the LED (D1).
mode
accepts the following parameters. tick
specifies the lighting duration in milliseconds [ms]. Refer to the mode descriptions for details.
Value | Description |
---|---|
LED_TIMER::BLINK | Blinks the LED. Toggles ON/OFF at intervals specified by tick [ms]. After waking from sleep, the timer resets and starts in the ON state. |
LED_TIMER::ON_RX | Turns on the LED for the specified tick duration [ms] when a packet is received. |
LED_TIMER::ON_TX_COMP | Turns on the LED for the specified tick duration [ms] when transmission completes. |
led_one_shot()
void led_one_shot(uint16_t tick)
Turns on the LED for a specified duration. Cannot be used simultaneously with set_led()
.
Watchdog Timer
Resets the external watchdog timer at startup, upon waking from sleep, and after a certain time has elapsed post-startup.
4 - <CUE>
Provides access to the onboard accelerometer, magnetic sensor, and LED.
- Accelerometer
- Magnetic Sensor
- LED
void setup() {
auto&& brd = the_twelite.board.use<CUE>();
}
Accelerometer
The member object (sns_MC3630
) of the MC3630 sensor is defined.
Magnetic Sensor
The sensor used in the Door Sensor PAL is a magnetic sensor, with two interrupt input lines only.
const uint8_t CUE::PIN_SNS_NORTH = 16;
const uint8_t CUE::PIN_SNS_OUT1 = 16;
const uint8_t CUE::PIN_SNS_SOUTH = 8;
const uint8_t CUE::PIN_SNS_OUT2 = 8;
CUE::PIN_SNS_NORTH
triggers an interrupt when the north pole is detected; CUE::PIN_SNS_SOUTH
also triggers on north pole detection.
Configure the following before entering sleep:
pinMode(CUE::PIN_SNS_OUT1, PIN_MODE::WAKE_FALLING);
pinMode(CUE::PIN_SNS_OUT2, PIN_MODE::WAKE_FALLING);
On wake-up, check which GPIO triggered the event:
uint8_t b_north =
the_twelite.is_wokeup_by_dio(CUE::PIN_SNS_NORTH);
uint8_t b_south =
the_twelite.is_wokeup_by_dio(CUE::PIN_SNS_SOUTH);
LED
set_led()
void set_led(uint8_t mode, uint16_t tick)
Controls LED (D1).
mode
takes the following parameters. tick
specifies the lighting duration [ms]; see the explanation of mode
for details.
Value | Description |
---|---|
LED_TIMER::BLINK | Blinks the LED. Toggles ON/OFF every tick milliseconds. Resets counter after sleep and starts ON. |
LED_TIMER::ON_RX | Turns on LED for tick milliseconds upon packet reception. |
LED_TIMER::ON_TX_COMP | Turns on LED for tick milliseconds when transmission completes. |
led_one_shot()
void led_one_shot(uint16_t tick)
Turns on LED for a specified duration. Cannot be used simultaneously with set_led()
.
Watchdog Timer
Resets the external watchdog timer at startup, after waking from sleep, and after a certain time post-start.
5 - <MONOSTICK>
Constants
The following constants are defined:
const uint8_t PIN_LED = mwx::PIN_DIGITAL::DIO16; // LED
const uint8_t PIN_WDT = mwx::PIN_DIGITAL::DIO9; // WDT (shall tick < 1sec)
const uint8_t PIN_WDT_EN = mwx::PIN_DIGITAL::DIO11; // WDT (LO as WDT enabled)
const uint8_t PIN_LED_YELLOW = mwx::PIN_DIGITAL::DO1; // YELLOW LED
Accessible via MONOSTICK::PIN_LED
.
Hardware Initialization
pinMode(PIN_LED, OUTPUT_INIT_HIGH);
pinMode(PIN_WDT, OUTPUT_INIT_LOW);
pinMode(PIN_WDT_EN, OUTPUT_INIT_LOW);
pinMode(PIN_LED_YELLOW, OUTPUT);
Pins are initialized as shown in the code above.
Watchdog Timer
The external watchdog timer is reset at startup, after waking from sleep, and after a set duration.
The watchdog timeout is one second.
Although MONOSTICK typically runs non-sleeping applications, if using sleep, set MONOSTICK::PIN_WDT_EN
to HIGH before sleeping.
Methods
set_led()
void set_led_red(uint8_t mode, uint16_t tick)
void set_led_yellow(uint8_t mode, uint16_t tick)
Controls the red and yellow LEDs.
The yellow LED (MONOSTICK::PIN_LED_YELLOW
) is the SPIMISO pin (semiconductor pin name DO1). This board behavior does not include methods or procedures for PWM control. Implement the following steps if needed:
- Avoid calling
set_led_yellow()
. - Initialize PWM output separately after startup. The SPIMISO pin corresponds to PWM3 in App_Twelite and can be controlled by the Timer3 class object.
- Reinitialize PWM output after waking from sleep. At that time, disable output settings for DO1.
- Disable DO1 output before configuring PWM:
pinMode(PIN_LED_YELLOW, DISABLE_OUTPUT);
MONOSTICK::PIN_LED_YELLOW
) cannot be lit during sleep.mode
accepts the following parameters. tick
specifies the lighting duration in milliseconds [ms]; see the descriptions for each mode below.
Parameter | Meaning |
---|---|
LED_TIMER::BLINK | The LED blinks. ON/OFF toggles every tick milliseconds. After waking from sleep, the count resets and starts in the ON state. |
LED_TIMER::ON_RX | Lights the LED for tick milliseconds upon packet reception. |
LED_TIMER::ON_TX_COMP | Lights the LED for tick milliseconds upon transmission completion. |