ESP32 FreeRTOS 任务通知4(使用直接任务通知当作邮箱)

本文介绍如何在Arduino中利用直接任务通知将RGB值封装到32Bits中发送,接收端解析并控制NeoPixel灯的显示,展示了任务创建和通信过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

巧用直接任务通知的32Bits来封装打包多个数据后传递,接收到后,如何对数据进行还原,如图所示。

程序实现

#include <Adafruit_NeoPixel.h>
#define NEOPIN 33
#define NUMPIXELS 16

#define RPIN 34
#define GPIN 35
#define BPIN 32
#define SPIN 36 // speed

static TaskHandle_t xTaskNeoRing = NULL;

void configTask(void *pvParam) {
  pinMode(RPIN, INPUT);
  pinMode(GPIN, INPUT);
  pinMode(BPIN, INPUT);
  pinMode(SPIN, INPUT);
  uint8_t r_value = 0;
  uint8_t g_value = 0;
  uint8_t b_value = 0;
  uint8_t s_value = 100;

  while (1) {
    r_value = map(analogRead(RPIN), 0, 4095, 0, 255);
    g_value = map(analogRead(GPIN), 0, 4095, 0, 255);
    b_value = map(analogRead(BPIN), 0, 4095, 0, 255);
    s_value = map(analogRead(SPIN), 0, 4095, 10, 200);

    uint32_t rgb = s_value << 24 | r_value << 16 | g_value << 8 | b_value << 0;

    xTaskNotify(xTaskNeoRing, rgb, eSetValueWithOverwrite);

    vTaskDelay(100);
  }
}

void neoRing(void *pvParam) {

  Adafruit_NeoPixel pixels(NUMPIXELS, NEOPIN, NEO_GRB + NEO_KHZ800);
  pixels.begin();

  uint32_t srgb = 0;
  uint8_t r = 0;
  uint8_t g = 0;
  uint8_t b = 0;
  uint8_t s = 100;

  while (1) {

    pixels.clear();
    for (int i = 0; i < NUMPIXELS; i++) {

      xTaskNotifyWait(0x00, 0x00, &srgb, 0);
      s = (srgb & 0xff000000) >> 24;
      r = (srgb & 0x00ff0000) >> 16;
      g = (srgb & 0x0000ff00) >> 8;
      b = (srgb & 0x000000ff) >> 0;

      pixels.setPixelColor(i, pixels.Color(r, g, b));
      pixels.show();
      vTaskDelay(s * 5);
    }
  }
}

void setup() {
  Serial.begin(115200);

  xTaskCreate(configTask, "Configuration", 1024 * 10, NULL, 1, NULL);
  xTaskCreate(neoRing, "Neo Ring", 1024 * 20, NULL, 1, &xTaskNeoRing);

  vTaskDelete(NULL); //没我啥事了,自宫ba
}


void loop() {
}

运行环境

使用直接任务通知当作邮箱

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值