Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added the code for the left scroll animation. #997

Binary file removed assets/icons/postman collections.zip
Binary file not shown.
1 change: 1 addition & 0 deletions devtools_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
extensions:
- provider: true
53 changes: 53 additions & 0 deletions lib/bademagic_module/utils/byte_array_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,56 @@ List<int> hexStringToByteArray(String hexString) {
logger.d(data.length);
return data;
}

List<List<int>> byteArrayToBinaryArray(List<int> byteArray) {
List<List<int>> binaryArray = List.generate(11, (_) => []);

int rowIndex = 0;
for (int byte in byteArray) {
List<int> binaryRepresentation = [];
for (int i = 7; i >= 0; i--) {
binaryRepresentation.add((byte >> i) & 1);
}

binaryArray[rowIndex].addAll(binaryRepresentation);

rowIndex = (rowIndex + 1) % 11;
}

logger.d(
"binaryArray: $binaryArray"); // Use print instead of logger for standalone example
return binaryArray;
}

String hexToBin(String hex) {
// Convert hex to binary string
String binaryString = BigInt.parse(hex, radix: 16).toRadixString(2);

// Pad the binary string with leading zeros if necessary to ensure it's a multiple of 8 bits
int paddingLength = (8 - (binaryString.length % 8)) % 8;
binaryString = binaryString.padLeft(binaryString.length + paddingLength, '0');
logger.d("binaryString: $binaryString");
return binaryString;
}

List<List<int>> binaryStringTo2DList(String binaryString) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Simplify and optimize binaryStringTo2DList function

The current implementation uses complex nested loops. Consider rewriting this function to use a single loop with index manipulation for better efficiency and readability.

List<List<int>> binaryStringTo2DList(String binaryString) {
  final int maxHeight = 11;
  final int width = binaryString.length ~/ maxHeight;
  return List.generate(maxHeight, (i) =>
    binaryString.substring(i * width, (i + 1) * width).split('').map(int.parse).toList()
  );
}

int maxHeight = 11;
List<List<int>> binary2DList = List.generate(maxHeight, (_) => []);

for (int x = 0; x < binaryString.length; x++) {
int a = 0;
for (int y = a; y < 11; y++) {
for (int z = 0; z < 8; z++) {
binary2DList[y].add(int.parse(binaryString[x++]));
if (x >= binaryString.length) {
break;
}
}
if (x >= binaryString.length) {
break;
}
}
}
logger.d("binary2DList: $binary2DList");
return binary2DList;
}
33 changes: 25 additions & 8 deletions lib/bademagic_module/utils/converters.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import 'dart:math';
import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart';
import 'package:badgemagic/bademagic_module/utils/data_to_bytearray_converter.dart';
import 'package:badgemagic/bademagic_module/utils/file_helper.dart';
import 'package:badgemagic/bademagic_module/utils/image_utils.dart';
import 'package:badgemagic/providers/badgeview_provider.dart';
import 'package:badgemagic/providers/imageprovider.dart';
import 'package:get_it/get_it.dart';

