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

FIFO 通信によるマイク入力サンプル。


fifocommon ライブラリの使いかたを大雑把だが理解したので作ってみた。
今日読んで何となく理解した一夜漬けのような状態なので
もっと正しい使いかたがあるのかも知れんけど(汗)。
つかこれ説明が難しい。ソース晒して逃げとこう。


マイク入力関数自体は ARM7 用に既に用意されているのだが
サウンドライブラリにある録音関数にしか使われておらず
リアルタイムで入力データを見る手段が無かったため
今回下記コードを記述してみた次第。


これでバンゲリングベイよろしく「ハドソンー!」が実装できる。<そこか


microphone get current value sample


プロジェクト一式。
ttp://page.freett.com/ntr/example/mic.zip


arm9/source/main.c

/*---------------------------------------------------------------------------------
	
	microphone get current value sample
	arm9 side
	
	version 0.01
	Mar 16, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/
#include <nds.h>
#include <stdio.h>
#include "mic.h"

//---------------------------------------------------------------------------------
int main(void) {
//---------------------------------------------------------------------------------
	
	u8 mic = 0;
	
	consoleDemoInit();
	printf("microphone\nget current value sample\n\nversion 0.01\nMar 16, 2010\n\nBy REGEKATSU\n\n");
	
	while(1) {
		
		swiWaitForVBlank();
		
		scanKeys();
		
		if(keysDown() & KEY_A){
			micEnable();
			printf("\x01b[10;0HMIC ON ");
		}else if(keysDown() & KEY_B){
			micDisable();
			printf("\x01b[10;0HMIC OFF");
		}
		
		mic = micGetValue8();
		printf("\x01b[12;0Hmic_cur = %03d", mic);
		
	}
	
}
//---------------------------------------------------------------------------------


arm9/source/micdef.h

/*---------------------------------------------------------------------------------
	
	mic module define header
	
	version 0.01
	Mar 16, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _MIC_DEF_H_
#define _MIC_DEF_H_


typedef enum {
	MIC_ENABLE, 
	MIC_DISABLE, 
	MIC_GETVAL8, 
	MIC_GETVAL12
} FifoMicCommands;


#ifdef __cplusplus
extern "C" {
#endif


#ifdef __cplusplus
}
#endif

#endif	// _MIC_DEF_H_


arm9/source/mic.h

/*---------------------------------------------------------------------------------
	
	mic arm9 module header
	
	version 0.01
	Mar 16, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _MIC_H_
#define _MIC_H_


#ifdef __cplusplus
extern "C" {
#endif


void micEnable(void);
void micDisable(void);
u8 micGetValue8(void);
u16 micGetValue12(void);


#ifdef __cplusplus
}
#endif

#endif	// _MIC_H_


arm9/source/mic.c

/*---------------------------------------------------------------------------------
	
	mic arm9 module routine
	
	version 0.01
	Mar 16, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <nds/ndstypes.h>
#include <nds/fifocommon.h>
#include "micdef.h"
#include "mic.h"


//---------------------------------------------------------------------------------
void micEnable(void){
//---------------------------------------------------------------------------------
	fifoSendValue32(FIFO_USER_01, MIC_ENABLE);
}

//---------------------------------------------------------------------------------
void micDisable(void){
//---------------------------------------------------------------------------------
	fifoSendValue32(FIFO_USER_01, MIC_DISABLE);
}

//---------------------------------------------------------------------------------
u8 micGetValue8(void){
//---------------------------------------------------------------------------------
	fifoSendValue32(FIFO_USER_01, MIC_GETVAL8);
	
	while(!fifoCheckValue32(FIFO_USER_01));
	
	return (u8)fifoGetValue32(FIFO_USER_01);
}

//---------------------------------------------------------------------------------
u16 micGetValue12(void){
//---------------------------------------------------------------------------------
	fifoSendValue32(FIFO_USER_01, MIC_GETVAL12);
	
	while(!fifoCheckValue32(FIFO_USER_01));
	
	return (u16)fifoGetValue32(FIFO_USER_01);
}

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


arm7/source/main7.c

/*---------------------------------------------------------------------------------
	
	microphone get current value sample
	arm7 side
	
	version 0.01
	Mar 16, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/
#include <nds.h>
#include <dswifi7.h>
#include <maxmod7.h>
#include "mic7.h"

//---------------------------------------------------------------------------------
void VcountHandler() {
//---------------------------------------------------------------------------------
	inputGetAndSend();
}

//---------------------------------------------------------------------------------
void VblankHandler(void) {
//---------------------------------------------------------------------------------
	Wifi_Update();
}

//---------------------------------------------------------------------------------
int main() {
//---------------------------------------------------------------------------------
	
	irqInit();
	fifoInit();

	// read User Settings from firmware
	readUserSettings();

	// Start the RTC tracking IRQ
	initClockIRQ();

	SetYtrigger(80);

	installWifiFIFO();
	installSoundFIFO();

	mmInstall(FIFO_MAXMOD);

	installSystemFIFO();
	
	//init my fifo channel
	installMicFIFO();
	
	irqSet(IRQ_VCOUNT, VcountHandler);
	irqSet(IRQ_VBLANK, VblankHandler);

	irqEnable( IRQ_VBLANK | IRQ_VCOUNT | IRQ_NETWORK);   

	// Keep the ARM7 mostly idle
	while (1) swiWaitForVBlank();
}

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


arm7/source/mic7.h

/*---------------------------------------------------------------------------------
	
	mic arm7 module header
	
	version 0.01
	Mar 16, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/

#ifndef _MIC7_H_
#define _MIC7_H_


#ifdef __cplusplus
extern "C" {
#endif


void installMicFIFO(void);


#ifdef __cplusplus
}
#endif

#endif	// _MIC7_H_


arm7/source/mic7.c

/*---------------------------------------------------------------------------------
	
	mic arm7 module routine
	
	version 0.01
	Mar 16, 2010
	
	By REGEKATSU
	
---------------------------------------------------------------------------------*/

#include <nds/fifocommon.h>
#include <nds/arm7/audio.h>
#include "../../arm9/source/micdef.h"
#include "mic7.h"


void micCommandHandler(u32 value, void *data);


//---------------------------------------------------------------------------------
void installMicFIFO(void){
//---------------------------------------------------------------------------------
	
	fifoSetValue32Handler(FIFO_USER_01, micCommandHandler, 0);
	
}

//---------------------------------------------------------------------------------
void micCommandHandler(u32 value, void *data){
//---------------------------------------------------------------------------------
	
	switch(value){
		
	case MIC_ENABLE:
		micOn();
		break;
		
	case MIC_DISABLE:
		micOff();
		break;
		
	case MIC_GETVAL8:
		fifoSendValue32(FIFO_USER_01, (u32)micReadData8());
		break;
		
	case MIC_GETVAL12:
		fifoSendValue32(FIFO_USER_01, (u32)micReadData12());
		break;
		
	default:
		break;
		
	}
	
}

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