文档章节

呼吸灯的深入研究

BetaYuan
 BetaYuan
发布于 2016/10/31 12:58
字数 1030
阅读 303
收藏 0

精选30+云产品,助力企业轻松上云!>>>

http://sean.voisen.org/blog/2011/10/breathing-led-with-arduino/

Re-creating the "breathing" LED

呼吸灯的逆向工程

October 31, 2011

Perhaps it’s not surprising, but Apple has a patent on the iconic "breathing" pattern used for the sleep indicator LED on all their computers.

也许呼吸灯这东西没什么大不了的,然而苹果有一个专利关于用于指示电脑处于睡眠模式下LED亮灭的模式。

Recently — for a personal project — I wanted to see if I could replicate this effect. It turns out that I wasn’t alone. Ladyada tried to reverse engineer the pattern a few years ago. Unfortunately, she stopped short of providing anything — like code — that the lazy web surfing Arduino hacker might use to recreate the effect. That’s the purpose of this short tutorial.

最近有个个人项目,我想看看是不是能复制这个效果。事实上并不是我一个人这么做。Ladyada试着逆向工程这个几年前专利里提及的模式。不幸的是,她没有提供任何更进一步的东西,例如代码,那种慵懒的Arduino玩家可以用来创造这种效果的。这也是本教程的目的所在。

Digging Into the Math

挖掘数学根基

The Apple patent claims that the breathing pattern is a simple sinusoid, but observation of one’s own (heavy) breathing will show that the pattern is a little more complicated than that. Maybe a sine wave works for Apple, but it doesn’t look quite right to me. In my own breathing, I tend to ease in to a fast inhale, and stop suddenly before easing out to a fast exhale. Also, the period between inhalation and exhalation happens to be shorter than the period between exhalation and the next inhalation. This is not a simple sinusoid, as the oscilloscope plot from Ladyada’s own investigations will attest.

苹果的专利声称呼吸模式只是简单的类正弦函数,但是据观察一个人(深)呼吸的模式比这个略复杂。也许一个正弦波对苹果是足够的,但是无法满足我的要求。在我呼吸的过程中,我趋向于不自在的快速吸气,然后突然停下来在#@%×(@#×(&%(@#%(。。。同时,从吸气到呼气之间的时间长度显得比呼气到下一次吸气之间的短。这不是简单的正弦,就如来自Ladyada(一个技术大姐)的示波器波形所揭示的。

A commenter on Ladyada’s blog suggested that the pattern is probably more accurately modeled by f(x) = esin(x). Plotting this equation gives the following:

一个在Ladyada博客上一则评论认为这种闪烁模式能更精确的用 f(x) = esin(x)所建模。下图是该方程的图像:

Plot of exp(sin(x))

Compare the curvature characteristics of this plot with the simple sinusoid below:

对比这条曲线和正弦曲线的差异:

Plot of sin(x)

The plot of f(x) = esin(x) has wider "troughs" (periods between inhale and exhale) and narrower "peaks" (periods between exhale and inhale), more accurately matching natural breathing patterns. As a simple experiment, try breathing "sinusoidally" and you’ll see how unnatural it feels.

 

Turning it Into Code

For my own experimentation, I used the ubiquitous Arduino. The Arduino supports analog output using pulse-width modulation (PWM) mapped to integer values from 0 to 255. To recreate the breathing LED, this means manipulating the original equation f(x) = esin(x) such that the amplitude fits within the PWM range.

I took enough math in school to know that the minima and maxima of any equation occur at critical points in the equation, where the derivative of that equation is either 0 or its not differentiable. Beyond that, I left it to Wolfram Alpha to do the hard work. It turns out that the minimum of the wave is 1/e, and the maximum is e. Using this information to adjust the amplitude of the equation such that it fits within the 0 to 255 range gives the following:

The final equation

Swap x for the number of seconds that have elapsed, and map the above equation to PWM output on any supported Arduino pin, and you have the beginnings of a breathing pattern. The problem is that the frequency may be too high or low (depending on your preference), and so the breathing will appear fast or slow. Easy enough: Multiply x by any value to adjust the frequency. I like π/2.

Finally, 1 - e, and 255/(e - 1/e) are constants, and can be pre-calcuated to reduce overhead. The final Arduino sketch is as follows (with the LED connected to pin 11, a suitable resistor in series, yadda, yadda …):

