开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

sam/circuitpython

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (28)
标签 (152)
master
stm32f103
main
5.3.x
type_hints
4.x
2.x
3.x
audiocore-rawsample-sinewave
5.1.x
5.0.x
4.1.x
4.0.x
azure-pipelines
azure
dm-mixer
stable
1.x
travis-esp8266
esp-extra-scripts
5.3.1
6.0.0-alpha.1
6.0.0-alpha.0
5.4.0-beta.1
5.4.0-beta.0
5.3.0
5.3.0-rc.0
5.2.0
5.1.0
5.1.0-rc.0
5.0.0
5.0.0-rc.1
5.0.0-rc.0
5.0.0-beta.5
5.0.0-beta.4
5.0.0-beta.3
5.0.0-beta.2
4.1.2
5.0.0-beta.1
5.0.0-beta.0
master
分支 (28)
标签 (152)
master
stm32f103
main
5.3.x
type_hints
4.x
2.x
3.x
audiocore-rawsample-sinewave
5.1.x
5.0.x
4.1.x
4.0.x
azure-pipelines
azure
dm-mixer
stable
1.x
travis-esp8266
esp-extra-scripts
5.3.1
6.0.0-alpha.1
6.0.0-alpha.0
5.4.0-beta.1
5.4.0-beta.0
5.3.0
5.3.0-rc.0
5.2.0
5.1.0
5.1.0-rc.0
5.0.0
5.0.0-rc.1
5.0.0-rc.0
5.0.0-beta.5
5.0.0-beta.4
5.0.0-beta.3
5.0.0-beta.2
4.1.2
5.0.0-beta.1
5.0.0-beta.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (28)
标签 (152)
master
stm32f103
main
5.3.x
type_hints
4.x
2.x
3.x
audiocore-rawsample-sinewave
5.1.x
5.0.x
4.1.x
4.0.x
azure-pipelines
azure
dm-mixer
stable
1.x
travis-esp8266
esp-extra-scripts
5.3.1
6.0.0-alpha.1
6.0.0-alpha.0
5.4.0-beta.1
5.4.0-beta.0
5.3.0
5.3.0-rc.0
5.2.0
5.1.0
5.1.0-rc.0
5.0.0
5.0.0-rc.1
5.0.0-rc.0
5.0.0-beta.5
5.0.0-beta.4
5.0.0-beta.3
5.0.0-beta.2
4.1.2
5.0.0-beta.1
5.0.0-beta.0
PixelBuf.c 11.25 KB
一键复制 编辑 原始数据 按行查看 历史
Roy Hooper 提交于 2020年05月25日 08:35 +08:00 . Allow setting RGBW pixels with RGB tuples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
/*
* This file is part of the Circuit Python project, https://github.com/adafruit/circuitpython
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Roy Hooper
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/obj.h"
#include "py/objstr.h"
#include "py/objtype.h"
#include "py/runtime.h"
#include "shared-bindings/_pixelbuf/PixelBuf.h"
#include <string.h>
// Helper to ensure we have the native super class instead of a subclass.
static pixelbuf_pixelbuf_obj_t* native_pixelbuf(mp_obj_t pixelbuf_obj) {
mp_obj_t native_pixelbuf = mp_instance_cast_to_native_base(pixelbuf_obj, &pixelbuf_pixelbuf_type);
mp_obj_assert_native_inited(native_pixelbuf);
return MP_OBJ_TO_PTR(native_pixelbuf);
}
void common_hal__pixelbuf_pixelbuf_construct(pixelbuf_pixelbuf_obj_t *self, size_t n,
pixelbuf_byteorder_details_t* byteorder, mp_float_t brightness, bool auto_write,
uint8_t* header, size_t header_len, uint8_t* trailer, size_t trailer_len) {
self->pixel_count = n;
self->byteorder = *byteorder; // Copied because we modify for dotstar
self->bytes_per_pixel = byteorder->is_dotstar ? 4 : byteorder->bpp;
self->auto_write = false;
size_t pixel_len = self->pixel_count * self->bytes_per_pixel;
self->transmit_buffer_obj = mp_obj_new_bytes_of_zeros(header_len + pixel_len + trailer_len);
mp_obj_str_t *o = MP_OBJ_TO_PTR(self->transmit_buffer_obj);
// Abuse the bytes object a bit by mutating it's data by dropping the const. If the user's
// Python code holds onto it, they'll find out that it changes. At least this way it isn't
// mutable by the code itself.
uint8_t* transmit_buffer = (uint8_t*) o->data;
memcpy(transmit_buffer, header, header_len);
memcpy(transmit_buffer + header_len + pixel_len, trailer, trailer_len);
self->post_brightness_buffer = transmit_buffer + header_len;
if (self->byteorder.is_dotstar) {
// Initialize the buffer with the dotstar start bytes.
// Note: Header and end must be setup by caller
for (uint i = 0; i < self->pixel_count * 4; i += 4) {
self->post_brightness_buffer[i] = DOTSTAR_LED_START_FULL_BRIGHT;
}
}
// Call set_brightness so that it can allocate a second buffer if needed.
self->brightness = 1.0;
common_hal__pixelbuf_pixelbuf_set_brightness(MP_OBJ_FROM_PTR(self), brightness);
// Turn on auto_write. We don't want to do it with the above brightness call.
self->auto_write = auto_write;
}
size_t common_hal__pixelbuf_pixelbuf_get_len(mp_obj_t self_in) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
return self->pixel_count;
}
uint8_t common_hal__pixelbuf_pixelbuf_get_bpp(mp_obj_t self_in) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
return self->byteorder.bpp;
}
mp_obj_t common_hal__pixelbuf_pixelbuf_get_byteorder_string(mp_obj_t self_in) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
return self->byteorder.order_string;
}
bool common_hal__pixelbuf_pixelbuf_get_auto_write(mp_obj_t self_in) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
return self->auto_write;
}
void common_hal__pixelbuf_pixelbuf_set_auto_write(mp_obj_t self_in, bool auto_write) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
self->auto_write = auto_write;
}
mp_float_t common_hal__pixelbuf_pixelbuf_get_brightness(mp_obj_t self_in) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
return self->brightness;
}
void common_hal__pixelbuf_pixelbuf_set_brightness(mp_obj_t self_in, mp_float_t brightness) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
// Skip out if the brightness is already set. The default of self->brightness is 1.0. So, this
// also prevents the pre_brightness_buffer allocation when brightness is set to 1.0 again.
mp_float_t change = brightness - self->brightness;
if (-0.001 < change && change < 0.001) {
return;
}
self->brightness = brightness;
size_t pixel_len = self->pixel_count * self->bytes_per_pixel;
if (self->pre_brightness_buffer == NULL) {
self->pre_brightness_buffer = m_malloc(pixel_len, false);
memcpy(self->pre_brightness_buffer, self->post_brightness_buffer, pixel_len);
}
for (size_t i = 0; i < pixel_len; i++) {
// Don't adjust per-pixel luminance bytes in dotstar mode
if (self->byteorder.is_dotstar && i % 4 == 0) {
continue;
}
self->post_brightness_buffer[i] = self->pre_brightness_buffer[i] * self->brightness;
}
if (self->auto_write) {
common_hal__pixelbuf_pixelbuf_show(self_in);
}
}
void _pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t* self, mp_obj_t color, uint8_t* r, uint8_t* g, uint8_t* b, uint8_t* w) {
pixelbuf_byteorder_details_t *byteorder = &self->byteorder;
// w is shared between white in NeoPixels and brightness in dotstars (so that DotStars can have
// per-pixel brightness). Set the defaults here in case it isn't set below.
if (byteorder->is_dotstar) {
*w = 255;
} else {
*w = 0;
}
if (MP_OBJ_IS_INT(color)) {
mp_int_t value = mp_obj_get_int_truncated(color);
*r = value >> 16 & 0xff;
*g = (value >> 8) & 0xff;
*b = value & 0xff;
} else {
mp_obj_t *items;
size_t len;
mp_obj_get_array(color, &len, &items);
if (len < 3 || len > 4) {
mp_raise_ValueError_varg(translate("Expected tuple of length %d, got %d"), byteorder->bpp, len);
}
*r = mp_obj_get_int_truncated(items[PIXEL_R]);
*g = mp_obj_get_int_truncated(items[PIXEL_G]);
*b = mp_obj_get_int_truncated(items[PIXEL_B]);
if (len > 3) {
if (mp_obj_is_float(items[PIXEL_W])) {
*w = 255 * mp_obj_get_float(items[PIXEL_W]);
} else {
*w = mp_obj_get_int_truncated(items[PIXEL_W]);
}
return;
}
}
// Int colors can't set white directly so convert to white when all components are equal.
// Also handles RGBW values assigned an RGB tuple.
if (!byteorder->is_dotstar && byteorder->bpp == 4 && byteorder->has_white && *r == *g && *r == *b) {
*w = *r;
*r = 0;
*g = 0;
*b = 0;
}
}
void _pixelbuf_set_pixel_color(pixelbuf_pixelbuf_obj_t* self, size_t index, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
// DotStars don't have white, instead they have 5 bit brightness so pack it into w. Shift right
// by three to leave the top five bits.
if (self->bytes_per_pixel == 4 && self->byteorder.is_dotstar) {
w = DOTSTAR_LED_START | w >> 3;
}
pixelbuf_rgbw_t *rgbw_order = &self->byteorder.byteorder;
size_t offset = index * self->bytes_per_pixel;
if (self->pre_brightness_buffer != NULL) {
uint8_t* pre_brightness_buffer = self->pre_brightness_buffer + offset;
if (self->bytes_per_pixel == 4) {
pre_brightness_buffer[rgbw_order->w] = w;
}
pre_brightness_buffer[rgbw_order->r] = r;
pre_brightness_buffer[rgbw_order->g] = g;
pre_brightness_buffer[rgbw_order->b] = b;
}
uint8_t* post_brightness_buffer = self->post_brightness_buffer + offset;
if (self->bytes_per_pixel == 4) {
// Only apply brightness if w is actually white (aka not DotStar.)
if (!self->byteorder.is_dotstar) {
w *= self->brightness;
}
post_brightness_buffer[rgbw_order->w] = w;
}
post_brightness_buffer[rgbw_order->r] = r * self->brightness;
post_brightness_buffer[rgbw_order->g] = g * self->brightness;
post_brightness_buffer[rgbw_order->b] = b * self->brightness;
}
void _pixelbuf_set_pixel(pixelbuf_pixelbuf_obj_t* self, size_t index, mp_obj_t value) {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t w;
_pixelbuf_parse_color(self, value, &r, &g, &b, &w);
_pixelbuf_set_pixel_color(self, index, r, g, b, w);
}
void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, mp_int_t step, size_t slice_len, mp_obj_t* values) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
for (size_t i = 0; i < slice_len; i++) {
_pixelbuf_set_pixel(self, start, values[i]);
start+=step;
}
if (self->auto_write) {
common_hal__pixelbuf_pixelbuf_show(self_in);
}
}
void common_hal__pixelbuf_pixelbuf_set_pixel(mp_obj_t self_in, size_t index, mp_obj_t value) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
_pixelbuf_set_pixel(self, index, value);
if (self->auto_write) {
common_hal__pixelbuf_pixelbuf_show(self_in);
}
}
mp_obj_t common_hal__pixelbuf_pixelbuf_get_pixel(mp_obj_t self_in, size_t index) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
mp_obj_t elems[self->byteorder.bpp];
uint8_t* pixel_buffer = self->post_brightness_buffer;
if (self->pre_brightness_buffer != NULL) {
pixel_buffer = self->pre_brightness_buffer;
}
pixel_buffer += self->byteorder.bpp * index;
pixelbuf_rgbw_t *rgbw_order = &self->byteorder.byteorder;
elems[0] = MP_OBJ_NEW_SMALL_INT(pixel_buffer[rgbw_order->r]);
elems[1] = MP_OBJ_NEW_SMALL_INT(pixel_buffer[rgbw_order->g]);
elems[2] = MP_OBJ_NEW_SMALL_INT(pixel_buffer[rgbw_order->b]);
if (self->byteorder.bpp > 3) {
uint8_t w = pixel_buffer[rgbw_order->w];
if (self->byteorder.is_dotstar) {
elems[3] = mp_obj_new_float((w & 0b00011111) / 31.0);
} else {
elems[3] = MP_OBJ_NEW_SMALL_INT(w);
}
}
return mp_obj_new_tuple(self->byteorder.bpp, elems);
}
void common_hal__pixelbuf_pixelbuf_show(mp_obj_t self_in) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
mp_obj_t dest[2 + 1];
mp_load_method(self_in, MP_QSTR__transmit, dest);
dest[2] = self->transmit_buffer_obj;
mp_call_method_n_kw(1, 0, dest);
}
void common_hal__pixelbuf_pixelbuf_fill(mp_obj_t self_in, mp_obj_t fill_color) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t w;
_pixelbuf_parse_color(self, fill_color, &r, &g, &b, &w);
for (size_t i = 0; i < self->pixel_count; i++) {
_pixelbuf_set_pixel_color(self, i, r, g, b, w);
}
if (self->auto_write) {
common_hal__pixelbuf_pixelbuf_show(self_in);
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

暂无描述
暂无标签
MIT
使用 MIT 开源许可协议
, MIT
使用 MIT 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/stduino/circuitpython.git
git@gitee.com:stduino/circuitpython.git
stduino
circuitpython
circuitpython
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /