Skip to content

Commit 85a4871

Browse files
committed
samples: Added attach_interrupt sample
Add a sample to demonstrate how to use attachInterrupt Signed-off-by: TOKITA Hiroshi <[email protected]>
1 parent 69afdab commit 85a4871

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
set(DTC_OVERLAY_FILE $ENV{ZEPHYR_BASE}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay)
6+
7+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
8+
project(attach_interrupt)
9+
10+
target_sources(app PRIVATE src/main.cpp)
11+
12+
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/attach_interrupt/README.rst

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.. _attach_interrupt-sample:
2+
3+
AttachInterrupt
4+
######
5+
6+
Overview
7+
********
8+
9+
This sample demonstrates how to use attachInterrupt API.
10+
11+
Building and Running
12+
********************
13+
14+
Build and flash attachInterrupt sample as follows,
15+
16+
```sh
17+
$> west build -p -b arduino_nano_33_ble samples/basic/attach_interrupt/ -DZEPHYR_EXTRA_MODULES=/home/$USER/zephyrproject/modules/lib/Arduino-Core-Zephyr
18+
19+
$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac
20+
```
21+
22+
Turn on the LED by detecting interrupts. And Turn off the next interrupt.

samples/attach_interrupt/prj.conf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_GPIO=y
2+
CONFIG_CPLUSPLUS=y
3+
CONFIG_ARDUINO_API=y
4+
CONFIG_GPIO_GET_CONFIG=y

samples/attach_interrupt/src/main.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
#include <Arduino.h>
6+
7+
const byte ledPin = LED_BUILTIN;
8+
const byte interruptPin = 2;
9+
volatile PinStatus state = LOW;
10+
11+
#ifndef digitalPinToInterrupt
12+
#define digitalPinToInterrupt(p) p
13+
#endif
14+
15+
void blink() {
16+
state = (PinStatus)!state;
17+
}
18+
19+
void setup() {
20+
pinMode(ledPin, OUTPUT);
21+
pinMode(interruptPin, INPUT_PULLUP);
22+
attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
23+
}
24+
25+
void loop() {
26+
digitalWrite(ledPin, state);
27+
}
28+

0 commit comments

Comments
 (0)