#include <math.h>

void setup()
{
  pinMode(11, OUTPUT);
}

void loop()
{
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(11, val);
}

Gratuitous Video

And, in case you don’t have an Arduino handy, here’s a short video of the final effect:

 

Conclusion

You might be asking: Is it really that big of a difference? Wouldn’t a simple sinusoid suffice? To answer the latter question: yes. To answer the former: the difference is noticeable, but only slightly. Steve Jobs was a notorious perfectionist. I like to think that he would’ve cared about such things.

More posts in the archives →

BetaYuan
粉丝 0
博文 50
码字总数 14252
作品 0
松江
程序员
私信 提问
加载中
请先登录后再评论。
[FPGA]Verilog利用PWM调制完成RGB三色彩虹呼吸灯

<input type="hidden" id="headbgimg" value="https://s2.ax1x.com/2019/11/28/QF00JK.jpg"/> 概述 实现彩虹呼吸灯 题目就是这么简短,但这是目前我碰到的最有意思的一道题目,因为他有无数种解......

osc_oupyj5vz
2019/11/28
2
0
iOS使用[核心动画]和[定时器]两种方式实现【呼吸灯动画】(仿蘑菇街价格标签)

最近公司需求做个类似小红书的标签呼吸灯动画,经过一段时间研究使用两种方式实现了该效果… 第一种方式使用定时器加 UIView动画,核心方法如下

秦无炎
2016/10/14
207
0
单片机呼吸灯

呼吸灯大家应该都知道,就是一盏灯慢慢的亮起来又慢慢的灭下去形成一个类似呼吸的效果。现在大家的手机上应该都有这个灯。 那么呼吸灯到底是怎么实现的呢原理又是什么? 其实就是控制一盏灯的...

浪子-
05/03
0
0
android 呼吸灯实现源码

  呼吸灯是个非常酷的功能,闲暇之余就尝试做了这个。实现原理:利用android的通知指示灯,通过闹钟的方式实现呼吸灯。   俗话说“巧妇难为无米之炊”,我们的呼吸灯在那里呢?哈哈,就用...

androidCode
2013/01/23
1.7K
0
呼吸灯和花样流水灯 51单片机实现

1.用C51单片机实现花样流水灯,代码如下: #include <reg52.h> #define LEDAll P1            //led灯所用的接口,单片机接口不同,led灯不同 void delayms() { unsigned char x...

osc_8iux0cyz
2019/09/28
11
0

没有更多内容

加载失败,请刷新页面

加载更多

将字符串拆分为具有多个单词边界定界符的单词 - Split Strings into words with multiple word boundary delimiters

问题: I think what I want to do is a fairly common task but I've found no reference on the web. 我认为我想做的是一项相当普通的任务,但是我在网络上找不到任何参考。 I have text ...

fyin1314
38分钟前
9
0
jQuery选择器中的通配符 - Wildcards in jQuery selectors

问题: I'm trying to use a wildcard to get the id of all the elements whose id begin with "jander". 我正在尝试使用通配符来获取id以“jander”开头的所有元素的id。 I tried $('#jand......

法国红酒甜
今天
19
0
唐山5.1级地震 百度人工智能技术帮上忙:成都减灾所提前30秒让北京、天津市民收到预警

本文作者:y****n 7月12日6时38分在河北唐山市古冶区发生5.1级地震,成都高新减灾研究所与应急管理部门联合建成的大陆地震预警网成功预警该地震,给唐山市提前3秒预警,给天津市提前33秒预警...

百度开发者中心
昨天
30
0
如何使用PHP发送POST请求? - How do I send a POST request with PHP?

问题: Actually I want to read the contents that come after the search query, when it is done. 实际上,我想阅读搜索查询之后的内容,完成之后。 The problem is that the URL only a......

javail
今天
14
0
如何从Java读取文件夹中的所有文件? - How to read all files in a folder from Java?

问题: 如何通过Java读取文件夹中的所有文件? 解决方案: 参考一: https://stackoom.com/question/7jt2/如何从Java读取文件夹中的所有文件 参考二: https://oldbug.net/q/7jt2/How-to-rea...

富含淀粉
今天
25
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部