Browse Source

fixed float convert

master
Z 5 months ago
parent
commit
50ad86dc94
  1. 30
      Example/Gateway/Script/Data/DataGatewayInfo.cs
  2. 12
      Example/Gateway/Script/View/ViewModbusGateway.cs
  3. 14
      Example/Theme/StyleBox/CardFrame.tres
  4. 29
      Example/Tween/EGTween.tscn
  5. 12
      Example/Tween/Script/TweenTest.cs
  6. 134
      addons/EGFramework/Module/Extension/EGConvertExtension.cs

30
Example/Gateway/Script/Data/DataGatewayInfo.cs

@ -0,0 +1,30 @@
namespace EGFramework.Examples.Gateway{
public class DataGatewayInfo
{
public AddressTCPClient TCPClientAddress { set; get; }
}
public interface IAddress{
void GetSender();
}
public struct AddressTCPClient{
public string Host { set; get; }
public int Port { set; get; }
}
public struct AddressTCPServer{
public int Port { set; get; }
}
public struct AddressUDP{
public string Host { set; get; }
public int Port { set; get; }
}
public struct AddressHttpServer{
public string[] prefix { set; get; }
}
public struct AddressHttpClient{
public string[] TargetAddress { set; get; }
}
}

12
Example/Gateway/Script/View/ViewModbusGateway.cs

