2進LEDっぽい何か。


2進LEDっぽい何か。寝起きの落書き。
昨夜の7セグLEDモジュールをベースに弄ってるので捻りないです。
16進数から2進数変換とかのエレガントなコードなら他所にいっぱいありますお。


devkitPro1.5.0用です(1.4.9で書いてますが多分大丈夫。)。


BinLED test


main.c

/*---------------------------------------------------------------------------------
	
	BinLED module test
	
	version 0.01
	Oct 03, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/
#include <nds.h>
#include <stdio.h>

#include "BinLED.h"

//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	char bin_led = 0x00;
	uint32 kdr;
	
	consoleDemoInit();
	keysSetRepeat(30,10);
	
	BinLED_ClearBinLED();
	
	while(1) {
		
		swiWaitForVBlank();
		
		BinLED_ShowBinLED(bin_led);
		printf("\x1b[4;6H0x%02X", bin_led);
		
		scanKeys();
		kdr = keysDownRepeat();
		
		if(kdr & KEY_UP){
			
			if(bin_led < 0x7F){
				bin_led++;
			}else{
				bin_led = 0x00;
			}
			
		}else if(kdr & KEY_DOWN){
			
			if(bin_led > 0x00){
				bin_led--;
			}else{
				bin_led = 0x7F;
			}
			
		}else if(kdr & KEY_START){
			
			bin_led = 0xFF;
			
		}
		
	}
	
}

//---------------------------------------------------------------------------------


BinLED.h

/*---------------------------------------------------------------------------------
	
	BinLED module header
	
	version 0.01
	Oct 03, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _BIN_LED_H_
#define _BIN_LED_H_


#include <nds.h>
#include <stdio.h>


#ifdef __cplusplus
extern "C" {
#endif


void BinLED_ShowBinLED(char bin_led);
void BinLED_HideBinLED(void);
void BinLED_ClearBinLED(void);


#ifdef __cplusplus
}
#endif

#endif	//_BIN_LED_H_


BinLED.c

/*---------------------------------------------------------------------------------
	
	BinLED module routine
	
	version 0.01
	Oct 03, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/

#include "BinLED.h"


#define BIN_LED_POSX 0
#define BIN_LED_POSY 0


//|BIN|--|04|02|01|08|04|02|01|
//|bit|--|06|05|04|03|02|01|00|

typedef enum{
	BIN_0 = 0x01, 
	BIN_1 = 0x02, 
	BIN_2 = 0x04, 
	BIN_3 = 0x08, 
	BIN_4 = 0x10, 
	BIN_5 = 0x20, 
	BIN_6 = 0x40, 
	BIN_7 = 0x80  //debug only
}BIN_LED;


static char st_bin_led = 0x00;


#define IS_BIN_LED(x) ((x) ? true:false)

char binLED_GetBinLED(void);
void binLED_SetBinLED(char bin_led);
void binLED_DrawBinLED(char bin_led);


//---------------------------------------------------------------------------------
char binLED_GetBinLED(void){
//---------------------------------------------------------------------------------
	return st_bin_led;
}

//---------------------------------------------------------------------------------
void binLED_SetBinLED(char bin_led){
//---------------------------------------------------------------------------------
	st_bin_led = bin_led & 0x7F;
}

//---------------------------------------------------------------------------------
void binLED_DrawBinLED(char bin_led){
//---------------------------------------------------------------------------------
	
	printf("\x1b[%d;%dH%d", BIN_LED_POSY, BIN_LED_POSX, IS_BIN_LED(bin_led & BIN_7));//debug only
	printf("\x1b[%d;%dH%d", BIN_LED_POSY, BIN_LED_POSX + 1, IS_BIN_LED(bin_led & BIN_6));
	printf("\x1b[%d;%dH%d", BIN_LED_POSY, BIN_LED_POSX + 2, IS_BIN_LED(bin_led & BIN_5));
	printf("\x1b[%d;%dH%d", BIN_LED_POSY, BIN_LED_POSX + 3, IS_BIN_LED(bin_led & BIN_4));
	printf("\x1b[%d;%dH%d", BIN_LED_POSY, BIN_LED_POSX + 4, IS_BIN_LED(bin_led & BIN_3));
	printf("\x1b[%d;%dH%d", BIN_LED_POSY, BIN_LED_POSX + 5, IS_BIN_LED(bin_led & BIN_2));
	printf("\x1b[%d;%dH%d", BIN_LED_POSY, BIN_LED_POSX + 6, IS_BIN_LED(bin_led & BIN_1));
	printf("\x1b[%d;%dH%d", BIN_LED_POSY, BIN_LED_POSX + 7, IS_BIN_LED(bin_led & BIN_0));
	
}

//---------------------------------------------------------------------------------
void BinLED_ShowBinLED(char bin_led){
//---------------------------------------------------------------------------------
	binLED_SetBinLED(bin_led);
	binLED_DrawBinLED(binLED_GetBinLED());
}

//---------------------------------------------------------------------------------
void BinLED_HideBinLED(void){
//---------------------------------------------------------------------------------
	BinLED_ShowBinLED(0x00);
}

//---------------------------------------------------------------------------------
void BinLED_ClearBinLED(void){
//---------------------------------------------------------------------------------
	BinLED_ShowBinLED(0x00);
}

//---------------------------------------------------------------------------------