Provides class structure for simple control of Nixie Tubes using custom PCB
NixieTube.cpp@0:34f3e0f37c55, 2016-12-05 (annotated)
- Committer:
- fischk08
- Date:
- Mon Dec 05 21:35:57 2016 +0000
- Revision:
- 0:34f3e0f37c55
- Child:
- 1:0470cc006ce7
V1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fischk08 | 0:34f3e0f37c55 | 1 | #include "NixieTube.h" |
fischk08 | 0:34f3e0f37c55 | 2 | /* Not Currently Implmented |
fischk08 | 0:34f3e0f37c55 | 3 | #define TUBE_1_LED 0x00800 |
fischk08 | 0:34f3e0f37c55 | 4 | #define TUBE_2_LED 0x01000 |
fischk08 | 0:34f3e0f37c55 | 5 | #define TUBE_3_LED 0x02000 |
fischk08 | 0:34f3e0f37c55 | 6 | #define TUBE_4_LED 0x04000 |
fischk08 | 0:34f3e0f37c55 | 7 | #define TUBE_5_LED 0x08000 |
fischk08 | 0:34f3e0f37c55 | 8 | #define TUBE_6_LED 0x10000 |
fischk08 | 0:34f3e0f37c55 | 9 | */ |
fischk08 | 0:34f3e0f37c55 | 10 | #define TUBE_1_CATH 0x20000 |
fischk08 | 0:34f3e0f37c55 | 11 | #define TUBE_2_CATH 0x40000 |
fischk08 | 0:34f3e0f37c55 | 12 | #define TUBE_3_CATH 0x80000 |
fischk08 | 0:34f3e0f37c55 | 13 | #define TUBE_4_CATH 0x100000 |
fischk08 | 0:34f3e0f37c55 | 14 | #define TUBE_5_CATH 0x200000 |
fischk08 | 0:34f3e0f37c55 | 15 | #define TUBE_6_CATH 0x400000 |
fischk08 | 0:34f3e0f37c55 | 16 | #define START_THREAD 1 |
fischk08 | 0:34f3e0f37c55 | 17 | Mutex nixie_mutex; |
fischk08 | 0:34f3e0f37c55 | 18 | |
fischk08 | 0:34f3e0f37c55 | 19 | NixieTube::NixieTube(PinName sdi, PinName scl, PinName rcl, PinName cen): _thread(&NixieTube::threadStarter, this, osPriorityNormal,1024),_sdi(sdi), _scl(scl), _rcl(rcl), _cen(cen){ |
fischk08 | 0:34f3e0f37c55 | 20 | digit1 = 1; |
fischk08 | 0:34f3e0f37c55 | 21 | digit2 = 2; |
fischk08 | 0:34f3e0f37c55 | 22 | digit3 = 3; |
fischk08 | 0:34f3e0f37c55 | 23 | digit4 = 4; |
fischk08 | 0:34f3e0f37c55 | 24 | delay1 = 0.0005; |
fischk08 | 0:34f3e0f37c55 | 25 | delay2 = 0.0045; |
fischk08 | 0:34f3e0f37c55 | 26 | brightness = 0; |
fischk08 | 0:34f3e0f37c55 | 27 | _cen.write(1.0); |
fischk08 | 0:34f3e0f37c55 | 28 | _cen.period_us(10000); |
fischk08 | 0:34f3e0f37c55 | 29 | _thread.signal_set(START_THREAD); |
fischk08 | 0:34f3e0f37c55 | 30 | } |
fischk08 | 0:34f3e0f37c55 | 31 | |
fischk08 | 0:34f3e0f37c55 | 32 | void NixieTube::threadStarter(void const *p) { |
fischk08 | 0:34f3e0f37c55 | 33 | NixieTube *instance = (NixieTube*)p; |
fischk08 | 0:34f3e0f37c55 | 34 | instance->threadWorker(); |
fischk08 | 0:34f3e0f37c55 | 35 | } |
fischk08 | 0:34f3e0f37c55 | 36 | |
fischk08 | 0:34f3e0f37c55 | 37 | void NixieTube::threadWorker() { |
fischk08 | 0:34f3e0f37c55 | 38 | _thread.signal_wait(START_THREAD); |
fischk08 | 0:34f3e0f37c55 | 39 | while(1) { |
fischk08 | 0:34f3e0f37c55 | 40 | nixie_thread(); |
fischk08 | 0:34f3e0f37c55 | 41 | } |
fischk08 | 0:34f3e0f37c55 | 42 | } |
fischk08 | 0:34f3e0f37c55 | 43 | |
fischk08 | 0:34f3e0f37c55 | 44 | NixieTube::~NixieTube() |
fischk08 | 0:34f3e0f37c55 | 45 | { |
fischk08 | 0:34f3e0f37c55 | 46 | } |
fischk08 | 0:34f3e0f37c55 | 47 | |
fischk08 | 0:34f3e0f37c55 | 48 | int NixieTube::nixie_id_convert(int digit) { |
fischk08 | 0:34f3e0f37c55 | 49 | if (digit == 0 || digit == -1) { |
fischk08 | 0:34f3e0f37c55 | 50 | return digit + 1; |
fischk08 | 0:34f3e0f37c55 | 51 | } |
fischk08 | 0:34f3e0f37c55 | 52 | if (digit < 10) { |
fischk08 | 0:34f3e0f37c55 | 53 | return 11 - digit; |
fischk08 | 0:34f3e0f37c55 | 54 | } |
fischk08 | 0:34f3e0f37c55 | 55 | if (digit == 10) { |
fischk08 | 0:34f3e0f37c55 | 56 | return 11; |
fischk08 | 0:34f3e0f37c55 | 57 | } |
fischk08 | 0:34f3e0f37c55 | 58 | return 0; |
fischk08 | 0:34f3e0f37c55 | 59 | } |
fischk08 | 0:34f3e0f37c55 | 60 | |
fischk08 | 0:34f3e0f37c55 | 61 | void NixieTube::send_nixie_shiftreg(int cathode_id, int led_bitfield, int tube_bitfield) { |
fischk08 | 0:34f3e0f37c55 | 62 | |
fischk08 | 0:34f3e0f37c55 | 63 | int buffer = 0; |
fischk08 | 0:34f3e0f37c55 | 64 | buffer = 1 << (cathode_id-1) | led_bitfield | tube_bitfield; |
fischk08 | 0:34f3e0f37c55 | 65 | |
fischk08 | 0:34f3e0f37c55 | 66 | for (int i=23; i >= 0; i--) { |
fischk08 | 0:34f3e0f37c55 | 67 | _sdi = buffer & (1 << i); |
fischk08 | 0:34f3e0f37c55 | 68 | _scl = 1; |
fischk08 | 0:34f3e0f37c55 | 69 | _scl = 0; // Whats going on here? |
fischk08 | 0:34f3e0f37c55 | 70 | } |
fischk08 | 0:34f3e0f37c55 | 71 | _rcl = 1; |
fischk08 | 0:34f3e0f37c55 | 72 | _rcl = 0; // Whats going on here? |
fischk08 | 0:34f3e0f37c55 | 73 | |
fischk08 | 0:34f3e0f37c55 | 74 | } |
fischk08 | 0:34f3e0f37c55 | 75 | |
fischk08 | 0:34f3e0f37c55 | 76 | void NixieTube::nixie_thread() { |
fischk08 | 0:34f3e0f37c55 | 77 | while (1) { |
fischk08 | 0:34f3e0f37c55 | 78 | nixie_mutex.lock(); |
fischk08 | 0:34f3e0f37c55 | 79 | send_nixie_shiftreg(nixie_id_convert(digit1), 0, TUBE_1_CATH); |
fischk08 | 0:34f3e0f37c55 | 80 | Thread::wait(1); |
fischk08 | 0:34f3e0f37c55 | 81 | send_nixie_shiftreg(0, 0, 0); |
fischk08 | 0:34f3e0f37c55 | 82 | Thread::wait(1); |
fischk08 | 0:34f3e0f37c55 | 83 | send_nixie_shiftreg(nixie_id_convert(digit2), 0, TUBE_2_CATH); |
fischk08 | 0:34f3e0f37c55 | 84 | Thread::wait(1); |
fischk08 | 0:34f3e0f37c55 | 85 | send_nixie_shiftreg(0, 0, 0); |
fischk08 | 0:34f3e0f37c55 | 86 | Thread::wait(1); |
fischk08 | 0:34f3e0f37c55 | 87 | send_nixie_shiftreg(nixie_id_convert(digit3), 0, TUBE_3_CATH); |
fischk08 | 0:34f3e0f37c55 | 88 | Thread::wait(1); |
fischk08 | 0:34f3e0f37c55 | 89 | send_nixie_shiftreg(0, 0, 0); |
fischk08 | 0:34f3e0f37c55 | 90 | Thread::wait(1); |
fischk08 | 0:34f3e0f37c55 | 91 | send_nixie_shiftreg(nixie_id_convert(digit4), 0, TUBE_4_CATH); |
fischk08 | 0:34f3e0f37c55 | 92 | Thread::wait(1); |
fischk08 | 0:34f3e0f37c55 | 93 | send_nixie_shiftreg(0, 0, 0); |
fischk08 | 0:34f3e0f37c55 | 94 | Thread::wait(1); |
fischk08 | 0:34f3e0f37c55 | 95 | nixie_mutex.unlock(); |
fischk08 | 0:34f3e0f37c55 | 96 | } |
fischk08 | 0:34f3e0f37c55 | 97 | } |
fischk08 | 0:34f3e0f37c55 | 98 | void NixieTube::start_nixie(){ |
fischk08 | 0:34f3e0f37c55 | 99 | //thread = new Thread(nixie_thread, this); |
fischk08 | 0:34f3e0f37c55 | 100 | } |
fischk08 | 0:34f3e0f37c55 | 101 | void NixieTube::update_nixie_tube(int ref,int val){ |
fischk08 | 0:34f3e0f37c55 | 102 | switch(ref){ |
fischk08 | 0:34f3e0f37c55 | 103 | case(1): |
fischk08 | 0:34f3e0f37c55 | 104 | digit1 = val; |
fischk08 | 0:34f3e0f37c55 | 105 | case(2): |
fischk08 | 0:34f3e0f37c55 | 106 | digit2 = val; |
fischk08 | 0:34f3e0f37c55 | 107 | case(3): |
fischk08 | 0:34f3e0f37c55 | 108 | digit3 = val; |
fischk08 | 0:34f3e0f37c55 | 109 | case(4): |
fischk08 | 0:34f3e0f37c55 | 110 | digit4 = val; |
fischk08 | 0:34f3e0f37c55 | 111 | } |
fischk08 | 0:34f3e0f37c55 | 112 | } |
fischk08 | 0:34f3e0f37c55 | 113 | void NixieTube::set_dim(float duty,int pulsewidth){ |
fischk08 | 0:34f3e0f37c55 | 114 | _cen.write(duty); |
fischk08 | 0:34f3e0f37c55 | 115 | _cen.period_us(pulsewidth); |
fischk08 | 0:34f3e0f37c55 | 116 | } |