読者です 読者をやめる 読者になる 読者になる

ハートに16連射。(9)


スプライト描画モジュールの暫定版を作ってたり。
復刻版の挙動を参考にしてるのでスーパーやプロとは若しかしたら違うかも。
時計、ストップウォッチ、連打計測、サイコロゲーム、スロットゲーム、
それから「いい加減時計」の”コロ”表示まで考えるとセグをビットで扱うのが無難かなと。
あとゼロサプレスの有無だったり、任意のセグを非表示とか、そういった部分を
int 型(10進6桁)1つで扱うのは無理なので。


んで、折角だから下記の内蔵時計サンプルを関数化して
内蔵時計表示までさせましたよと。


スプライト初期化処理、アップデート処理と、
printf 部分をまんまスプライトに定義を挿げ替えればこのモジュールは出来上がり。のハズ。


shtwatch
spr module test


main.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	spr module test
	
	version 0.01
	Jan 30, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <time.h>
#include <nds/ndstypes.h>
#include <nds/interrupts.h>
#include <nds/arm9/input.h>
#include <nds/arm9/console.h>
#include "Spr.h"


struct tm *getRtc(void);
void drawRtc(const struct tm *tm);


//---------------------------------------------------------------------------------
struct tm* getRtc(void){
//---------------------------------------------------------------------------------
	
	time_t unix_time;
	
	unix_time = time(NULL);
	
	return gmtime((const time_t *)&unix_time);
	
}

//---------------------------------------------------------------------------------
void drawRtc(const struct tm *tm){
//---------------------------------------------------------------------------------
	
	printf("\x1b[9;0H");
	printf("Time   %2i:%02i:%02i", tm->tm_hour, tm->tm_min, tm->tm_sec);
	
}

//---------------------------------------------------------------------------------
int main(void){
//---------------------------------------------------------------------------------
	
	struct tm tm_now, tm_old;
	
	/*
	struct tm{
		int tm_sec;     // 秒 (0〜59)
		int tm_min;     // 分 (0〜59)
		int tm_hour;    // 時 (0〜23)
		int tm_mday;    // 日 (1〜31)
		int tm_mon;     // 月 (0〜11)
		int tm_year;    // 年 (年 - 1900)
		int tm_wday;    // 週 (0〜6 : 日曜日〜土曜日)
		int tm_yday;    // 年内の経過日数 (0〜365)
		int tm_isdst;   // 夏時間 : 0以外の値
	};
	*/
	
	u8 sel_cursor = 0;
	
	
	consoleDemoInit();
	printf("shtwatch\nspr module test\n\nversion 0.01\nJan 30, 2010\n\nBy. REGEKATSU\n\n\n");
	
	Spr_Init();
	Spr_SetColon(3);
	Spr_SetCursor(BIT(sel_cursor));
	
	while(1) {
		
		swiWaitForVBlank();
		scanKeys();
		
		Spr_Update();
		
		tm_old = tm_now;
		tm_now = *getRtc();
		
		if(tm_old.tm_sec == tm_now.tm_sec){
			
			drawRtc(&tm_now);
			
			Spr_SetNumber(tm_now.tm_sec, SEG_SECOND, true);
			Spr_SetNumber(tm_now.tm_min, SEG_MINUTE, true);
			Spr_SetNumber(tm_now.tm_hour, SEG_HOUR, false);
			
		}
		
		if(keysDown() & KEY_A){
			
			if(sel_cursor < 4){
				sel_cursor++;
			}else{
				sel_cursor = 0;
			}
			
			Spr_SetCursor(BIT(sel_cursor));
			
		}
		
	}
	
}

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


Spr.h