@ -15,7 +15,14 @@ namespace EGFramework.Examples.Gateway{
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
InitGateway(); if(this.Visible){
InitGateway();
}
// byte[] fData ={0x40,0xB9,0x9B,0x00,0x00,0x00,0x00,0x00};
// fData.Reverse();
// GD.Print(fData.ToDoubleArray()[0]);
// double t = 54.32f;
// GD.Print(BitConverter.GetBytes(fData.ToDoubleArray()[0]).ToStringByHex());
} }
// Called every frame. 'delta' is the elapsed time since the previous frame. // Called every frame. 'delta' is the elapsed time since the previous frame.
@ -53,6 +60,9 @@ namespace EGFramework.Examples.Gateway{
} }
public async void PushDataToGateway(){ public async void PushDataToGateway(){
if(!this.Visible){
return;
}
JObject pushData = new JObject(); JObject pushData = new JObject();
foreach(KeyValuePair<string,DataModbus485Device> device485 in Setting.Devices485){ foreach(KeyValuePair<string,DataModbus485Device> device485 in Setting.Devices485){
foreach(KeyValuePair<string,DataModbusRegister> register in Setting.Devices485[device485.Key].Registers){ foreach(KeyValuePair<string,DataModbusRegister> register in Setting.Devices485[device485.Key].Registers){

14
Example/Theme/StyleBox/CardFrame.tres

@ -0,0 +1,14 @@
[gd_resource type="StyleBoxFlat" format=3 uid="uid://t4prctf3uxtb"]
[resource]
bg_color = Color(0.858824, 0.858824, 0.858824, 1)
border_width_left = 2
border_width_top = 2
border_width_right = 2
border_width_bottom = 2
border_color = Color(0, 0, 0, 1)
corner_radius_top_left = 16
corner_radius_top_right = 16
corner_radius_bottom_right = 16
corner_radius_bottom_left = 16
corner_detail = 1

29
Example/Tween/EGTween.tscn

@ -1,6 +1,7 @@
[gd_scene load_steps=2 format=3 uid="uid://1garq2laye1n"] [gd_scene load_steps=3 format=3 uid="uid://1garq2laye1n"]
[ext_resource type="Script" path="res://Example/Tween/Script/TweenTest.cs" id="1_jpym0"] [ext_resource type="Script" path="res://Example/Tween/Script/TweenTest.cs" id="1_jpym0"]
[ext_resource type="StyleBox" uid="uid://t4prctf3uxtb" path="res://Example/Theme/StyleBox/CardFrame.tres" id="2_cbb73"]
[node name="EgTween" type="Control" node_paths=PackedStringArray("Target")] [node name="EgTween" type="Control" node_paths=PackedStringArray("Target")]
layout_mode = 3 layout_mode = 3
@ -10,20 +11,16 @@ anchor_bottom = 1.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
script = ExtResource("1_jpym0") script = ExtResource("1_jpym0")
Target = NodePath("ColorRect") Target = NodePath("Card1")
[node name="ColorRect" type="ColorRect" parent="."] [node name="Card1" type="Panel" parent="."]
layout_mode = 1 custom_minimum_size = Vector2(90, 160)
anchors_preset = 8 layout_mode = 2
anchor_left = 0.5 offset_left = 390.0
anchor_top = 0.5 offset_top = 488.0
anchor_right = 0.5 offset_right = 480.0
anchor_bottom = 0.5 offset_bottom = 648.0
offset_left = -20.0
offset_top = -20.0
offset_right = 20.0
offset_bottom = 20.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 0
rotation = 1.14319 pivot_offset = Vector2(45, 160)
pivot_offset = Vector2(20, 20) theme_override_styles/panel = ExtResource("2_cbb73")

12
Example/Tween/Script/TweenTest.cs

@ -3,15 +3,15 @@ using System;
namespace EGFramework.Examples.TweenAnime{ namespace EGFramework.Examples.TweenAnime{
public partial class TweenTest : Node public partial class TweenTest : Node
{ {
[Export] ColorRect Target {set;get;} [Export] Control Target {set;get;}
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
Tween tween = GetTree().CreateTween(); // Tween tween = GetTree().CreateTween();
tween.TweenProperty(Target,"color",Colors.Red,1.0f); // tween.TweenProperty(Target,"color",Colors.Red,1.0f);
tween.TweenProperty(Target,"color",Colors.Green,1.0f); // tween.TweenProperty(Target,"color",Colors.Green,1.0f);
tween.TweenProperty(Target,"color",Colors.Blue,1.0f); // tween.TweenProperty(Target,"color",Colors.Blue,1.0f);
tween.SetLoops(); // tween.SetLoops();
// Tween tween2 = GetTree().CreateTween(); // Tween tween2 = GetTree().CreateTween();
// tween2.TweenProperty(Target,"position",new Vector2(100,0),1.0f); // tween2.TweenProperty(Target,"position",new Vector2(100,0),1.0f);

134
addons/EGFramework/Module/Extension/EGConvertExtension.cs

@ -104,6 +104,14 @@ namespace EGFramework {
return BitConverter.ToUInt16(self, 0); return BitConverter.ToUInt16(self, 0);
} }
public static ushort ToUShortLittleEndian(this byte[] self){
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(self);
}
return BitConverter.ToUInt16(self, 0);
}
public static byte[] ToBytes(this uint self){ public static byte[] ToBytes(this uint self){
byte[] byteArray = BitConverter.GetBytes(self); byte[] byteArray = BitConverter.GetBytes(self);
if (BitConverter.IsLittleEndian) if (BitConverter.IsLittleEndian)
@ -112,6 +120,14 @@ namespace EGFramework {
} }
return byteArray; return byteArray;
} }
public static byte[] ToBytesLittleEndian(this uint self){
byte[] byteArray = BitConverter.GetBytes(self);
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(byteArray);
}
return byteArray;
}
public static uint ToUINT(this byte[] self){ public static uint ToUINT(this byte[] self){
if (BitConverter.IsLittleEndian) if (BitConverter.IsLittleEndian)
@ -120,6 +136,27 @@ namespace EGFramework {
} }
return BitConverter.ToUInt32(self, 0); return BitConverter.ToUInt32(self, 0);
} }
public static uint ToUINTLittleEndian(this byte[] self){
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(self);
}
return BitConverter.ToUInt32(self, 0);
}
public static byte[] ToBytes(this uint[] uintArray)
{
int byteCount = uintArray.Length * sizeof(uint);
byte[] byteArray = new byte[byteCount];
for (int i = 0; i < uintArray.Length; i++)
{
byte[] tempBytes = BitConverter.GetBytes(uintArray[i]);
Array.Copy(tempBytes, 0, byteArray, i * sizeof(uint), sizeof(uint));
}
return byteArray;
}
/// <summary> /// <summary>
/// convert and resize byte array,such as uint is 0x00FF7799 => byte array {0xFF,0x77,0x99} /// convert and resize byte array,such as uint is 0x00FF7799 => byte array {0xFF,0x77,0x99}
@ -178,6 +215,17 @@ namespace EGFramework {
return boolArray; return boolArray;
} }
public static bool[] ToBoolArray(this byte byteData)
{
bool[] boolArray = new bool[8];
byte currentByte = byteData;
for (int j = 0; j < 8; j++)
{
boolArray[j] = (currentByte & (1 << j)) != 0;
}
return boolArray;
}
public static bool[] ToBoolArray(this int value) public static bool[] ToBoolArray(this int value)
{ {
string binaryString = Convert.ToString(value, 2); string binaryString = Convert.ToString(value, 2);
@ -214,5 +262,91 @@ namespace EGFramework {
return ushortArray; return ushortArray;
} }
public static byte[] ToByteArray(this float[] floatArray)
{
byte[] byteArray = new byte[floatArray.Length * 4];
for (int i = 0; i < floatArray.Length; i++)
{
byte[] tempArray = BitConverter.GetBytes(floatArray[i]);
if(!BitConverter.IsLittleEndian){
Array.Reverse(tempArray);
}
//Array.Reverse(tempArray); // 大端序需要反转字节数组以满足高字节在后
Array.Copy(tempArray, 0, byteArray, i * 4, 4);
}
return byteArray;
}
public static float[] ToFloatArray(this byte[] byteArray)
{
float[] floatArray = new float[byteArray.Length / 4];
for (int i = 0; i < floatArray.Length; i++)
{
byte[] tempArray = new byte[4];
Array.Copy(byteArray, i * 4, tempArray, 0, 4);
if(!BitConverter.IsLittleEndian){
Array.Reverse(tempArray);
}
//Array.Reverse(tempArray);
floatArray[i] = BitConverter.ToSingle(tempArray, 0);
}
return floatArray;
}
public static double[] ToDoubleArray(this byte[] byteArray)
{
double[] doubleArray = new double[byteArray.Length / 8];
for (int i = 0; i < doubleArray.Length; i++)
{
byte[] tempArray = new byte[8];
Array.Copy(byteArray, i * 8, tempArray, 0, 8);
if(!BitConverter.IsLittleEndian){
Array.Reverse(tempArray);
}
//Array.Reverse(tempArray);
doubleArray[i] = BitConverter.ToDouble(tempArray, 0);
}
return doubleArray;
}
public static byte[] ToByteArray(this int[] intArray)
{
byte[] byteArray = new byte[intArray.Length * 4];
for (int i = 0; i < intArray.Length; i++)
{
byte[] tempArray = BitConverter.GetBytes(intArray[i]);
//Array.Reverse(tempArray); // 大端序需要反转字节数组以满足高字节在后
Array.Copy(tempArray, 0, byteArray, i * 4, 4);
}
return byteArray;
}
public static byte[] Reverse(this byte[] bytes){
Array.Reverse(bytes);
return bytes;
}
public static byte[] ToSubByte(this byte[] bytes,int index,int length){
byte[] resultByte = new byte[length];
Array.Copy(bytes,index,resultByte,0,length);
return resultByte;
}
public static float[] ToSubFloat(this float[] floats,int index,int length){
float[] resultFloats = new float[length];
Array.Copy(floats,index,resultFloats,0,length);
return resultFloats;
}
public static float[] ToSubArrayByCount(this float[] originalArray, int targetLength)
{
float[] reducedArray = new float[targetLength];
float ratio = (float)(originalArray.Length - 1) / (targetLength - 1);
for (int i = 0; i < targetLength; i++)
{
int originalIndex = (int)Math.Round(ratio * i);
reducedArray[i] = originalArray[originalIndex];
}
return reducedArray;
}
} }
} }

Loading…
Cancel
Save