Skip to content

Commit 167c4b8

Browse files
feat: Added the Logic for changing clipart to led hex (#958)
Co-authored-by: Aditya Gupta <[email protected]>
1 parent 0e70ee0 commit 167c4b8

8 files changed

+758
-204
lines changed

lib/bademagic_module/bluetooth/bluetooth.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import 'package:logger/logger.dart';
66

77
class BadgeMagicBluetooth {
88
static final Logger logger = Logger();
9+
static DataToByteArrayConverter converter = DataToByteArrayConverter();
910

1011
static Future<void> writeCharacteristic(
1112
BluetoothDevice device,
1213
Guid characteristicId,
1314
Data data,
1415
) async {
15-
List<List<int>> dataChunks = convert(data);
16+
List<List<int>> dataChunks = converter.convert(data);
1617
logger.d("Data to write: $dataChunks");
1718

1819
try {

lib/bademagic_module/utils/byte_array_utils.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ String toHex(List<int> bytes) {
1010
return buffer.toString().toUpperCase();
1111
}
1212

13+
bool isValidHex(String input) {
14+
return RegExp(r'^[0-9a-fA-F]+$').hasMatch(input);
15+
}
16+
1317
List<int> hexStringToByteArray(String hexString) {
14-
if (hexString.length % 2 != 0) {
18+
if (hexString.length % 2 != 0 || !isValidHex(hexString)) {
1519
throw ArgumentError("Invalid hex string: $hexString");
1620
}
1721

lib/bademagic_module/utils/converters.dart

+106-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,119 @@ class Converters {
66
//then adds the hexstring to the list
77
//thus generating the hex value of the message
88
static List<String> messageTohex(String message) {
9+
DataToByteArrayConverter converter = DataToByteArrayConverter();
910
List<String> messages = [];
1011
int i = 0;
1112
while (i < message.length) {
1213
var ch = message[i];
13-
logger.d("ch = $ch");
14-
if (charCodes.containsKey(ch)) {
15-
messages.add(charCodes[ch]!);
14+
if (converter.charCodes.containsKey(ch)) {
15+
messages.add(converter.charCodes[ch]!);
1616
}
1717
i++;
1818
}
19-
logger.d("message to hex = $message");
2019
return messages;
2120
}
21+
22+
//function to convert the bitmap to the LED hex format
23+
//it takes the 2D list of pixels and converts it to the LED hex format
24+
static List<String> convertBitmapToLEDHex(List<List<int>> image) {
25+
// Determine the height and width of the image
26+
int height = image.length;
27+
int width = image.isNotEmpty ? image[0].length : 0;
28+
29+
// Initialize variables to calculate padding and offsets
30+
int finalSum = 0;
31+
32+
// Calculate and adjust for right-side padding
33+
for (int j = 0; j < width; j++) {
34+
int sum = 0;
35+
for (int i = 0; i < height; i++) {
36+
sum += image[i][j]; // Sum up pixel values in each column
37+
}
38+
if (sum == 0) {
39+
// If column sum is zero, mark all pixels in that column as -1
40+
for (int i = 0; i < height; i++) {
41+
image[i][j] = -1;
42+
}
43+
} else {
44+
// Otherwise, update finalSum and exit loop
45+
finalSum += j;
46+
break;
47+
}
48+
}
49+
50+
// Calculate and adjust for left-side padding
51+
for (int j = width - 1; j >= 0; j--) {
52+
int sum = 0;
53+
for (int i = 0; i < height; i++) {
54+
sum += image[i]
55+
[j]; // Sum up pixel values in each column (from right to left)
56+
}
57+
if (sum == 0) {
58+
// If column sum is zero, mark all pixels in that column as -1
59+
for (int i = 0; i < height; i++) {
60+
image[i][j] = -1;
61+
}
62+
} else {
63+
// Otherwise, update finalSum and exit loop
64+
finalSum += (height - j - 1);
65+
break;
66+
}
67+
}
68+
69+
// Calculate padding difference to align height to a multiple of 8
70+
int diff = 0;
71+
if ((height - finalSum) % 8 > 0) {
72+
diff = 8 - (height - finalSum) % 8;
73+
}
74+
75+
// Calculate left and right offsets for padding
76+
int rOff = (diff / 2).floor();
77+
int lOff = (diff / 2).ceil();
78+
79+
// Initialize a new list to accommodate the padded image
80+
List<List<int>> list =
81+
List.generate(height, (i) => List.filled(width + rOff + lOff, 0));
82+
83+
// Fill the new list with the padded image data
84+
for (int i = 0; i < height; i++) {
85+
int k = 0;
86+
for (int j = 0; j < rOff; j++) {
87+
list[i][k++] = 0; // Fill right-side padding
88+
}
89+
for (int j = 0; j < width; j++) {
90+
if (image[i][j] != -1) {
91+
list[i][k++] = image[i][j]; // Copy non-padded pixels
92+
}
93+
}
94+
for (int j = 0; j < lOff; j++) {
95+
list[i][k++] = 0; // Fill left-side padding
96+
}
97+
}
98+
99+
// Convert each 8-bit segment into hexadecimal strings
100+
List<String> allHexs = [];
101+
for (int i = 0; i < list[0].length ~/ 8; i++) {
102+
StringBuffer lineHex = StringBuffer();
103+
104+
for (int k = 0; k < height; k++) {
105+
StringBuffer stBuilder = StringBuffer();
106+
107+
// Construct 8-bit segments for each row
108+
for (int j = i * 8; j < i * 8 + 8; j++) {
109+
stBuilder.write(list[k][j]);
110+
}
111+
112+
// Convert binary string to hexadecimal
113+
String hex = int.parse(stBuilder.toString(), radix: 2)
114+
.toRadixString(16)
115+
.padLeft(2, '0');
116+
lineHex.write(hex); // Append hexadecimal to line
117+
}
118+
119+
allHexs.add(lineHex.toString()); // Store completed hexadecimal line
120+
}
121+
122+
return allHexs; // Return list of hexadecimal strings
123+
}
22124
}

0 commit comments

Comments
 (0)