forked from lmdpua/A2168_Laptop_Battery_Flasher_on_CP2112
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButtons_QuickCommands.cpp
216 lines (179 loc) · 5.32 KB
/
Buttons_QuickCommands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QBitArray>
enum QuickCommands{
Device_Part_Number = 0x0001,
FW_Version = 0x0002,
HW_Version = 0x0003,
Chemistry_ID = 0x0008,
Shutdown = 0x0010,
Sleep = 0x0011,
Seal = 0x0020,
Gas_Gauge_Enable = 0x0021,
FUSO_Activation = 0x0030,
FUSO_Clear = 0x0031,
LEDs_On = 0x0032,
LEDs_Off = 0x0033,
Display_On = 0x0034,
Calibration_Mode = 0x0040,
Reset = 0x0041,
BootROM = 0x0046
};
void MainWindow::sendCommand(quint16 command, bool answer)
{
QString str = QString("%1").arg(command, 4, 16, QLatin1Char('0')).toUpper();
str.prepend("0x");
ui->lineEdit_Command->setText(str);
while(device.isInTransferMode()){} //Ждем пока закончится передача
device.setTransfer(true);//Поставим флаг, что устройство в режиме передачи
if(!device.init()){
device.close();
device.setTransfer(false);
ui->lineEdit_Result->setText("No Device!");
return;
}
if(!battery.isConnected(&device)){ //Если батарея не подключена
ui->lineEdit_Result->setText("No Battery!");
}else{
if(battery.writeWord(&device, 0x00, command)){//Отправляем команду
if(answer){ //Если нужет ответ
quint16 data;
if(battery.readWord(&device, 0x00, &data)){//Читаем ответ
ui->lineEdit_Result->setText(QString::number(data,16).toUpper());
}else{//Если ответ не пришел
ui->lineEdit_Result->setText("No Response!");
}
}else{//Если ответ не нужен
ui->lineEdit_Result->setText("Done!");
}
}
}
device.close();
device.setTransfer(false);//снимем флаг, что устройство в режиме передачи
}
void MainWindow::on_button_DevicePartNumber_clicked()
{
sendCommand(Device_Part_Number, true);
}
void MainWindow::on_button_FW_Version_clicked()
{
sendCommand(FW_Version, true);
}
void MainWindow::on_button_HW_Version_clicked()
{
sendCommand(HW_Version, true);
}
void MainWindow::on_button_Chemistry_ID_clicked()
{
sendCommand(Chemistry_ID, true);
}
void MainWindow::on_button_Shutdown_clicked()
{
sendCommand(Shutdown, false);
}
void MainWindow::on_button_Sleep_clicked()
{
sendCommand(Sleep, false);
}
void MainWindow::on_button_Seal_clicked()
{
sendCommand(Seal, false);
}
void MainWindow::on_button_Gas_Gauge_Enable_clicked()
{
sendCommand(Gas_Gauge_Enable, false);
}
void MainWindow::on_button_FUSO_Activation_clicked()
{
sendCommand(FUSO_Activation, false);
}
void MainWindow::on_button_FUSO_Clear_clicked()
{
sendCommand(FUSO_Clear, false);
}
void MainWindow::on_button_LEDs_On_clicked()
{
sendCommand(LEDs_On, false);
}
void MainWindow::on_button_LEDs_Off_clicked()
{
sendCommand(LEDs_Off, false);
}
void MainWindow::on_button_Display_On_clicked()
{
sendCommand(Display_On, false);
}
void MainWindow::on_button_Calibration_Mode_clicked()
{
sendCommand(Calibration_Mode, false);
}
void MainWindow::on_button_Reset_clicked()
{
sendCommand(Reset, false);
}
void MainWindow::on_button_BootROM_clicked()
{
QString str = "Are you sure you want to switch the battery to BootROM mode?";
QMessageBox::StandardButton reply;
reply = QMessageBox::question(this, "Warning", str, QMessageBox::Yes|QMessageBox::No);
if(reply == QMessageBox::No)return;
sendCommand(BootROM, false);
}
enum FET_Control{
Discharge_FET = 0x1,
Charge_FET = 0x2,
Precharge_FET = 0x3
};
void MainWindow::FET(quint8 fet, bool on)
{
while(device.isInTransferMode()){} //Ждем пока закончится передача
device.setTransfer(true);//Поставим флаг, что устройство в режиме передачи
if(!device.init()){
device.close();
device.setTransfer(false);
ui->lineEdit_Result->setText("No Device!");
return;
}
quint16 fet_data;
if(!battery.readWord(&device, 0x46,&fet_data)){
device.close();
device.setTransfer(false);//снимем флаг, что устройство в режиме передачи
}
if(on){
fet_data |= 1UL << fet;//Установим бит
}else{
fet_data &= ~(1UL << fet);//Очистим бит
}
if(battery.writeWord(&device, 0x46, fet_data)){
ui->lineEdit_FET_Result->setText("Done!");
}else{
ui->lineEdit_FET_Result->setText("Error!");
}
device.close();
device.setTransfer(false);
}
void MainWindow::on_button_PFET_On_clicked()
{
FET(Precharge_FET, true);
}
void MainWindow::on_button_PFET_Off_clicked()
{
FET(Precharge_FET, false);
}
void MainWindow::on_button_CFET_On_clicked()
{
FET(Charge_FET, true);
}
void MainWindow::on_button_CFET_Off_clicked()
{
FET(Charge_FET, false);
}
void MainWindow::on_button_DFET_On_clicked()
{
FET(Discharge_FET, true);
}
void MainWindow::on_button_DFET_Off_clicked()
{
FET(Discharge_FET, false);
}