/
core1.cpp
66 lines (57 loc) · 1.5 KB
/
core1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <Arduino.h>
#include "pico-badge.h"
// Core #1 only handles buttons
// The only thing shared with core 0
extern volatile uint16_t buttons;
// Button pins
static unsigned ipins[] = {BTNA, BTNB, BTNC, BTND, JUP, JDN, JLF, JRT, JPR};
// Init pins for core #1
static void initIO1()
{
for (int i = 0; i < sizeof(ipins) / sizeof(ipins[0]); i++)
{
pinMode(ipins[i], INPUT_PULLUP);
}
}
// Read the buttons (call from core #1)
static unsigned readpins(void)
{
int i;
unsigned rv = 0, mask = 1;
for (i = 0; i < sizeof(ipins) / sizeof(ipins[0]);i++)
{
if (digitalRead(ipins[i]))
rv |= mask;
mask <<= 1;
}
return 0x1FF ^ rv; // active low buttons so flip 'em
}
void setup1() // second core will handle buttons
{
initIO1();
}
// Read buttons with short debounce delay
static unsigned readdebounce(void)
{
unsigned s1, s2;
s1 = readpins();
delay(33); // debounce delay
s2 = readpins();
return s1 & s2; // must be on both times
}
// Core #1 just reads the buttons
// This code ensures that one button push gets one key event
// to core #0 -- Again, idleOtherCore acts like a critical section
void loop1()
{
static unsigned btnstate = 0;
static unsigned xbtn;
unsigned cbutton;
cbutton = readdebounce(); // current buttons
xbtn = cbutton & ~btnstate; // turn off any that were on before
btnstate = cbutton; // ready for next time
rp2040.idleOtherCore();
buttons |= xbtn; // put in any that are set; client will reset them
rp2040.resumeOtherCore();
delay(5); // give other core some time to run
}