Skip to content

Commit 14657b4

Browse files
committed
fixed ATmega328 pcint tables
1 parent 2727ca7 commit 14657b4

File tree

3 files changed

+74
-62
lines changed

3 files changed

+74
-62
lines changed

README.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Works either over serial or with LCD + encoder
99
V2.0
1010

1111
main changes:
12-
- non-blocking menu mode
12+
- non-blocking menu main cycle
1313
- Menufields as menu prompts with associated value
1414
values can be:
1515
numeric withing range
@@ -23,45 +23,45 @@ notes:
2323
-------------------------------------------------------------------------------
2424
example of menu definition (c++ macros)
2525

26-
//a submenu
27-
MENU(ledMenu,"LED on pin 13",
28-
OP("LED On",ledOn),
29-
OP("LED Off",ledOff)
30-
);
31-
32-
//field value toggle on click
33-
TOGGLE(targetVar,trigModes,"Mode: ",
34-
VALUE("None",trigPoint::None),
35-
VALUE("On rise",trigPoint::onRise),
36-
VALUE("On fall",trigPoint::onFall),
37-
VALUE("Both",trigPoint::isIn)
38-
);
39-
40-
//field value, click to browse, click to choose
41-
CHOOSE(adc_prescale,sample_clock,"Sample clock",
42-
VALUE("/128",avrADC::clk_128,setADCClk),
43-
VALUE("/64",avrADC::clk_64,setADCClk),
44-
VALUE("/32",avrADC::clk_32,setADCClk),
45-
VALUE("/16",avrADC::clk_16,setADCClk),
46-
VALUE("/8",avrADC::clk_8,setADCClk),
47-
VALUE("/4",avrADC::clk_4,setADCClk),
48-
VALUE("/2",avrADC::clk_2,setADCClk)
49-
);
50-
51-
//the main menu...
52-
//Fields are numeric and show selected value. click to start change, click to fine tune, click to end
53-
MENU(mainMenu,"Main menu",
54-
FIELD(frequency,"Freq","Hz",0,16000000,100,1,updateFreq),
55-
FIELD(dutty,"Duty","%",0,100,1,0,updateDutty),
56-
OP("Handler test",completeHandlerTest),
57-
SUBMENU(trigModes),
58-
SUBMENU(ledMenu)
59-
);
26+
//a submenu
27+
MENU(ledMenu,"LED on pin 13",
28+
OP("LED On",ledOn),
29+
OP("LED Off",ledOff)
30+
);
31+
32+
//field value toggle on click
33+
TOGGLE(targetVar,trigModes,"Mode: ",
34+
VALUE("None",trigPoint::None),
35+
VALUE("On rise",trigPoint::onRise),
36+
VALUE("On fall",trigPoint::onFall),
37+
VALUE("Both",trigPoint::isIn)
38+
);
39+
40+
//field value, click to browse, click to choose
41+
CHOOSE(adc_prescale,sample_clock,"Sample clock",
42+
VALUE("/128",avrADC::clk_128,setADCClk),
43+
VALUE("/64",avrADC::clk_64,setADCClk),
44+
VALUE("/32",avrADC::clk_32,setADCClk),
45+
VALUE("/16",avrADC::clk_16,setADCClk),
46+
VALUE("/8",avrADC::clk_8,setADCClk),
47+
VALUE("/4",avrADC::clk_4,setADCClk),
48+
VALUE("/2",avrADC::clk_2,setADCClk)
49+
);
50+
51+
//the main menu...
52+
//Fields are numeric and show selected value. click to start change, click to fine tune, click to end
53+
MENU(mainMenu,"Main menu",
54+
FIELD(frequency,"Freq","Hz",0,16000000,100,1,updateFreq),
55+
FIELD(dutty,"Duty","%",0,100,1,0,updateDutty),
56+
OP("Handler test",completeHandlerTest),
57+
SUBMENU(trigModes),
58+
SUBMENU(ledMenu)
59+
);
6060

6161
-------------------------------------------------------------------------------
6262
syntax:
6363

64-
OP(name,function)
64+
OP(name,function)
6565
name string to be shown as menu option prompt
6666
function to be called on click
6767

examples/gfx_menu/gfx_menu.ino

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,35 @@ ruihfazevedo@[email protected]
1414
#include <Adafruit_ST7735.h> // Hardware-specific library
1515
#include <menuGFX.h>
1616

