入門から習熟までのIoT Loraモジュール(2) LED電球の点滅と呼吸ライト

目次

I.はじめに

2. 練習とコーディング

        1. 電気的明るい LED1

        2. LED1をオフにする

        3. LEDレベルを反転します

        4. LED1とLED2が交互に点滅

        5. LED1呼吸ライト

3. プログラムコード


I.はじめに

        この記事の内容は、新世界のモノのインターネットの Lora モジュールの開発に属しており、指定された Lora 基本ルーチンを使用し、それに基づいて開発を完了し、他のバージョンの Lora を学習するためのアイデアを提供できます。

2. 練習とコーディング

        1. 電気的明るい LED1

        

        2. LED1をオフにする

 

        3. LEDレベルを反転します

        4. LED1とLED2が交互に点滅

        それが提供する led_light.c ファイルでは、点滅の方法が定義されています。

#define HELF_SECOND 50
uint16_t helfSecondCnt = 0;
bool isLed1Lighted = true;
bool isHelfSecondLedBlinkEnable = false;
void blinkPerHelfSecond() {
    if (!isHelfSecondLedBlinkEnable)
        return;

    helfSecondCnt ++;
    if (helfSecondCnt > HELF_SECOND) {
        helfSecondCnt = 0;
        isLed1Lighted = !isLed1Lighted;
    }

    if (isLed1Lighted) {
        GpioWrite( &Led1, 0 );
        GpioWrite( &Led2, 1 );
    }
    else {
        GpioWrite( &Led1, 1 );
        GpioWrite( &Led2, 0 );
    }
}

void startLedBlink() {
    isHelfSecondLedBlinkEnable = true;
    GpioWrite( &Led1, 1 );
    GpioWrite( &Led2, 1 );
}

void stopLedBlink() {
    isHelfSecondLedBlinkEnable = false;
    GpioWrite( &Led1, 1 );
    GpioWrite( &Led2, 1 );
}

        上記のコード分析を通じて、点滅を有効にしたい場合は、最初に startLedBlink() 関数を呼び出す必要があり、次に、点滅効果を実現するために main 関数で blinkPerHelfSecond() を呼び出し続ける必要があることがわかります。

        コードから、blinkPerHelfSecond() 関数が 50 回呼び出されると LED のレベルが反転することがわかります。そのため、固定時間の点滅を完了するには呼び出し間隔を制御するだけで済みます。

        上記のコードは、blinkPerHelfSecond() 関数を 10 ミリ秒ごとに呼び出すことで、0.5 秒ごとに交互に点滅する効果を実現します。

        5. LED1呼吸ライト

         また、led_light.c では、呼吸光に関するメソッドも定義されています。

#define LED_ILLUMINATION_LEVEL 8
#define LED_LEVEL_TIMER				LED_ILLUMINATION_LEVEL/2
#define DEVIDE_VALUE 16

uint8_t levelCount = LED_ILLUMINATION_LEVEL;
uint8_t led_levet_timer = LED_LEVEL_TIMER;
uint8_t devideCount = DEVIDE_VALUE;
uint8_t level = 1;
uint8_t timeTic = 0;
bool isUprise = true;
bool isLed1BreathEnable = false;
bool isLed2BreathEnable = false;

void setBreathLedArg(uint8_t levelCnt, uint8_t levelUpDevide){
	levelCount = levelCnt;
	led_levet_timer = levelCount/2;
	devideCount = levelUpDevide;
}

void resetLedPwm() {
    timeTic = 0;
    if (isLed1BreathEnable)
        GpioWrite( &Led1, 0 );
    if (isLed2BreathEnable)
        GpioWrite( &Led2, 0 );
}

void pwmLevelUp() {
    static unsigned long led_level_tick = 0;
    led_level_tick++;
    if (led_level_tick > led_levet_timer) {
        led_level_tick = 0;
        if (isUprise)
            level++;
        else
            level--;
        if (level > levelCount) {
            level = levelCount;
            isUprise = false;
        } else if (level == 0) {
            isUprise = true;
        }
    }
}

void pwm() {
    timeTic++;
    if (timeTic > level) {
        if (isLed1BreathEnable)
            GpioWrite( &Led1, 1 );
        if (isLed2BreathEnable)
            GpioWrite( &Led2, 1 );
    }
    if (timeTic > levelCount) {
        resetLedPwm();
    }
}

uint8_t timeDevice = 0;
void breathLed() {
    timeDevice++;
    if (timeDevice >= devideCount) {
        timeDevice = 0;
        pwmLevelUp();
    }
    pwm();
}

void switchLed1Breath() {
    isLed1BreathEnable = !isLed1BreathEnable;
}

void switchLed2Breath() {
    isLed2BreathEnable = !isLed2BreathEnable;
}

void startLedBreath() {
    isLed1BreathEnable = true;
    isLed2BreathEnable = true;
    GpioWrite( &Led1, 1 );
    GpioWrite( &Led2, 1 );
}

void stopLedBreath() {
    isLed1BreathEnable = false;
    isLed2BreathEnable = false;
    GpioWrite( &Led1, 1 );
    GpioWrite( &Led2, 1 );
}

        上記のコードによると、setBreathLedArg() 関数が初期化に使用されていることがわかります。最初のパラメータは明るさの階調 (推奨値は 100)、2 番目のパラメータは明るさレベルを変更するための呼び出し回数です。次に、startLedBreath() を使用して軽い呼吸を開始し、メイン プログラムで常に BreathLed() 関数を呼び出すだけです。switchLed1Breath 関数は startLedBreath() と同様に使用でき、特定の呼吸ライトの呼吸フラグを独立してオンにできます。

3. プログラムコード

/**
 * Main application entry point.
 */
int main( void )//任务4
{
    Init();
		startLedBlink();
    while( 1 )
    {
			HAL_Delay(10);
			blinkPerHelfSecond();
    }
}

int main( void )//任务5
{
    Init();
		setBreathLedArg(100,50);
		switchLed1Breath();
    while( 1 )
    {
			breathLed();
    }
}

おすすめ

転載: blog.csdn.net/qq_39724355/article/details/131142399