アウトプットっぽい何か。


先日の2進LED, 7セグLEDの改良版。アウトプットに集約。
これによって両LEDは描画に専念するのみとなりました。
一応目的があってこのような構造としました。


7セグLEDの指摘があった箇所は
リソースを揃えたら直してしまうつもりです。


Outport module test


main.c

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

#include "Outport.h"


char *mode_name[] = { "DEFAULT", "SWAP   " };


//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	u8 num_val = 0x00;
	u8 bin_val = 0x00;
	
	int mode;
	
	consoleDemoInit();
	
	mode = Outport_InitOutport();
	
	while(1) {
		
		swiWaitForVBlank();
		
		Outport_ShowOPortValue(num_val);
		Outport_ShowRPortValue(bin_val);
		
		printf("\x1b[8;0HMAPPING MODE %d:%s", mode, mode_name[mode]);
		
		scanKeys();
		
		if(keysDown() & KEY_UP){
			
			if(bin_val < 0x7F){
				bin_val++;
			}else{
				bin_val = 0x00;
			}
			
		}else if(keysDown() & KEY_DOWN){
			
			if(bin_val > 0x00){
				bin_val--;
			}else{
				bin_val = 0x7F;
			}
			
		}else if(keysDown() & KEY_RIGHT){
			
			if(num_val < 0x0F){
				num_val++;
			}else{
				num_val = 0x00;
			}
			
		}else if(keysDown() & KEY_LEFT){
			
			if(num_val > 0x00){
				num_val--;
			}else{
				num_val = 0x0F;
			}
			
		}
		
		if(keysDown() & KEY_SELECT){
			mode = Outport_SwapMappingMode();
		}
		
	}
	
}

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


Outport.h

