Python 3.x 我如何让这个程序只生成变量的输出:265? #/usr/bin/env python #i2c_ADXL345.py este el Acceleroómetro de 3 ejes # 2015-04-01 #公共领域 导入时间 导入结构 导入系统 进口pigpio#http://abyz.co.uk/rpi/pigpio/python.html 如果sys.version>'3': 缓冲区=内存视图 总线=0 ADXL345_I2C_ADDR=0x53 #运行时=60.0这是原始行。 #运行时=0.10/这是新行 pi=pigpio.pi()#打开本地pi h=pi.i2c_开路(总线,ADXL345_i2c_地址) 如果h>=0:#连接是否正常? #初始化ADXL345。 pi.i2c_写入_字节_数据(h,0x2d,0)#电源控制复位。 pi.i2c_write_byte_data(h,0x2d,8)#POWER_CTL measure。 pi.i2c_写入_字节_数据(h,0x31,0)#数据_格式重置。 pi.i2c_写入_字节_数据(h,0x31,11)#数据_格式完整分辨率+/-16g。 读取=0 #start_time=time.time()?运行时的一部分? #while(time.time()-start_time)

Python 3.x 我如何让这个程序只生成变量的输出:265? #/usr/bin/env python #i2c_ADXL345.py este el Acceleroómetro de 3 ejes # 2015-04-01 #公共领域 导入时间 导入结构 导入系统 进口pigpio#http://abyz.co.uk/rpi/pigpio/python.html 如果sys.version>'3': 缓冲区=内存视图 总线=0 ADXL345_I2C_ADDR=0x53 #运行时=60.0这是原始行。 #运行时=0.10/这是新行 pi=pigpio.pi()#打开本地pi h=pi.i2c_开路(总线,ADXL345_i2c_地址) 如果h>=0:#连接是否正常? #初始化ADXL345。 pi.i2c_写入_字节_数据(h,0x2d,0)#电源控制复位。 pi.i2c_write_byte_data(h,0x2d,8)#POWER_CTL measure。 pi.i2c_写入_字节_数据(h,0x31,0)#数据_格式重置。 pi.i2c_写入_字节_数据(h,0x31,11)#数据_格式完整分辨率+/-16g。 读取=0 #start_time=time.time()?运行时的一部分? #while(time.time()-start_time),python-3.x,Python 3.x,本例中变量的名称是x,定义见(x,y,z)=struct.unpack('我从来没有写过一行python代码,但是print({}{}{}})。format(x,y,z))看起来可疑。我会尝试将它改为print({}.format(x))看看会发生什么。这就是我尝试的!我检查了脚本,这是我更改的最后一行。它生成(264,-14,-33)。它不仅生成了三个轴,而且现在它将它们封装在并列中-lol.Oho!我刚刚返回了这一行(x,y,z)=struct.unpack('这种类型的更改似乎确实有效。请

本例中变量的名称是
x
,定义见
(x,y,z)=struct.unpack('我从来没有写过一行python代码,但是
print({}{}{}})。format(x,y,z))
看起来可疑。我会尝试将它改为
print({}.format(x))
看看会发生什么。这就是我尝试的!我检查了脚本,这是我更改的最后一行。它生成(264,-14,-33)。它不仅生成了三个轴,而且现在它将它们封装在并列中-lol.Oho!我刚刚返回了这一行(x,y,z)=struct.unpack('这种类型的更改似乎确实有效。请参阅。
x
具有这三个值中的第一个。是的,注释掉变量的赋值,但随后使用它不太可能是您想要的。谢谢,mah-您的幽默也得到了赞赏。代码示例非常优秀-简短,切中要害-希望我能早点找到它。有一个wo这是一个愉快的日子!:)
#!/usr/bin/env python

# i2c_ADXL345.py  este es el accelerómetro de 3 ejes
# 2015-04-01
# Public Domain

import time
import struct
import sys

import pigpio # http://abyz.co.uk/rpi/pigpio/python.html

if sys.version > '3':
   buffer = memoryview

BUS=0

ADXL345_I2C_ADDR=0x53

#RUNTIME=60.0 This is the original line.
#RUNTIME=0.10 / This was the new line

pi=pigpio.pi() # open local Pi

h = pi.i2c_open(BUS, ADXL345_I2C_ADDR)

if h >= 0: # Connected OK?

   # Initialise ADXL345.
   pi.i2c_write_byte_data(h, 0x2d, 0)  # POWER_CTL reset.
   pi.i2c_write_byte_data(h, 0x2d, 8)  # POWER_CTL measure.
   pi.i2c_write_byte_data(h, 0x31, 0)  # DATA_FORMAT reset.
   pi.i2c_write_byte_data(h, 0x31, 11) # DATA_FORMAT full res +/- 16g.

   read = 0

#   start_time = time.time() ?part of the RUNTIME?

#   while (time.time()-start_time) < RUNTIME: ?part of the RUNTIME?

      # 0x32 = X LSB, 0x33 = X MSB
      # 0x34 = Y LSB, 0x35 = Y MSB
      # 0x36 = Z LSB, 0x37 = Z MSB

      # < = little endian

(s, b) = pi.i2c_read_i2c_block_data(h, 0x32, 6)

if s >= 0:
         (x, y, z) = struct.unpack('<3h', buffer(b))
         print("{} {} {}".format(x, y, z))
         read += 1

pi.i2c_close(h)

pi.stop()

print()
#!/usr/bin/env python

# i2c_ADXL345.py  this is the 3 axis accelerometer
# 2015-04-01
# Public Domain
# Note - Don't forget to run pigpiod first

import time
import struct
import sys

import pigpio # http://abyz.co.uk/rpi/pigpio/python.html

if sys.version > '3':
   buffer = memoryview

BUS=0

ADXL345_I2C_ADDR=0x53

#RUNTIME=60.0 This is the original line.
#RUNTIME=0.10 / This was the new line

pi=pigpio.pi() # open local Pi

h = pi.i2c_open(BUS, ADXL345_I2C_ADDR)

if h >= 0: # Connected OK?

   # Initialise ADXL345.
   pi.i2c_write_byte_data(h, 0x2d, 0)  # POWER_CTL reset.
   pi.i2c_write_byte_data(h, 0x2d, 8)  # POWER_CTL measure.
   pi.i2c_write_byte_data(h, 0x31, 0)  # DATA_FORMAT reset.
   pi.i2c_write_byte_data(h, 0x31, 11) # DATA_FORMAT full res +/- 16g.

   read = 0

#   start_time = time.time() ?part of the RUNTIME?

#   while (time.time()-start_time) < RUNTIME: ?part of the RUNTIME?

      # 0x32 = X LSB, 0x33 = X MSB
      # 0x34 = Y LSB, 0x35 = Y MSB
      # 0x36 = Z LSB, 0x37 = Z MSB

      # < = little endian

(s, b) = pi.i2c_read_i2c_block_data(h, 0x32, 6)

if s >= 0:
         (x, y, z) = struct.unpack('<3h', buffer(b))
#         (x) = struct.unpack('<3h', buffer(b)) # Let's experiment again.
#         print("{} {} {}".format(x, y, z))
         print("{}".format(x))

         read += 1

pi.i2c_close(h)

pi.stop()

print()