PK70 Status LEDs

Post your example source code and application notes to share with others
Post Reply
User avatar
mx270a
Posts: 80
Joined: Tue Jan 19, 2010 6:55 pm

PK70 Status LEDs

Post by mx270a » Sun Sep 18, 2011 8:27 pm

The standard method of controlling the two LEDs on the PK70 is to use the putleds() function. Since the state of both LEDs are set at the same time when running that function, if you only want to change the state of one of the LEDs, you will have to remember how you last set the other LED. I wrote this snippit of code to make it easier to change the status of a single LED and to be able to have them blink with a simple command in the while(1) loop.

Code: Select all

#include "predef.h"
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <autoupdate.h>

extern "C" {
void UserMain(void * pd);
}

const char * AppName="PK70 LED Example";

int LED1 = 0;
int LED2 = 0;
void SetLED(int led, int color=0, bool blink=false) {
	//The first parameter can be 0 (both LEDs), 1 (Status 1 LED), or 2 (Status 2 LED)
	//The second parameter is the color. 0 is off, 1 is red, 2 is green. If unspecified, it will default to 0 (off).
	//The third parameter can be set to true if you want the LED(s) to blink.
	if (led < 2) { //Either 0 or 1
		if (blink && LED1>0) {
			LED1 = 0;
		} else {
			LED1 = color;
		}
	}
	if (led != 1) { //Either 0 or 2
		if (blink && LED2>0) {
			LED2 = 0;
		} else {
			LED2 = color*4;
		}
	}
	putleds(LED1 | LED2); //OR the bits
}

void UserMain(void * pd) {
    InitializeStack();
    OSChangePrio(MAIN_PRIO);
    EnableAutoUpdate();

    while (1) {
    	SetLED(1,1); //This sets LED1 to 1 (red)
    	OSTimeDly(20);
    	SetLED(1,2); //This sets LED1 to 2 (green)
    	OSTimeDly(20);
    	SetLED(2,1); //This sets LED2 to 1 (red)
    	OSTimeDly(20);
    	SetLED(2,2); //This sets LED1 to 2 (green)
    	OSTimeDly(20);
    	SetLED(0,2,true); //This makes both LEDs blink green
    	OSTimeDly(20);
    	SetLED(0,2,true); //This makes both LEDs blink green
    	OSTimeDly(20);
    	SetLED(0,1,true); //This makes both LEDs blink red
    	OSTimeDly(20);
    	SetLED(0,1,true); //This makes both LEDs blink red
    	OSTimeDly(20);
    	SetLED(0); //This turns off both LEDs
    	OSTimeDly(20);
    }
}
-Lance

Post Reply