请用c语言重写这段代码from machine import Pin,Timer,SPI import time hspi = SPI(1, 2500000, sck=Pin(14), mosi=Pin(13), miso=Pin(12), polarity=0) def byte2bin(b): bstr = bin(b)[2:] return '0'(8-len(bstr)) + bstr def rgb2byte(r,g,b): str = byte2bin(g) + byte2bin(r) + byte2bin(b) spistr = ''.join([(lambda s: '011' if s'0' else '001')(x) for x in str]) rgbdim = [int(spistr[i8:i8+8], 2) for i in range(9)] return bytes(rgbdim) rgbbyte = rgb2byte(0xff,0x80,0x3f) print(rgbbyte) rstbyte = bytes([0xff]*16) outbyte = rstbyte+rgbbyte while True: hspi.write(outbyte) time.sleep_ms(10)
时间: 2024-03-04 15:51:22 浏览: 100
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
#include "hardware/gpio.h"
#define SCK_PIN 14
#define MOSI_PIN 13
#define MISO_PIN 12
void byte2bin(uint8_t b, char* result) {
char* bstr = itoa(b, result, 2);
int len = strlen(bstr);
for (int i = 0; i < 8 - len; i++) {
result[i] = '0';
}
result[8 - len] = '\0';
}
void rgb2byte(uint8_t r, uint8_t g, uint8_t b, uint8_t* rgbdim) {
char str[25];
char spistr[25];
char result[9];
byte2bin(g, result);
strcpy(str, result);
byte2bin(r, result);
strcat(str, result);
byte2bin(b, result);
strcat(str, result);
for (int i = 0; i < 24; i++) {
spistr[i] = (str[i] == '0') ? '0' : '1';
}
spistr[24] = '\0';
for (int i = 0; i < 9; i++) {
rgbdim[i] = strtol(spistr + i * 8, NULL, 2);
}
}
int main() {
stdio_init_all();
spi_init(spi1, 2500000);
gpio_set_function(SCK_PIN, GPIO_FUNC_SPI);
gpio_set_function(MOSI_PIN, GPIO_FUNC_SPI);
gpio_set_function(MISO_PIN, GPIO_FUNC_SPI);
uint8_t rgbbyte[9];
rgb2byte(0xff, 0x80, 0x3f, rgbbyte);
uint8_t rstbyte[16];
memset(rstbyte, 0xff, sizeof(rstbyte));
uint8_t outbyte[25];
memcpy(outbyte, rstbyte, sizeof(rstbyte));
memcpy(outbyte + sizeof(rstbyte), rgbbyte, sizeof(rgbbyte));
while (true) {
spi_write_blocking(spi1, outbyte, sizeof(outbyte));
sleep_ms(10);
}
return 0;
}
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)