-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
159 lines (100 loc) · 4.49 KB
/
Program.cs
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
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Text;
using NETMF.Nordic;
using WJMIOT;
namespace NetduinoOLED
{
public class Program
{
//Global globalSPIDevice
static SPI.Configuration oledSpiConfig=new SPI.Configuration(Pins.GPIO_PIN_D10, false, 0, 0, false, true, 10000, SPI.SPI_module.SPI1);
static SPI.Configuration nrf24L01PConfig = new SPI.Configuration(Pins.GPIO_PIN_D9, false, 0, 0, false, true, 10000, SPI.SPI_module.SPI1);
static SPI globalSPIDevice=new SPI(oledSpiConfig);
//LED
static OutputPort dataCommandPin = new OutputPort(Pins.GPIO_PIN_D7, true);
static OutputPort resetPin = new OutputPort(Pins.GPIO_PIN_D6, true);
static OLED_sh1106 oled = new OLED_sh1106(ref globalSPIDevice, dataCommandPin, resetPin);
//nrf24L01Plus
static NRF24L01Plus _radio=new NRF24L01Plus(ref globalSPIDevice, Pins.GPIO_PIN_D3, Pins.GPIO_PIN_D2);
static Timer _timer;
static int count;
public static void Main()
{
count=0;
oled.Inital();
Thread.Sleep(100);
oled.DisplayString("***");
globalSPIDevice.Config = nrf24L01PConfig;
_radio.Initialize();
//TX
// _radio.Configure(new byte[] { 138, 138, 138 }, 76, NRFDataRate.DR1Mbps);
// _radio.SetAddress(AddressSlot.One, new byte[] { 0, 0, 1 });
//RX
_radio.Configure(new byte[] { 0xF0, 0xF0, 0xE1 }, 76, NRFDataRate.DR1Mbps);
_radio.SetAddress(AddressSlot.One, new byte[] { 0, 0, 2 });
_radio.OnDataReceived += _radio_OnDataReceived;
_radio.Enable();
string outputinfo = "Listening on: " +
ByteArrayToHexString(_radio.GetAddress(AddressSlot.Zero, 3)) + " | " +
ByteArrayToHexString(_radio.GetAddress(AddressSlot.One, 3)) + " | " +
ByteArrayToHexString(_radio.GetAddress(AddressSlot.Two, 3)) + " | " +
ByteArrayToHexString(_radio.GetAddress(AddressSlot.Three, 3)) + " | " +
ByteArrayToHexString(_radio.GetAddress(AddressSlot.Four, 3)) + " | " +
ByteArrayToHexString(_radio.GetAddress(AddressSlot.Five, 3));
Debug.Print(outputinfo);
//TX
// _timer = new Timer(TimerFire, null, new TimeSpan(0, 0, 0, 2), new TimeSpan(0, 0, 0,5));
globalSPIDevice.Config = oledSpiConfig;
oled.DisplayString(outputinfo);
Thread.Sleep(50);
globalSPIDevice.Config = nrf24L01PConfig;
Thread.Sleep(Timeout.Infinite);
/*
globalSPIDevice.Config = oledSpiConfig;
oled.DisplayString("Receiving ---");
Thread.Sleep(100);
globalSPIDevice.Config = nrf24L01PConfig;
*/
}
static string ByteArrayToHexString(byte[] Bytes)
{
StringBuilder Result = new StringBuilder(Bytes.Length * 2);
string HexAlphabet = "0123456789ABCDEF";
foreach (byte B in Bytes)
{
Result.Append(HexAlphabet[(int)(B >> 4)]);
Result.Append(HexAlphabet[(int)(B & 0xF)]);
}
return Result.ToString();
}
static void _radio_OnDataReceived(byte[] data)
{
count++;
Debug.Print("Received: " + new string(Encoding.UTF8.GetChars(data)));
Thread.Sleep(50);
globalSPIDevice.Config = oledSpiConfig;
oled.DisplayString("Received: " + new string(Encoding.UTF8.GetChars(data)));
Thread.Sleep(50);
globalSPIDevice.Config = nrf24L01PConfig;
Thread.Sleep(100);
_radio.SendTo(new byte[] { 0, 0, 1 }, Encoding.UTF8.GetBytes("DDD" + count.ToString()));
}
static void TimerFire(object state)
{
count++;
_radio.SendTo(new byte[] { 0, 0, 2 }, Encoding.UTF8.GetBytes("Hello_"+ count.ToString()));
Thread.Sleep(50);
globalSPIDevice.Config = oledSpiConfig;
oled.DisplayString("Hello_" + count.ToString());
Thread.Sleep(50);
globalSPIDevice.Config = nrf24L01PConfig;
}
}
}