Qt Creator增量步骤与“for in range”循环

时间:2017-05-22 20:30:58

标签: python

我有Qt Creator按钮控制数字电位器(此刻连接到LED)。现在编写代码的方式,“增加”按钮使LED从完全调暗并逐渐增加到全亮,同样“减少”按钮则相反。这工作得很好,但我想要的是每按一次按钮增加或减少每次推动一步的LED亮度,而不是完全暗淡到亮,或亮到暗,如果这是有道理的。这就是我现在所拥有的:

var data = {
labels: ["Label1", "Label2", "Label3", "Label4", "Label5"],
datasets: [
{
   "label": "Total V",
   "xAxisID": "v",
   "backgroundColor": "rgba(53,81,103,1)",
   "borderColor": "rgba(53,81,103,.4)",
   "data": [100,90,80,70,60]
},
{
   "label": "Total C",
   "xAxisID": "c",
   "backgroundColor": "rgba(255,153,0,1)",
   "borderColor": "rgba(255,153,0,.4)",
   "data": [10,9,8,7,6]
}]};

var options = {scales:{
xAxes:[
   {position:"top",id:"v", gridLines:{color:"rgba(53,81,103,.4)"}},
   {position:"bottom",id:"c", gridLines:{color:"rgba(255,153,0,.4)"}}
]}};

var ctx = document.getElementById("myChart");
new Chart(ctx, {type: "horizontalBar",data:data, options:options});

因此,按下每个按钮,我希望数字电位器的电阻值增加或减少“5”,而不是通过整个范围。所以它部分工作但我现在卡住了。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您的LED控制器似乎总是采用全亮度值(在高8位中使用静态命令0x11),因此您需要将其状态存储在方法之外的某个位置以保持持久性。

编辑:这里是一个如何在课堂上表现的方式。它也经过了优化,因此它不会对控制器进行不必要的调用,而且您可以使用o2dim()以任意增量调暗/调暗灯光。

def __init__(self):  # the signature could be different
    # your code...
    self.resist_val = 0  # default (init) state
    self.dim_step = 5  # how much to increase/decrease on each call
    self.dim_command = 0x11 << 8  # change brightness command
    # call this only if your global digipot1 variable is initialized:
    self.o2dim(64)  # initialize the default state to 64

# rest of your class code

def o2dim(self, change=0):  # update the current dim state
    value = max(-1, min(64, self.resist_val + change)) # limit the state between -1 and 64
    if value != self.resist_val:  # update only if changed
        digipot1.put(self.dim_command | value, bits=64)
        self.resist_val = value

def o2zeroup(self):
    self.o2dim(-self.dim_step)

def o2zerodown(self):
    self.o2dim(self.dim_step)