17-
///////////////////////////////////////////////////////////////////////////
18-
//TFT + SD
19-
#define sdCS 9
20-
#define tftCS 8
21-
#define dc 6
22-
#define rst 7
17+
#if defined(__AVR_ATmega2560__)
18+
///////////////////////////////////////////////////////////////////////////
19+
//TFT + SD
20+
//#define sdCS 9//not using SD card
21+
#define tftCS 8
22+
#define dc 6
23+
#define rst 7
24+
////////////////////////////////////////////
25+
// ENCODER (aka rotary switch) PINS
26+
#define encA 12
27+
#define encB 10
28+
#define encBtn 13
29+
#elif defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
30+
///////////////////////////////////////////////////////////////////////////
31+
//TFT + SD
32+
//#define sdCS 9//not using SD card
33+
#define tftCS 10
34+
#define dc 7
35+
#define rst 8
36+
////////////////////////////////////////////
37+
// ENCODER (aka rotary switch) PINS
38+
#define encA A2
39+
#define encB A1
40+
#define encBtn A3
41+
#else
42+
#error "Uknown pinout"
43+
#endif
2344

2445
Adafruit_ST7735 tft(tftCS, dc, rst);
25-
26-
////////////////////////////////////////////
27-
// ENCODER (aka rotary switch) PINS
28-
// mega 2560
29-
#define encA 10
30-
#define encB 12
31-
#define encBtn 13
32-
3346
///////////////////////////////////////////////////////////////////////////
3447
//functions to wire as menu actions
3548

pcint.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ Nov.2014 large changes
3636
#endif
3737

3838
#define digitalPinFromPCINTSlot(slot,bit) pgm_read_byte(pcintPinMap+(((slot)<<3)+(bit)))
39-
#define pcintPinMapBank(slot) ((uint8_t*)(pcintPinMap+((slot)<<3)))
40-
#define digitalPinFromPCINTBank(bank,bit) pgm_read_byte(bank+bit)
41-
39+
#define pcintPinMapBank(slot) ((uint8_t*)((uint8_t*)pcintPinMap+((slot)<<3)))
40+
#define digitalPinFromPCINTBank(bank,bit) pgm_read_byte((uint8_t*)bank+bit)
4241

4342
//this handler can be used instead of any void(*)() and optionally it can have an associated void *
4443
//and use it to call void(*)(void* payload)
@@ -62,12 +61,12 @@ Nov.2014 large changes
6261

6362
static int PCintMode[3][8];
6463
static HANDLER_TYPE PCintFunc[3][8];
65-
static uint8_t PCintLast[3][8];
64+
static bool PCintLast[3][8];//?we can use only 3 bytes... but will use more processing power calculating masks
6665

6766
/*
6867
* attach an interrupt to a specific pin using pin change interrupts.
6968
*/
70-
void PCattachInterrupt(uint8_t pin, class mixHandler userFunc, int mode) {
69+
void PCattachInterrupt(uint8_t pin, class mixHandler userFunc, uint8_t mode) {
7170
volatile uint8_t *pcmask=digitalPinToPCMSK(pin);
7271
if (!pcmask) return;//runtime checking if pin has PCINT, i would prefer a compile time check
7372
uint8_t bit = digitalPinToPCMSKbit(pin);
@@ -93,24 +92,24 @@ Nov.2014 large changes
9392
PCICR &= ~(1<<digitalPinToPCICRbit(pin));
9493
}
9594

96-
volatile int x=0;
9795
// common code for isr handler. "port" is the PCINT number.
9896
// there isn't really a good way to back-map ports and masks to pins.
9997
// here we consider only the first change found ignoring subsequent, assuming no interrupt cascade
10098
static void PCint(uint8_t port) {
10199
const uint8_t* map=pcintPinMapBank(port);//get 8 bits pin change map
102100
for(int i=0;i<8;i++) {
103-
uint8_t p=digitalPinFromPCINTBank(map,i);
104-
if (p==-1) continue;//its not assigned
101+
uint8_t p=digitalPinFromPCINTBank(map,i);
102+
if (p==-1)
103+
continue;//its not assigned
105104
//uint8_t bit = digitalPinToPCMSKbit(p);
106105
//uint8_t mask = (1<<bit);
107106
if (PCintFunc[port][i]!=NULL) {//only check active pins
108-
uint8_t stat=(*portInputRegister(digitalPinToPort(p)))&digitalPinToBitMask(p);
107+
bool stat=(*portInputRegister(digitalPinToPort(p)))&digitalPinToBitMask(p);
109108
if (PCintLast[port][i]^stat) {//pin changed!
110-
PCintLast[port][i]=stat;
109+
PCintLast[port][i]=stat;//register change
111110
if (
112-
PCintMode[port][i]==CHANGE
113-
|| (stat&&(PCintMode[port][i]==RISING))
111+
(PCintMode[port][i]==CHANGE)
112+
|| ((stat)&&(PCintMode[port][i]==RISING))
114113
|| ((!stat)&&(PCintMode[port][i]==FALLING))
115114
) {
116115
PCintFunc[port][i]();

0 commit comments

Comments
 (0)