/*---------------------------------------------------------------------------------
	
	Outport module header
	
	version 0.01
	Oct 07, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _OUTPORT_H_
#define _OUTPORT_H_


#include <nds/ndstypes.h>


#ifdef __cplusplus
extern "C" {
#endif


int Outport_InitOutport(void);
int Outport_ClearOutport(void);

int Outport_SwapMappingMode(void);
int Outport_ClearMappingMode(void);

void Outport_ShowOPortValue(u8 num_val);
void Outport_HideOPortValue(void);
void Outport_ClearOPortValue(void);

void Outport_ShowRPortValue(u8 bin_val);
void Outport_HideRPortValue(void);
void Outport_ClearRPortValue(void);


#ifdef __cplusplus
}
#endif

#endif	//_OUTPORT_H_


OutportPrivate.h

/*---------------------------------------------------------------------------------
	
	Outport module Private header
	
	version 0.01
	Oct 07, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _OUTPORT_PRIVATE_H_
#define _OUTPORT_PRIVATE_H_


#include "Outport.h"
#include "NumLED.h"
#include "BinLED.h"


#ifdef __cplusplus
extern "C" {
#endif


#ifdef __cplusplus
}
#endif

#endif	//_OUTPORT_PRIVATE_H_


Outport.c

/*---------------------------------------------------------------------------------
	
	Outport module routine
	
	version 0.01
	Oct 07, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/

#include "OutportPrivate.h"


//Outport OPort -> NumberLED
// AO - Ar -> Op
// CAL RSTO - Reset Op

//Outport RPort -> BinaryLED
// CAL SETR
// CAL RSTR
// CAL DSPR


typedef enum{
	MAPPING_DEF,
	MAPPING_SWAP
}MAPPING_MODE;


typedef struct{
	u8 oport_val;
	u8 rport_val;
	MAPPING_MODE mapping_mode;
}OutportStatus;


static OutportStatus st_outport;


void (*pDrawLED[2])(u8 val);


u8 outport_GetOPortValue(void);
void outport_SetOPortValue(u8 seg_val);
u8 outport_GetRPortValue(void);
void outport_SetRPortValue(u8 bin_val);
MAPPING_MODE outport_GetMappingMode(void);
void outport_SetMappingMode(MAPPING_MODE mapping_mode);


//---------------------------------------------------------------------------------
// Outport OPort Getter / Setter
//---------------------------------------------------------------------------------
u8 outport_GetOPortValue(void){
//---------------------------------------------------------------------------------
	return st_outport.oport_val;
}

//---------------------------------------------------------------------------------
void outport_SetOPortValue(u8 num_val){
//---------------------------------------------------------------------------------
	
	const static u8 seg_pattern[] = {
		//7 Seg LED hexa number pattern
		0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 
		0x7F, 0x67, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 
		0x00
	};
	
	//配列外の値を送り込まれた時の対処(暫定)
	if(num_val > 0x10)
		num_val = 0x00;
	
	st_outport.oport_val = seg_pattern[num_val];
	
}


//---------------------------------------------------------------------------------
// Outport RPort Getter / Setter
//---------------------------------------------------------------------------------
u8 outport_GetRPortValue(void){
//---------------------------------------------------------------------------------
	return st_outport.rport_val;
}

//---------------------------------------------------------------------------------
void outport_SetRPortValue(u8 bin_val){
//---------------------------------------------------------------------------------
	st_outport.rport_val = bin_val & 0x7F;
}


//---------------------------------------------------------------------------------
// Outport Mapping Getter / Setter
//---------------------------------------------------------------------------------
MAPPING_MODE outport_GetMappingMode(void){
//---------------------------------------------------------------------------------
	return st_outport.mapping_mode;
}

//---------------------------------------------------------------------------------
void outport_SetMappingMode(MAPPING_MODE mapping_mode){
//---------------------------------------------------------------------------------
	st_outport.mapping_mode = mapping_mode;
}


//---------------------------------------------------------------------------------
// Outport Mapping mode
//---------------------------------------------------------------------------------
int Outport_InitOutport(void){
//---------------------------------------------------------------------------------
	pDrawLED[0] = NumLED_DrawSegLED;
	pDrawLED[1] = BinLED_DrawBinLED;
	
	return Outport_ClearOutport();
}

//---------------------------------------------------------------------------------
int Outport_ClearOutport(void){
//---------------------------------------------------------------------------------
	Outport_ClearOPortValue();
	Outport_ClearRPortValue();
	return Outport_ClearMappingMode();
}

//---------------------------------------------------------------------------------
int Outport_SwapMappingMode(void){
//---------------------------------------------------------------------------------
	if(outport_GetMappingMode())
		outport_SetMappingMode(MAPPING_DEF);
	else
		outport_SetMappingMode(MAPPING_SWAP);
	
	return (int)outport_GetMappingMode();
}

//---------------------------------------------------------------------------------
int Outport_ClearMappingMode(void){
//---------------------------------------------------------------------------------
	outport_SetMappingMode(MAPPING_DEF);
	
	return (int)outport_GetMappingMode();
}

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


//---------------------------------------------------------------------------------
// Outport to Number LED
//---------------------------------------------------------------------------------
void Outport_ShowOPortValue(u8 num_val){
//---------------------------------------------------------------------------------
	outport_SetOPortValue(num_val);
	
	(*pDrawLED[st_outport.mapping_mode])(outport_GetOPortValue());
}

//---------------------------------------------------------------------------------
void Outport_HideOPortValue(void){
//---------------------------------------------------------------------------------
	outport_SetOPortValue(0x10);
	
	(*pDrawLED[st_outport.mapping_mode])(outport_GetOPortValue());
}

//---------------------------------------------------------------------------------
void Outport_ClearOPortValue(void){
//---------------------------------------------------------------------------------
	outport_SetOPortValue(0x0F);
	
	(*pDrawLED[st_outport.mapping_mode])(outport_GetOPortValue());
}


//---------------------------------------------------------------------------------
// Outport to Binary LED
//---------------------------------------------------------------------------------
void Outport_ShowRPortValue(u8 bin_val){
//---------------------------------------------------------------------------------
	outport_SetRPortValue(bin_val);
	
	(*pDrawLED[st_outport.mapping_mode ^ 0x01])(outport_GetRPortValue());
}

//---------------------------------------------------------------------------------
void Outport_HideRPortValue(void){
//---------------------------------------------------------------------------------
	outport_SetRPortValue(0x00);
	
	(*pDrawLED[st_outport.mapping_mode ^ 0x01])(outport_GetRPortValue());
}

//---------------------------------------------------------------------------------
void Outport_ClearRPortValue(void){
//---------------------------------------------------------------------------------
	outport_SetRPortValue(0x00);
	
	(*pDrawLED[st_outport.mapping_mode ^ 0x01])(outport_GetRPortValue());
}

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


NumLED.h

/*---------------------------------------------------------------------------------
	
	NumLED module header
	
	version 0.02
	Oct 07, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _NUM_LED_H_
#define _NUM_LED_H_


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


#ifdef __cplusplus
extern "C" {
#endif


void NumLED_DrawSegLED(u8 seg_led);


#ifdef __cplusplus
}
#endif

#endif	//_NUM_LED_H_


NumLED.c

/*---------------------------------------------------------------------------------
	
	NumLED module routine
	
	version 0.02
	Oct 07, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/

#include "NumLED.h"

//AO - Ar -> Op
//CAL RSTO - Reset Op

#define NUM_LED_POSX 0
#define NUM_LED_POSY 2


//O Port -> NumberLED

//|SEG|Dp|g |f |e |d |c |b |a |
//|bit|07|06|05|04|03|02|01|00|

typedef enum{
	SEG_A  = 0x01, 
	SEG_B  = 0x02, 
	SEG_C  = 0x04, 
	SEG_D  = 0x08, 
	SEG_E  = 0x10, 
	SEG_F  = 0x20, 
	SEG_G  = 0x40, 
	SEG_DP = 0x80
}SEG_LED;


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


//---------------------------------------------------------------------------------
void NumLED_DrawSegLED(u8 seg_led){
//---------------------------------------------------------------------------------
	
	printf("\x1b[%d;%dH%d", NUM_LED_POSY, NUM_LED_POSX + 1, IS_SEG_LED(seg_led & SEG_A));
	printf("\x1b[%d;%dH%d", NUM_LED_POSY + 1, NUM_LED_POSX, IS_SEG_LED(seg_led & SEG_F));
	printf("\x1b[%d;%dH%d", NUM_LED_POSY + 1, NUM_LED_POSX + 2, IS_SEG_LED(seg_led & SEG_B));
	printf("\x1b[%d;%dH%d", NUM_LED_POSY + 2, NUM_LED_POSX + 1, IS_SEG_LED(seg_led & SEG_G));
	printf("\x1b[%d;%dH%d", NUM_LED_POSY + 3, NUM_LED_POSX, IS_SEG_LED(seg_led & SEG_E));
	printf("\x1b[%d;%dH%d", NUM_LED_POSY + 3, NUM_LED_POSX + 2, IS_SEG_LED(seg_led & SEG_C));
	printf("\x1b[%d;%dH%d", NUM_LED_POSY + 4, NUM_LED_POSX + 1, IS_SEG_LED(seg_led & SEG_D));
	printf("\x1b[%d;%dH%d", NUM_LED_POSY + 4, NUM_LED_POSX + 3, IS_SEG_LED(seg_led & SEG_DP));
	
}

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


BinLED.h

/*---------------------------------------------------------------------------------
	
	BinLED module header
	
	version 0.02
	Oct 07, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _BIN_LED_H_
#define _BIN_LED_H_


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


#ifdef __cplusplus
extern "C" {
#endif


void BinLED_DrawBinLED(u8 bin_led);


#ifdef __cplusplus
}
#endif

#endif	//_BIN_LED_H_


BinLED.c

/*---------------------------------------------------------------------------------
	
	BinLED module routine
	
	version 0.02
	Oct 07, 2009
	
	(C)2009 REGEKATSU
	
---------------------------------------------------------------------------------*/

#include "BinLED.h"


#define BIN_LED_POSX 0
#define BIN_LED_POSY 0


//R Port -> BinaryLED

//|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;


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


//---------------------------------------------------------------------------------
void BinLED_DrawBinLED(u8 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));
	
}

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