Dan Goldsmith
Object will also have a set of functions associate with them.
What do we need to do with a specific object?
So a person may have:
Personally, I like to take an iterative approach to deriving classes and functions. What are the core elements? What are the core tasks? It if becomes clear that we need more functionality then we can update the documentation.
// Variable to hold button State, this needs to be globally avaialable
int buttonState;
// Hander Function for our Buton Press
void handler(){
printf("Update Button State\n");
buttonState = !buttonState;
}
int main() {
// put your setup code here, to run once:
//Setup the LED1 pin to be a digital output called ledOne
DigitalOut ledOne(LED1);
//Setup the User Buton as an Input
InterruptIn theButton(USER_BUTTON);
//Bind the interupt to a function
theButton.rise(&handler);
while(1) {
// put your main code here, to run repeatedly:
//Print the State of our button
printf("Button State is %d\n", buttonState);
//Is the Button Pressed
if (buttonState) {
ledOne = 1;
thread_sleep_for(2000);
ledOne = 0;
thread_sleep_for(2000);
}
else{
ledOne = 1;
thread_sleep_for(500);
ledOne = 0;
thread_sleep_for(500);
}
}
}
Take the form Return Type Funtion Name (parameters)
void flash(int sleep){
ledOne = 1;
thread_sleep(sleep);
ledOne = 0;
thread_sleep(sleep);
}
int main() {
//Flash with one second Delay
flash(1000);
What else can we change?
void flash(DigitalOut theLed, int ledOn, int ledOff){
...
}
class Flasher{
// Attributes
// Methods
};
class Flasher{
// Attributes
private:
DigitalOut theLed;
int state;
// Methods
};
class Flasher{
// Variables
private:
DigitalOut theLed;
int state;
// Methods
public:
void flash(){
if (state == 0){
theLed = 1;
thread_sleep_for(1000);
theLed = 0;
thread_sleep_for(1000);
}
else {
theLed = 1;
thread_sleep_for(5000);
theLed = 0;
thread_sleep_for(5000);
}
}
};
Flasher(PinName thePin, int startState) : theLed(thePin){
state = startState;
}
int main() {
....
// Global Flasher Object
Flasher theFlasher(LED1, 0);
while(1) {
theFlasher.flash();
}
}
class Flasher{
// Variables
private:
DigitalOut theLed;
int state;
// Methods
public:
Flasher(PinName thePin, int startState) : theLed(thePin){
state = startState;
}
void flash(){
if (state == 0){
theLed = 1;
thread_sleep_for(1000);
theLed = 0;
thread_sleep_for(1000);
}
else {
theLed = 1;
thread_sleep_for(5000);
theLed = 0;
thread_sleep_for(5000);
}
}
};
// Variable to hold button State, this needs to be globally avaialable
int buttonState;
// Hander Function for our Buton Press
void handler(){
printf("Update Button State\n");
buttonState = !buttonState;
}
int main() {
// put your setup code here, to run once:
//Setup the User Buton as an Input
InterruptIn theButton(USER_BUTTON);
//Bind the interupt to a function
theButton.rise(&handler);
// Global Flasher Object
Flasher theFlasher(LED1, 0);
while(1) {
//Print the State of our button
printf("Button State is %d\n", buttonState);
theFlasher.flash();
}
}