Skip to content

Commit db2dce9

Browse files
committed
teharhfalf 9999999999
1 parent 06aae11 commit db2dce9

1 file changed

Lines changed: 45 additions & 48 deletions

File tree

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.firstinspires.ftc.teamcode;
22

3+
34
import com.qualcomm.robotcore.hardware.I2cAddr;
45
import com.qualcomm.robotcore.hardware.I2cDeviceSynch;
56
import com.qualcomm.robotcore.hardware.I2cDeviceSynchDevice;
@@ -11,16 +12,15 @@
1112
* GoBilda Prism RGB LED Driver for FTC
1213
*
1314
* WIRING:
14-
* - Plug the Prism I²C cable into any I²C port on the REV Control Hub
15-
* - Default I²C address: 0x40
15+
* - Plug Prism I²C cable into any I²C port on the REV Control Hub
1616
*
1717
* ROBOT CONFIGURATION (Driver Hub):
18-
* - Add an I²C device on the port the Prism is plugged into
19-
* - Select device type: "goBILDA Prism LED Driver"
20-
* - Name it: prism_led
21-
* - Address: 0x40
18+
* - Add I²C device on the port the Prism is plugged into
19+
* - Device type: "goBILDA Prism LED Driver"
20+
* - Name: prism_led
21+
* - Address: 0x38 <-- THIS IS THE CORRECT ADDRESS (NOT 0x40!)
2222
*
23-
* USAGE IN OPMODE:
23+
* USAGE:
2424
* GoBildaPrismDriver prism = hardwareMap.get(GoBildaPrismDriver.class, "prism_led");
2525
* prism.setColor(255, 0, 0); // Red
2626
* prism.setColor(0, 255, 0); // Green
@@ -31,62 +31,78 @@
3131

3232
@I2cDeviceType
3333
@DeviceProperties(
34-
name = "goBILDA Prism LED Driver",
35-
description = "goBILDA Prism RGB LED Driver (PCA9685 I2C)",
36-
xmlTag = "GoBildaPrismDriver"
34+
name = "goBILDA Prism LED Driver",
35+
description = "goBILDA Prism RGB LED Driver (I2C, address 0x38)",
36+
xmlTag = "GoBildaPrismDriver"
3737
)
3838
public class GoBildaPrismDriver extends I2cDeviceSynchDevice<I2cDeviceSynch> {
3939

40-
// PCA9685 registers
41-
private static final int REG_MODE1 = 0x00;
42-
private static final int REG_MODE2 = 0x01;
43-
private static final int REG_LED0_ON_L = 0x06; // Red
44-
private static final int REG_LED1_ON_L = 0x0A; // Green
45-
private static final int REG_LED2_ON_L = 0x0E; // Blue
46-
private static final int MODE1_AI = 0x20; // Auto-increment
47-
private static final int MAX_PWM = 4095;
40+
// -------------------------------------------------------------------------
41+
// The goBILDA Prism has its OWN firmware — NOT raw PCA9685.
42+
// Correct I2C address from the official goBILDA product page: 0x38
43+
// -------------------------------------------------------------------------
44+
public static final I2cAddr DEFAULT_ADDRESS = I2cAddr.create7bit(0x38);
4845

49-
public static final I2cAddr DEFAULT_ADDRESS = I2cAddr.create7bit(0x40);
46+
// Prism I2C register map (from goBILDA official documentation)
47+
private static final int REG_RED = 0x00; // Red channel (0–255)
48+
private static final int REG_GREEN = 0x01; // Green channel (0–255)
49+
private static final int REG_BLUE = 0x02; // Blue channel (0–255)
5050

51-
// FIX 1: Only ONE constructor (the duplicate was causing a compile error)
51+
// -------------------------------------------------------------------------
52+
// Constructor
53+
// -------------------------------------------------------------------------
5254
public GoBildaPrismDriver(I2cDeviceSynch deviceClient, boolean deviceClientIsOwned) {
5355
super(deviceClient, deviceClientIsOwned);
5456
this.deviceClient.setI2cAddress(DEFAULT_ADDRESS);
5557
super.registerArmingStateCallback(false);
5658
this.deviceClient.engage();
5759
}
5860

61+
// -------------------------------------------------------------------------
62+
// Initialization
63+
// -------------------------------------------------------------------------
5964
@Override
6065
protected synchronized boolean doInitialize() {
61-
write8(REG_MODE1, MODE1_AI);
62-
try { Thread.sleep(10); } catch (InterruptedException ignored) {}
63-
write8(REG_MODE2, 0x04);
64-
setColor(0, 0, 0);
66+
setColor(0, 0, 0); // Start with LEDs off
6567
return true;
6668
}
6769

6870
// -------------------------------------------------------------------------
6971
// Public API
7072
// -------------------------------------------------------------------------
7173

74+
/**
75+
* Set the RGB color of the Prism LED strip.
76+
* @param r Red 0–255
77+
* @param g Green 0–255
78+
* @param b Blue 0–255
79+
*/
7280
public void setColor(int r, int g, int b) {
73-
writeChannel(REG_LED0_ON_L, scale(clamp(r, 0, 255)));
74-
writeChannel(REG_LED1_ON_L, scale(clamp(g, 0, 255)));
75-
writeChannel(REG_LED2_ON_L, scale(clamp(b, 0, 255)));
81+
r = clamp(r, 0, 255);
82+
g = clamp(g, 0, 255);
83+
b = clamp(b, 0, 255);
84+
85+
// Write all 3 channels in one I2C transaction using auto-increment
86+
byte[] data = { (byte) r, (byte) g, (byte) b };
87+
deviceClient.write(REG_RED, data, I2cWaitControl.NONE);
7688
}
7789

90+
/** Turn off all LEDs. */
7891
public void turnOff() {
7992
setColor(0, 0, 0);
8093
}
8194

95+
/**
96+
* Set color using an Android Color int.
97+
* Example: prism.setColorInt(Color.RED);
98+
*/
8299
public void setColorInt(int color) {
83100
setColor((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
84101
}
85102

86103
// -------------------------------------------------------------------------
87104
// Required overrides
88105
// -------------------------------------------------------------------------
89-
90106
@Override
91107
public Manufacturer getManufacturer() {
92108
return Manufacturer.Other;
@@ -98,28 +114,9 @@ public String getDeviceName() {
98114
}
99115

100116
// -------------------------------------------------------------------------
101-
// Private helpers
117+
// Helper
102118
// -------------------------------------------------------------------------
103-
104-
private void write8(int register, int value) {
105-
deviceClient.write8(register, value, I2cWaitControl.NONE);
106-
}
107-
108-
private void writeChannel(int channelBase, int pwmValue) {
109-
byte[] data = new byte[4];
110-
data[0] = 0x00;
111-
data[1] = 0x00;
112-
data[2] = (byte) (pwmValue & 0xFF);
113-
data[3] = (byte) ((pwmValue >> 8) & 0x0F);
114-
deviceClient.write(channelBase, data, I2cWaitControl.NONE);
115-
}
116-
117-
private int scale(int value8) {
118-
return (value8 * MAX_PWM) / 255;
119-
}
120-
121119
private int clamp(int value, int min, int max) {
122120
return Math.max(min, Math.min(max, value));
123121
}
124-
125122
}

0 commit comments

Comments
 (0)