|
1 | 1 | /*=============================================================================| |
2 | | -| PROJECT SNAP7 1.2.0 | |
| 2 | +| PROJECT SNAP7 1.3.0 | |
3 | 3 | |==============================================================================| |
4 | 4 | | Copyright (C) 2013, 2014 Davide Nardella | |
5 | 5 | | All rights reserved. | |
@@ -767,4 +767,138 @@ TextString SrvEventText(TSrvEvent *Event) |
767 | 767 | Srv_EventText(Event, text, TextLen); |
768 | 768 | return TextString(text); |
769 | 769 | } |
| 770 | +//============================================================================== |
| 771 | +// Helper GET routines |
| 772 | +//============================================================================== |
| 773 | +word SwapWord(pword Value) |
| 774 | +{ |
| 775 | + return ((*Value >> 8) & 0xFF) | ((*Value << 8) & 0xFF00); |
| 776 | +} |
| 777 | +//--------------------------------------------------------------------------- |
| 778 | +longword SwapDWord(plongword Value) |
| 779 | +{ |
| 780 | + return (*Value >> 24) | ((*Value << 8) & 0x00FF0000) | ((*Value >> 8) & 0x0000FF00) | (*Value << 24); |
| 781 | +} |
| 782 | +//--------------------------------------------------------------------------- |
| 783 | +bool GetBitAt(void *Buffer, int Pos, int Bit) |
| 784 | +{ |
| 785 | + byte Mask[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; |
| 786 | + if (Bit < 0) Bit = 0; |
| 787 | + if (Bit > 7) Bit = 7; |
| 788 | + return (*(pbyte(Buffer)+Pos) & Mask[Bit]) != 0; |
| 789 | +} |
| 790 | +//--------------------------------------------------------------------------- |
| 791 | +byte GetByteAt(void *Buffer, int Pos) |
| 792 | +{ |
| 793 | + return *(pbyte(Buffer)+Pos); |
| 794 | +} |
| 795 | +//--------------------------------------------------------------------------- |
| 796 | +word GetWordAt(void *Buffer, int Pos) |
| 797 | +{ |
| 798 | + return SwapWord(pword(pbyte(Buffer)+Pos)); |
| 799 | +} |
| 800 | +//--------------------------------------------------------------------------- |
| 801 | +smallint GetIntAt(void *Buffer, int Pos) |
| 802 | +{ |
| 803 | + return smallint(SwapWord(pword(pbyte(Buffer)+Pos))); |
| 804 | +} |
| 805 | +//--------------------------------------------------------------------------- |
| 806 | +longword GetDWordAt(void *Buffer, int Pos) |
| 807 | +{ |
| 808 | + return SwapDWord(plongword(pbyte(Buffer)+Pos)); |
| 809 | +} |
| 810 | +//--------------------------------------------------------------------------- |
| 811 | +longint GetDIntAt(void *Buffer, int Pos) |
| 812 | +{ |
| 813 | + return longint(SwapDWord(plongword(pbyte(Buffer)+Pos))); |
| 814 | +} |
| 815 | +//--------------------------------------------------------------------------- |
| 816 | +float GetRealAt(void *Buffer, int Pos) |
| 817 | +{ |
| 818 | + longword lw=SwapDWord(plongword(pbyte(Buffer)+Pos)); |
| 819 | + return *pfloat(&lw); |
| 820 | +} |
| 821 | +//--------------------------------------------------------------------------- |
| 822 | +byte BCDtoByte(byte B) |
| 823 | +{ |
| 824 | + return ((B >> 4) * 10) + (B & 0x0F); |
| 825 | +} |
| 826 | +//--------------------------------------------------------------------------- |
| 827 | +struct tm GetDateTimeAt(void *Buffer, int Pos) |
| 828 | +{ |
| 829 | + struct tm DateTime; |
| 830 | + pbyte p = pbyte(Buffer)+Pos; |
| 831 | + word Year=BCDtoByte(*p); |
770 | 832 |
|
| 833 | + if (Year<90) Year+=100; |
| 834 | + DateTime.tm_year=Year; |
| 835 | + DateTime.tm_mon =BCDtoByte(*(p+1))-1; |
| 836 | + DateTime.tm_mday=BCDtoByte(*(p+2)); |
| 837 | + DateTime.tm_hour=BCDtoByte(*(p+3)); |
| 838 | + DateTime.tm_min =BCDtoByte(*(p+4)); |
| 839 | + DateTime.tm_sec =BCDtoByte(*(p+5)); |
| 840 | + DateTime.tm_wday=(*(p+7) & 0x0F)-1; |
| 841 | + return DateTime; |
| 842 | +} |
| 843 | +//============================================================================== |
| 844 | +// Helper SET routines |
| 845 | +//============================================================================== |
| 846 | +void SetBitAt(void *Buffer, int Pos, int Bit, bool Value) |
| 847 | +{ |
| 848 | + byte Mask[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; |
| 849 | + pbyte p = pbyte(Buffer)+Pos; |
| 850 | + if (Bit < 0) Bit = 0; |
| 851 | + if (Bit > 7) Bit = 7; |
| 852 | + (Value) ? *p |= Mask[Bit] : *p &= ~Mask[Bit]; |
| 853 | +} |
| 854 | +//--------------------------------------------------------------------------- |
| 855 | +void SetByteAt(void *Buffer, int Pos, byte Value) |
| 856 | +{ |
| 857 | + *(pbyte(Buffer)+Pos)=Value; |
| 858 | +} |
| 859 | +//--------------------------------------------------------------------------- |
| 860 | +void SetWordAt(void *Buffer, int Pos, word Value) |
| 861 | +{ |
| 862 | + *(pword(pbyte(Buffer)+Pos))=SwapWord(&Value); |
| 863 | +} |
| 864 | +//--------------------------------------------------------------------------- |
| 865 | +void SetIntAt(void *Buffer, int Pos, smallint Value) |
| 866 | +{ |
| 867 | + *(psmallint(pbyte(Buffer)+Pos))=SwapWord(pword(&Value)); |
| 868 | +} |
| 869 | +//--------------------------------------------------------------------------- |
| 870 | +void SetDWordAt(void *Buffer, int Pos, longword Value) |
| 871 | +{ |
| 872 | + *(plongword(pbyte(Buffer)+Pos))=SwapDWord(&Value); |
| 873 | +} |
| 874 | +//--------------------------------------------------------------------------- |
| 875 | +void SetDIntAt(void *Buffer, int Pos, longint Value) |
| 876 | +{ |
| 877 | + *(plongint(pbyte(Buffer)+Pos))=SwapDWord(plongword(&Value)); |
| 878 | +} |
| 879 | +//--------------------------------------------------------------------------- |
| 880 | +void SetRealAt(void *Buffer, int Pos, float Value) |
| 881 | +{ |
| 882 | + *(plongword(pbyte(Buffer)+Pos))=SwapDWord(plongword(&Value)); |
| 883 | +} |
| 884 | +//--------------------------------------------------------------------------- |
| 885 | +byte WordToBCD(word Value) |
| 886 | +{ |
| 887 | + return ((Value / 10) << 4) | (Value % 10); |
| 888 | +} |
| 889 | +//--------------------------------------------------------------------------- |
| 890 | +void SetDateTimeAt(void *Buffer, int Pos, tm Value) |
| 891 | +{ |
| 892 | + pbyte p = pbyte(Buffer)+Pos; |
| 893 | + word Year = Value.tm_year; |
| 894 | + |
| 895 | + if (Year>99) Year-=100; |
| 896 | + *p=WordToBCD(Year); |
| 897 | + *(p+1)=WordToBCD(Value.tm_mon+1); |
| 898 | + *(p+2)=WordToBCD(Value.tm_mday); |
| 899 | + *(p+3)=WordToBCD(Value.tm_hour); |
| 900 | + *(p+4)=WordToBCD(Value.tm_min); |
| 901 | + *(p+5)=WordToBCD(Value.tm_sec); |
| 902 | + *(p+6)=0; |
| 903 | + *(p+7)=Value.tm_wday+1; |
| 904 | +} |
0 commit comments