MCPcopy Create free account
hub / github.com/SmingHub/Sming / interruptHandler

Function interruptHandler

samples/Basic_Interrupts/app/application.cpp:27–59  ·  view source on GitHub ↗

@brief Low-level interrupt handler * @note An interrupt handling callback must have the IRAM_ATTR attribute. * Interrupt processing code should be as short as possible. * You could perhaps set a flag, then check it in your main code (timers, etc) or read actual * pin state and save it to a global variable. * Avoid doing things like calling malloc(), new(), reading Flash memory. * If yo

Source from the content-addressed store, hash-verified

25 * If your application is not timing-critical, then use an InterruptDelegate callback instead.
26 */
27void IRAM_ATTR interruptHandler()
28{
29 // For this example, we just toggle the state of an output pin.
30 bool state = digitalRead(TOGGLE_PIN);
31 digitalWrite(TOGGLE_PIN, !state);
32
33 // Example of how you can queue a callback from inside a regular interrupt handler
34 const unsigned MAX_TOGGLE_COUNTS = 10;
35 ++interruptToggleCount;
36 if(interruptToggleCount > MAX_TOGGLE_COUNTS) {
37 System.queueCallback(showInterruptToggleCount, interruptToggleCount);
38 /*
39 * Note that `queueCallback` also supports std::function arguments, so we can use lambdas,
40 * class methods, etc.
41 *
42 * For example, we could use a lambda to capture to capture the instantaneous value of 'toggleCount':
43 *
44 * ```
45 * System.queueCallback([toggleCount]() {
46 * showInterruptToggleCount(toggleCount);
47 * };
48 * ```
49 *
50 * IMPORTANT: the lambda inherits this function's context, so will be stored in IRAM which
51 * is a very limited resource. The lambda is therefore best suited to simple 'glue' code.
52 *
53 * IMPORTANT: Avoid using std::bind from interrupt handlers because it may attempt to allocate
54 * storage on the heap; this will likely crash the system.
55 *
56 */
57 interruptToggleCount = 0;
58 }
59}
60
61/** @brief Example of an InterruptDelegate function
62 * @note Unlike interruptHandler() above, this function is not called directly from an interrupt so there

Callers

nothing calls this directly

Calls 3

digitalReadFunction · 0.50
digitalWriteFunction · 0.50
queueCallbackMethod · 0.45

Tested by

no test coverage detected