Edited at

1フレームの時間を測る

More than 3 years have passed since last update.


概要

ヒャッハー!約60fpsだぜ!


計測してみる

#include <Arduboy.h>

Arduboy boy;

typedef signed char s8;
typedef signed short s16;
typedef signed int s32;

typedef unsigned char u8;
typedef unsigned short u16;
//typedef unsigned int u32;

typedef signed short fx16;

s16 x = 0;
s32 cnt = 0;
unsigned long frameload = 0;
unsigned long prev_count = 0;

#define FX16_SHIFT (7)
#define FX16_ONE (1<<FX16_SHIFT)
#define FX16_CONST(v) ((s16)((v) * 128.0f))
#define FX16_TOINT(v) ((v)>>FX16_SHIFT)
#define FX16_MUL(a, b) (((a)*(b))>>FX16_SHIFT)
#define FX16_DIV(a, b) ((s16)(((s32)(a)<<FX16_SHIFT)/(b)))

struct Object {
fx16 x, y;
};

Object obj;

void update() {
if (boy.pressed(LEFT_BUTTON)) {
obj.x -= FX16_CONST(1.5f);
}
if (boy.pressed(RIGHT_BUTTON)) {
obj.x += FX16_CONST(1.5f);
}
if (boy.pressed(UP_BUTTON)) {
obj.y -= FX16_CONST(1.5f);
}
if (boy.pressed(DOWN_BUTTON)) {
obj.y += FX16_CONST(1.5f);
}
boy.drawCircle(FX16_TOINT(obj.x), FX16_TOINT(obj.y), 8, 0xff);
}

void setup() {
boy.begin();
boy.setFrameRate(60);
}

void loop() {
if (!boy.nextFrame()) return;
auto now = micros();
frameload = now - prev_count;
prev_count = now;
boy.clear();
update();
boy.display();
if (++cnt > 60) {
Serial.println(frameload);
cnt = 0;
}
}

関係ないコードも混じってますが、とりあえず、毎フレーム micros() で起動後から経過した時間を取得して、一秒に一回シリアルへ出力。

[k-ya@meteor] ~/Documents/Arduino/build

% picocom /dev/cu.usbmodem1411
picocom v2.1

port is : /dev/cu.usbmodem1411
flowcontrol : none
baudrate is : 9600
parity is : none
databits are : 8
stopbits are : 1
escape is : C-a
local echo is : no
noinit is : no
noreset is : no
nolock is : no
send_cmd is : sz -vv
receive_cmd is : rz -vv -E
imap is :
omap is :
emap is : crcrlf,delbs,

Type [C-a] [C-h] to see available commands

Terminal ready
16384
16384
16384
16384
16384
15360
15360
15360
16384
16384
16384
16384
16384
15360
15368

Thanks for using picocom

タイマー精度の関係なのか、16384というこれはこれでキリのいい数字が出ているが、大体60fps出ているようだ。