class Converters {
Converters._privateConstructor();
static final Converters _instance = Converters._privateConstructor();
factory Converters() {
return _instance;
}
InlineImageProvider controllerData =
GetIt.instance.get<InlineImageProvider>();
DrawBadgeProvider badgeList = GetIt.instance.get<DrawBadgeProvider>();
DataToByteArrayConverter converter = DataToByteArrayConverter();
ImageUtils imageUtils = ImageUtils();
FileHelper fileHelper = FileHelper();
Expand All @@ -18,15 +24,13 @@ class Converters {
Future<List<String>> messageTohex(String message) async {
List<String> hexStrings = [];
for (int x = 0; x < message.length; x++) {
if (message[x] == '<' && message[min(x + 5, message.length - 1)] == '>') {
if (message[x] == '<' && message[x + 5] == '>') {
int index = int.parse(message[x + 2] + message[x + 3]);
var key = controllerData.imageCache.keys.toList()[index];
if (key is List) {
String filename = key[0];
logger.d("Filename: $filename");
List<List<int>>? image = await fileHelper.readFromFile(filename);
logger.d("Image: $image");
hexStrings = convertBitmapToLEDHex(image!, true);
hexStrings += convertBitmapToLEDHex(image!, true);
x += 5;
} else {
List<String> hs =
Expand All @@ -43,6 +47,22 @@ class Converters {
return hexStrings;
}

void badgeAnimation(String message) async {
if (message == "") {
//geerate a 2d list with all values as 0
List<List<int>> image =
List.generate(11, (i) => List.generate(44, (j) => 0));
badgeList.setNewGrid(image);
badgeList.startAnimation();
} else {
List<String> hexStrings = await messageTohex(message);
List<int> byteArray = hexStringToByteArray(hexStrings.join());
List<List<int>> binaryArray = byteArrayToBinaryArray(byteArray);
badgeList.setNewGrid(binaryArray);
badgeList.startAnimation();
}
}

//function to convert the bitmap to the LED hex format
//it takes the 2D list of pixels and converts it to the LED hex format
static List<String> convertBitmapToLEDHex(
Expand Down Expand Up @@ -121,8 +141,6 @@ class Converters {
}
}

logger.d("Padded image: $list");

// Convert each 8-bit segment into hexadecimal strings
List<String> allHexs = [];
for (int i = 0; i < list[0].length ~/ 8; i++) {
Expand All @@ -145,7 +163,6 @@ class Converters {

allHexs.add(lineHex.toString()); // Store completed hexadecimal line
}
logger.d("All hexs: $allHexs");
return allHexs; // Return list of hexadecimal strings
}
}
2 changes: 0 additions & 2 deletions lib/bademagic_module/utils/file_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ class FileHelper {
// Convert the 2D list to JSON string
String jsonData = jsonEncode(image);

logger.d('JSON data: $jsonData');

// Write the JSON string to a file
await _writeToFile(filename, jsonData);

Expand Down
2 changes: 0 additions & 2 deletions lib/bademagic_module/utils/image_utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:ui' as ui;
import 'dart:ui';
import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart';
import 'package:badgemagic/bademagic_module/utils/converters.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -222,7 +221,6 @@ class ImageUtils {
}
}
}
logger.d("Pixel Array generated = $pixelArray");
return Converters.convertBitmapToLEDHex(pixelArray, false);
}
}
20 changes: 20 additions & 0 deletions lib/bademagic_module/utils/token_generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:badgemagic/bademagic_module/utils/byte_array_utils.dart';
import 'package:cancellation_token/cancellation_token.dart';

class TokenGenerator {
//make the class singleton
static final TokenGenerator _instance = TokenGenerator._internal();
factory TokenGenerator() => _instance;
TokenGenerator._internal();

CancellationToken token = CancellationToken();

//getter for the token
CancellationToken get getToken => token;

void refreshToken() {
token.cancel();
logger.i("Token status: ${token.isCancelled}");
token = CancellationToken();
}
}
30 changes: 30 additions & 0 deletions lib/badge_animation/ani_right.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:badgemagic/badge_animation/animation_abstract.dart';

class RightAnimation extends BadgeAnimation {
@override
void animation(
List<List<bool>> grid,
List<List<int>> newGrid,
int animationIndex,
bool validMarquee,
bool flashLEDOn,
int currentcountFrame,
int i,
int j,
int newHeight,
int newWidth,
int badgeHeight,
int badgeWidth) {
// Get the corresponding column in the new grid based on the scroll positio
int sourceCol = j;

// If sourceCol is negative, display blank space (off-screen part of the grid)
if (sourceCol >= 0 && sourceCol < newWidth) {
// Ensure flashLEDOn and validMarquee effects are applied
grid[i][j] =
validMarquee || flashLEDOn && newGrid[i % newHeight][sourceCol] == 1;
} else {
grid[i][j] = false;
}
}
}
15 changes: 15 additions & 0 deletions lib/badge_animation/animation_abstract.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
abstract class BadgeAnimation {
void animation(
List<List<bool>> grid,
List<List<int>> newGrid,
int animationIndex,
bool validMarquee,
bool flashLEDOn,
int currentcountFrame,
int i,
int j,
int newHeight,
int newWidth,
int badgeHeight,
int badgeWidth);
}
12 changes: 12 additions & 0 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ const String aniRight = 'assets/animations/ic_anim_right.gif';
const String effFlash = 'assets/effects/ic_effect_flash.gif';
const String effInvert = 'assets/effects/ic_effect_invert.gif';
const String effMarque = 'assets/effects/ic_effect_marquee.gif';

//constants for the animation speed
const Duration aniBaseSpeed = Duration(microseconds: 200000); // in uS
const Duration aniMarqueSpeed = Duration(microseconds: 100000); // in uS
const Duration aniFlashSpeed = Duration(microseconds: 500000); // in uS

// Function to calculate animation speed based on speed level
int aniSpeedStrategy(int speedLevel) {
int speedInMicroseconds = aniBaseSpeed.inMicroseconds -
(speedLevel * aniBaseSpeed.inMicroseconds ~/ 8);
return speedInMicroseconds;
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:badgemagic/providers/cardsprovider.dart';
import 'package:badgemagic/providers/drawbadge_provider.dart';
import 'package:badgemagic/providers/badgeview_provider.dart';
import 'package:badgemagic/providers/getitlocator.dart';
import 'package:badgemagic/providers/imageprovider.dart';
import 'package:badgemagic/view/draw_badge_screen.dart';
Expand Down
Loading