/*---------------------------------------------------------------------------------
	
	shtwatch
	spr module header
	
	version 0.01
	Jan 30, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _SPR_H_
#define _SPR_H_


typedef enum{
	SEG_SECOND, 
	SEG_MINUTE, 
	SEG_HOUR
}SEG_SELECT;


#ifdef __cplusplus
extern "C" {
#endif


void Spr_Init(void);
void Spr_Update(void);
void Spr_SetColon(u8 colon);
void Spr_SetCursor(u8 cursor);
void Spr_SetNumber(int number, SEG_SELECT seg_select, bool zero_suppress);
void Spr_SetSegment(u8 seg_low, u8 seg_high, SEG_SELECT seg_select);


#ifdef __cplusplus
}
#endif

#endif	//_SPR_H_


Spr.c

/*---------------------------------------------------------------------------------
	
	shtwatch
	spr module header
	
	version 0.01
	Jan 30, 2010
	
	By. REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <stdio.h>
#include <string.h>
#include <nds/ndstypes.h>
#include "Spr.h"


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

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


typedef struct{
	u8 colon;
	u8 cursor;
	u8 seg_bit[6];
}SprStatus;

static SprStatus st_spr;


void spr_DrawColon(void);
void spr_DrawCursor(void);
void spr_DrawSegment(void);


//---------------------------------------------------------------------------------
void Spr_Init(void){
//---------------------------------------------------------------------------------
	
	memset(&st_spr, 0, sizeof(st_spr));
	
}

//---------------------------------------------------------------------------------
void Spr_Update(void){
//---------------------------------------------------------------------------------
	
	spr_DrawSegment();
	spr_DrawColon();
	spr_DrawCursor();
	
}

//---------------------------------------------------------------------------------
void Spr_SetColon(u8 colon){
//---------------------------------------------------------------------------------
	
	st_spr.colon = colon & 0x03;
	
}

//---------------------------------------------------------------------------------
void Spr_SetCursor(u8 cursor){
//---------------------------------------------------------------------------------
	
	st_spr.cursor = cursor & 0x1f;
	
}

//---------------------------------------------------------------------------------
void Spr_SetNumber(int number, SEG_SELECT seg_select, bool zero_suppress){
//---------------------------------------------------------------------------------
	
	//7 Seg LED number pattern
	const static u8 seg_bit[] = {
		0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 
		0x00
	};
	
	int temp;
	u8 low, high;
	
	
	//round to value
	if(number > 99){
		temp = 99;
	}else if(number < 0){
		temp = 0;
	}else{
		temp = number;
	}
	
	//rank division
	low  = temp % 10;
	high = temp / 10;
	
	//zero suppress
	if(high == 0 && zero_suppress == false){
		high = 10;
	}
	
	//set segment bits
	Spr_SetSegment(seg_bit[low], seg_bit[high], seg_select);
	
}

//---------------------------------------------------------------------------------
void Spr_SetSegment(u8 seg_low, u8 seg_high, SEG_SELECT seg_select){
//---------------------------------------------------------------------------------
	
	switch(seg_select){
		
	case SEG_SECOND:
		st_spr.seg_bit[0] = seg_low;
		st_spr.seg_bit[1] = seg_high;
		break;
		
	case SEG_MINUTE:
		st_spr.seg_bit[2] = seg_low;
		st_spr.seg_bit[3] = seg_high;
		break;
		
	case SEG_HOUR:
		st_spr.seg_bit[4] = seg_low;
		st_spr.seg_bit[5] = seg_high;
		break;
		
	}
	
}

//---------------------------------------------------------------------------------
void spr_DrawColon(void){
//---------------------------------------------------------------------------------
	
	printf("\x1b[%d;%dH%d", 13, 9, IS_VAL(st_spr.colon & 0x01));
	printf("\x1b[%d;%dH%d", 15, 9, IS_VAL(st_spr.colon & 0x02));
	
}

//---------------------------------------------------------------------------------
void spr_DrawCursor(void){
//---------------------------------------------------------------------------------
	
	printf("\x1b[%d;%dH%d", 20, 1, IS_VAL(st_spr.cursor & 0x01));
	printf("\x1b[%d;%dH%d", 20, 3, IS_VAL(st_spr.cursor & 0x02));
	printf("\x1b[%d;%dH%d", 20, 5, IS_VAL(st_spr.cursor & 0x04));
	printf("\x1b[%d;%dH%d", 20, 7, IS_VAL(st_spr.cursor & 0x08));
	printf("\x1b[%d;%dH%d", 20, 9, IS_VAL(st_spr.cursor & 0x10));
	
}

//---------------------------------------------------------------------------------
void spr_DrawSegment(void){
//---------------------------------------------------------------------------------
	
	//hour high
	printf("\x1b[%d;%dH%d", 12, 2, IS_VAL(st_spr.seg_bit[5] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1, IS_VAL(st_spr.seg_bit[5] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3, IS_VAL(st_spr.seg_bit[5] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2, IS_VAL(st_spr.seg_bit[5] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1, IS_VAL(st_spr.seg_bit[5] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3, IS_VAL(st_spr.seg_bit[5] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2, IS_VAL(st_spr.seg_bit[5] & SEG_D));
	
	//hour low
	printf("\x1b[%d;%dH%d", 12, 2 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 4, IS_VAL(st_spr.seg_bit[4] & SEG_D));
	
	
	//minute high
	printf("\x1b[%d;%dH%d", 12, 2 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 10, IS_VAL(st_spr.seg_bit[3] & SEG_D));
	
	//minute low
	printf("\x1b[%d;%dH%d", 12, 2 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 14, IS_VAL(st_spr.seg_bit[2] & SEG_D));
	
	
	//second high
	printf("\x1b[%d;%dH%d", 12, 2 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 19, IS_VAL(st_spr.seg_bit[1] & SEG_D));
	
	//second low
	printf("\x1b[%d;%dH%d", 12, 2 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_A));
	printf("\x1b[%d;%dH%d", 13, 1 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_F));
	printf("\x1b[%d;%dH%d", 13, 3 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_B));
	printf("\x1b[%d;%dH%d", 14, 2 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_G));
	printf("\x1b[%d;%dH%d", 15, 1 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_E));
	printf("\x1b[%d;%dH%d", 15, 3 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_C));
	printf("\x1b[%d;%dH%d", 16, 2 + 23, IS_VAL(st_spr.seg_bit[0] & SEG_D));
	
}

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