強火で進め

このブログではプログラム関連の記事を中心に書いてます。

StarBoard OrangeのLCDに文字を流してみる

A を左から右へ移動させる。

#include "mbed.h"
#include "TextLCD.h"

TextLCD lcd(p24, p26, p27, p28, p29, p30);

int main(void) {
    int x = 0;
    while(1) {
        lcd.cls();
        lcd.locate(x, 0);
        lcd.printf("A");
        wait(0.5);
        x++;
        if (x >= 16) {
            x = 0;
        }
    }
}

以下の部分は

        x++;
        if (x >= 16) {
            x = 0;
        }

こんな感じや

        x = (x + 1) % 16;

こんな感じでも書けます。

        x = (x + 1) & 0x0F;

逆に右から左に流れる様にするプログラム。

#include "mbed.h"
#include "TextLCD.h"

TextLCD lcd(p24, p26, p27, p28, p29, p30);

int main(void) {
    int x = 15;
    while(1) {
        lcd.cls();
        lcd.locate(x, 0);
        lcd.printf("A");
        wait(0.5);
        x--;
        if (x < 0) {
            x = 15;
        }
    }
}

長い文字列をテロップの様に流す。

#include "mbed.h"
#include "TextLCD.h"

Serial pc(USBTX, USBRX); // tx, rx
TextLCD lcd(p24, p26, p27, p28, p29, p30);

int main(void) {
	char src_msg[] = "Hello World! 0123456789";
	char dst_msg[17];
	char show_len = 16;
	char len;
	char *src_p = src_msg;
	while(1) {
		memset(dst_msg, 0, sizeof(dst_msg));
		len = strlen(src_p);
		pc.printf("%dn", len);
		if (len >= show_len) {
			strncpy(dst_msg, src_p, show_len);
			pc.printf("dst_msg  %s\n", dst_msg);
		} else {
			strncpy(dst_msg, src_p, len);
			pc.printf("dst_msg1 %s\n", dst_msg);
			strncpy(dst_msg+len, src_msg, show_len-len);
			pc.printf("dst_msg2 %s\n", dst_msg);
		}
		src_p++;
		if (src_p >= src_msg + strlen(src_msg))
			src_p -= strlen(src_msg);
		lcd.cls();
		lcd.printf("%s", dst_msg);
		wait(1.0);
	}
}

※このプログラムでは src_msg に設定する文字列は16文字以上で有る必要があります。短い文字列を設定したい場合は足らない文字数は空白で埋めて下さい。
(例)

char src_msg[] = "ABC              ";

久しぶりにC言語触ると結構書き方や関数名、引数などを忘れてるなぁ。と実感しました。こちらのリファレンスページにとてもお世話になりました。

C/C++ リファレンス
http://www.cppll.jp/cppreference/

今回のエントリーはC言語をまだほとんどやった事が無い人には難しい内容だったかも知れません。

もし、そのような方はC言語の入門本を1、2冊先に買って読んでおいた方が良いかも知れません。

やさしいC 第3版 [やさしいシリーズ]

やさしいC 第3版 [やさしいシリーズ]

新C言語入門 ビギナー編 (C言語実用マスターシリーズ)

新C言語入門 ビギナー編 (C言語実用マスターシリーズ)