0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

nRF Connect SDK:Softblinkを実装してみる

Posted at

昔も作った

以前にも似たような内容でSoftblinkっぽいものを作ったことがあります。

もう3年くらい前なんですね…。そんなに長い間、このSDKと戯れているんですね(笑)。いつになったらまともにうごk…あ、誰か来たようだ…。

そんなに筋は悪くないかも

改めて見てみましたがそこまで筋が悪いサンプルではなさそうです。ただ、タイマースレッド使っていてその他のスレッドへの影響もありそうなので、もう少しそれっぽく作ってみることにしました。

タイマースレッドはシステムキューに入るので、システムキューに入るその他のスレッドへ影響を及ぼします

3年の間にずいぶんと変わった

SDKも変わりましたが、自分自身のRTOS(Zephyr)への理解度も相当上がっています。そりゃ3年も戯れて何も変わっていなかったらそれはそれで問題ですわwww

main.c
#include <zephyr/kernel.h>
#include <dk_buttons_and_leds.h>
#include <zephyr/device.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/logging/log.h>

LOG_MODULE_REGISTER(main, LOG_LEVEL_INF);

#define PWM_PERIOD	1000 * 1000 * 10				// 10msec
#define RATE_MIN	-200
#define RATE_MAX	100
static int16_t rate = 1;

static const struct pwm_dt_spec led_pwm0 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0));

static void led_pwm_work_thread(void *p1, void *p2, void *p3)
{
	int16_t dimming = 0;
	int16_t dimming_temp;

	while (true) {
		if (dimming <= 0) {
			dimming_temp = 0;
		} else if (100 <= dimming) {
			dimming_temp = 100;
		} else {
			dimming_temp = dimming;
		}
		pwm_set_dt(&led_pwm0, PWM_PERIOD, (PWM_PERIOD * dimming_temp / 100U));
		LOG_INF("PWM Percentage: %d%%", dimming);
		if ((dimming <= RATE_MIN) || (RATE_MAX <= dimming)) {
			rate = -rate;
		}
		dimming += rate;
		k_sleep(K_MSEC(10));
	}
}
K_THREAD_DEFINE(led_pwm, 512, led_pwm_work_thread, NULL, NULL, NULL, K_PRIO_PREEMPT(0), 0, 0);

int main(void)
{
	return 0;
}
prj.conf
CONFIG_PWM=y
CONFIG_LOG=y

以前のサンプルから比べたらずいぶんとすっきりしました。ただ、nRF SDK時代のようにAPI一つで勝手に光ってくれるようなものは未だ用意されていません。きっと今後も用意されることはないんじゃないかなと思います。

たぶんRTOSになった関係でそういうAPIは作れないんじゃないかな…

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?