Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-UC MODSERIAL mbed mbos
Fork of CDU_Mbed_30 by
buttons.cpp
- Committer:
- LvdK
- Date:
- 2012-12-08
- Revision:
- 5:99594f4ab659
File content as of revision 5:99594f4ab659:
// L. van der Kolk, ELVEDEKA, Holland //
// File: buttons.cpp version 1.0
#include "mbed.h"
#define TRUE 1
#define FALSE 0
extern Serial USB;
int button_flag = 0;
char key_message[20] = "$PCDUKEY,"; // : setup begin of key message string to FS
BusInOut button(p24, p23, p22, p21); // : Mbed buttons as 4 bit bus
void init_buttons_Mbed() {
// set button bus as input :
button.input();
// enable pull up resistors on inputs:
button.mode(PullUp);
}
void scan_buttons_Mbed() {
static int pushed = FALSE;
switch (button) {
case 0xF : {
// nothing pushed, reset pushed flag:
pushed = FALSE;
break;
}
case 0x7 : {
// button 1 pushed:
if ( pushed == FALSE ) button_flag = 1;
pushed = TRUE;
break;
}
case 0xB : {
// button 2 pushed:
if ( pushed == FALSE ) button_flag = 2;
pushed = TRUE;
break;
}
case 0xD : {
// button 3 pushed:
if ( pushed == FALSE ) button_flag = 3;
pushed = TRUE;
break;
}
case 0xE : {
// button 4 pushed:
if ( pushed == FALSE ) button_flag = 4;
pushed = TRUE;
break;
}
default : break;
}
}
void collect_and_send_FSdata() {
int i;
char byte_read;
char exor_byte = 0;
if ( button_flag != 0 ) {
// Create key message:
i = 9; // : i points to first place after $PCDUKEY,
// Add key identifier 'SKEY' and a '*' char to key message string:
sprintf(&key_message[i],"%s","SKEY");
// Add 2 digit keynumber and a '*' char :
i = 13; // : i points to first place after $PCDUKEY,SKEY
sprintf(&key_message[i],"%02d*",button_flag);
// Calculate checksum now :
i = 1; // : i points to first place after '$'
do { byte_read = key_message[i];
if (byte_read == '*') break; // : exclude '*' from calculation
exor_byte = exor_byte ^ byte_read;
i++;
} while ( i < 20 );
i++; // : i now points to first digit of checksum after '*'
// Add exor_byte in 2 hex digits and a CR + LF:
sprintf(&key_message[i],"%02x\r\n",exor_byte); // : ! extra NULL char added by sprintf
USB.printf("%s",key_message);
button_flag = 0;
}
}
