Skip to content

Commit 8370005

Browse files
committed
fixez
1 parent 494a3de commit 8370005

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

pkg/sensehat/sensehat.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func (s *SenseHat) readRegs(addr uint8, reg byte, n int) ([]byte, error) {
229229
if err := setI2CAddr(s.i2cFile, addr); err != nil {
230230
return nil, err
231231
}
232-
// Set auto-increment bit for multi-byte reads
232+
// Set auto-increment bit for multi-byte reads (0x80 for HTS221/LPS25H/LSM9DS1)
233233
if _, err := s.i2cFile.Write([]byte{reg | 0x80}); err != nil {
234234
return nil, err
235235
}
@@ -240,6 +240,23 @@ func (s *SenseHat) readRegs(addr uint8, reg byte, n int) ([]byte, error) {
240240
return buf, nil
241241
}
242242

243+
// readColorRegs reads multiple registers from the TCS34725/TCS3400 color sensor.
244+
// The color sensor uses a different command format: 0x80 (command) | 0x20 (auto-increment) | reg.
245+
func (s *SenseHat) readColorRegs(reg byte, n int) ([]byte, error) {
246+
if err := setI2CAddr(s.i2cFile, s.colorSensorAddr); err != nil {
247+
return nil, err
248+
}
249+
// TCS34725/TCS3400 command format: bit 7 = command, bits 6:5 = 01 for auto-increment
250+
if _, err := s.i2cFile.Write([]byte{0x80 | 0x20 | reg}); err != nil {
251+
return nil, err
252+
}
253+
buf := make([]byte, n)
254+
if _, err := s.i2cFile.Read(buf); err != nil {
255+
return nil, err
256+
}
257+
return buf, nil
258+
}
259+
243260
// initHTS221 initializes the HTS221 humidity/temperature sensor and reads calibration data.
244261
func (s *SenseHat) initHTS221() error {
245262
// Power on, BDU enabled, ODR 1Hz
@@ -478,7 +495,10 @@ func (s *SenseHat) readJoystickEvents() {
478495
}
479496

480497
s.joystickMu.Lock()
481-
s.joystickEvents = append(s.joystickEvents, event)
498+
// Limit buffer size to prevent unbounded growth if events aren't consumed
499+
if len(s.joystickEvents) < 100 {
500+
s.joystickEvents = append(s.joystickEvents, event)
501+
}
482502
s.joystickMu.Unlock()
483503
}
484504
}
@@ -644,8 +664,7 @@ func (s *SenseHat) GetColor() (r, g, b, c uint8, err error) {
644664
}
645665

646666
// Read RGBC data starting at CDATAL (0x14)
647-
// Note: readRegs already sets the auto-increment bit (0x80)
648-
data, err := s.readRegs(s.colorSensorAddr, 0x14, 8)
667+
data, err := s.readColorRegs(0x14, 8)
649668
if err != nil {
650669
return 0, 0, 0, 0, err
651670
}
@@ -675,11 +694,16 @@ func GetCPUTemperature() (float64, error) {
675694

676695
scanner := bufio.NewScanner(f)
677696
if scanner.Scan() {
678-
if temp, err := strconv.ParseFloat(strings.TrimSpace(scanner.Text()), 64); err == nil {
679-
return temp / 1000.0, nil
697+
temp, err := strconv.ParseFloat(strings.TrimSpace(scanner.Text()), 64)
698+
if err != nil {
699+
return 0, fmt.Errorf("failed to parse CPU temperature: %w", err)
680700
}
701+
return temp / 1000.0, nil
702+
}
703+
if err := scanner.Err(); err != nil {
704+
return 0, err
681705
}
682-
return 0, scanner.Err()
706+
return 0, fmt.Errorf("failed to read CPU temperature: empty file")
683707
}
684708

685709
// initFramebuffer finds and opens the Sense HAT LED framebuffer device.
@@ -783,14 +807,22 @@ func (s *SenseHat) SetNavLights() error {
783807
func (s *SenseHat) FlashStrobes(duration time.Duration) error {
784808
for flash := 0; flash < 2; flash++ {
785809
// Flash on
786-
s.SetPixel(0, 7, 255, 255, 255) // Left strobe (x=0, y=7)
787-
s.SetPixel(7, 7, 255, 255, 255) // Right strobe (x=7, y=7)
810+
if err := s.SetPixel(0, 7, 255, 255, 255); err != nil { // Left strobe (x=0, y=7)
811+
return err
812+
}
813+
if err := s.SetPixel(7, 7, 255, 255, 255); err != nil { // Right strobe (x=7, y=7)
814+
return err
815+
}
788816

789817
time.Sleep(duration)
790818

791819
// Flash off
792-
s.SetPixel(0, 7, 0, 0, 0)
793-
s.SetPixel(7, 7, 0, 0, 0)
820+
if err := s.SetPixel(0, 7, 0, 0, 0); err != nil {
821+
return err
822+
}
823+
if err := s.SetPixel(7, 7, 0, 0, 0); err != nil {
824+
return err
825+
}
794826

795827
if flash == 0 {
796828
time.Sleep(duration / 2)

0 commit comments

Comments
 (0)